Bubble Sort program
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 | #include<iostream.h> #include<conio.h> #define SIZE 10 void main(void) { int L[SIZE]; int i, n=0, current, temp, walk, sorted; clrscr(); cout<<"Input Number of elements to be sorted : "; cin>>n; cout<<"\nInput unordered list of elements to be sorted : \n"; for(i=0; i<n; i++) cin>>L[i]; for(current=0, sorted=0; current<=n && !sorted; current++) for(walk=n,sorted=1; walk>current; walk--) if(L[walk] < L[walk-1]) { sorted=0; temp=L[walk]; L[walk] = L[walk-1]; L[walk-1]=temp; } cout<<"\nThe sorted number are : \n"; for(i=0; i<n; i++) cout<<"\t"<<L[i]; getch(); } |
Input Number of elements to be sorted : 5
Input
unordered list of elements to be sorted : 89 12 66 3 1
The sorted number are :
|
||||
1
|
3
|
12
|
66
|
89
|
