1void bubbleSort (int S[ ], int length) {
2 bool isSorted = false;
3 while(!isSorted)
4 {
5 isSorted = true;
6 for(int i = 0; i<length; i++)
7 {
8 if(S[i] > S[i+1])
9 {
10 int temp = S[i];
11 S[i] = S[i+1];
12 S[i+1] = temp;
13 isSorted = false;
14 }
15 }
16 length--;
17}
18}
19
1#include <map>
2#include <set>
3#include <list>
4#include <cmath>
5#include <ctime>
6#include <deque>
7#include <queue>
8#include <stack>
9#include <string>
10#include <bitset>
11#include <cstdio>
12#include <limits>
13#include <vector>
14#include <climits>
15#include <cstring>
16#include <cstdlib>
17#include <fstream>
18#include <numeric>
19#include <sstream>
20#include <iostream>
21#include <algorithm>
22#include <unordered_map>
23
24using namespace std;
25
26
27int main(){
28 int n,temp,c=0;
29 cin >> n;
30 int a[n];
31 for(int i=0;i<n;i++)
32 {
33 cin>>a[i];
34 }
35 for(int i=0;i<n-1;i++)
36 {
37 for(int j=0;j<n-i-1;j++)
38 {
39 if(a[j]>a[j+1])
40 {
41 temp=a[j];
42 a[j]=a[j+1];
43 a[j+1]=temp;
44 c++;
45 }
46 }
47
48 if(c==0)
49 {
50 break;
51 }}
52 cout<<"Array is sorted in "<<c<<" swaps."<<endl;
53 cout<<"First Element:"<<" "<<a[0]<<endl;
54 cout<<"Last Element:"<<" "<<a[n-1]<<endl;
55 return 0;
56}