#include <iostream>
using namespace std;
void read_list(int list[],int n);
void print_list(int list[],int n);
void bubble(int list[],int n);
int main()
{
int list[5];
read_list(list,5);
print_list(list,5);
cout <<endl;
bubble(list,5);
print_list(list,5);
}
void read_list(int list[],int n)
{cout<<"enter "<<n<<" integers"<<endl;
for(int i=0;i<n;++i)
cin>>list[i];
}
void print_list(int list[],int n)
{ for(int i=0;i<n;++i)
cout<<list[i]<<endl;
}
void bubble(int c[], int n)
{
int temp;
int i,j,swapsmade=1;
for( i=n; i >=2 && swapsmade ; --i)
{ cout<<"i is:"<<i<<endl;
swapsmade = 0;
for(j=0; j < i-1; ++j)
{ cout<<"j is:"<<j<<endl;
//these couts purpose is trace
cout << "comparing:"<<c[j] <<"with:" <<c[j+1]<<endl;
if(c[j] > c[j+1])
{ temp = c[j];
c[j]=c[j+1];
c[j+1] = temp;
swapsmade =1;
}
}
}
}
[pt@cs my_prog1]$ c++ bubble.cpp
[pt@cs my_prog1]$ a.out
enter 5 integers
9 6 4 2 1
9
6
4
2
1
i is:5
j is:0
comparing:9with:6
j is:1
comparing:9with:4
j is:2
comparing:9with:2
j is:3
comparing:9with:1
i is:4
j is:0
comparing:6with:4
j is:1
comparing:6with:2
j is:2
comparing:6with:1
i is:3
j is:0
comparing:4with:2
j is:1
comparing:4with:1
i is:2
j is:0
comparing:2with:1
1
2
4
6
9
[pt@cs my_prog1]$ a.out
enter 5 integers
1 2 3 4 5
1
2
3
4
5
i is:5
j is:0
comparing:1with:2
j is:1
comparing:2with:3
j is:2
comparing:3with:4
j is:3
comparing:4with:5
1
2
3
4
5
No comments:
Post a Comment