forked from Vishal-Aggarwal0305/DSA
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBubbleSort.cpp
More file actions
36 lines (32 loc) · 727 Bytes
/
BubbleSort.cpp
File metadata and controls
36 lines (32 loc) · 727 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include<iostream>
using namespace std;
int main ()
{
int i, j, temp, n, count=0;
cout << "Enter size of the array: "<<endl;
cin >> n;
int a[n];
cout << "\nEnter the elements of the array: "<< endl;
for(i = 0; i < n; i++)
{
cin >> a[i];
}
cout<<endl;
for(i = 0; i<n; i++) {
for(j = i+1; j<n; j++)
{
if(a[j] < a[i]) {
temp = a[i];
a[i] = a[j];
a[j] = temp;
count++;
}
}
}
cout <<"Sorted Element List using bubble sort: "<<endl;
for(i = n-1; i>=0; i--) {
cout <<a[i]<<" ";
}
cout<<"\n\nNo. of swap: "<<count;
return 0;
}