-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbubleSort.cpp
More file actions
47 lines (47 loc) · 1.01 KB
/
bubleSort.cpp
File metadata and controls
47 lines (47 loc) · 1.01 KB
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
37
38
39
40
41
42
43
44
45
46
47
#include<iostream>
using namespace std;
// function using reference variable and swap two values
void swap(int &a, int &b) {
int temp;
temp=a;
a=b;
b=temp;
}
void display(int *a, int n) {
int i;
for(i=0;i<n;i++){
cout<<a[i]<<" ";
}
}
void bubleSort(int *a, int n){
int c=1,i;
//c will count the number of time array is checked
//while loop will operate untill c is smaller than array size.
while(c<n)
{
for(i=0;i<n-1;i++)
{
if(a[i]>a[i+1]) //Element at ith index will be compared with next element and if found smaller then will be swapped with next one.
swap(a[i], a[i+1]);
}
c++;
}
}
int main()
{
int n, i;
cout<<"\nEnter Number of Elements ";
cin>>n;
int a[n];
cout<<"\nEnter "<<n<<" Elements ";
for(i=0;i<n;i++)
{
cin>>a[i];
}
cout<<"\nBefore Sorting : ";
display(a, n);
bubleSort(a, n);
cout<<"\nAfter Sorting : ";
display(a, n);
return 0;
}