Hey guys i'm trying to get the bubble sort to work in my if(choice==2) statement but it's not working right at all. Any ideas?
Thanks!
Code:#include <iostream> #include <iomanip> #include <algorithm> using namespace std; int main() { int i,num[20],n,j,choice,tmp; cout<< "Please enter 20 integers"; for (i=0; i<20; i++) { cout<<"\nEnter next value:"; cin>>num[i]; } cout<<"\n1.Display original data.\n"; cout<<"2.Sort the data into descending order\n"; cout<<"3.Display the sorted data (Only if you've already sorted)\n"; cout<<"4.Get the address of the first element of array.\n\n"; cin>>choice; if (choice==1) { for (i=0; i<20; i++) { cout<<"\n"<<num[i]<<endl; } return 0; } else if (choice==2) { n=20; for (i=0; i<n; i++) { for (j=0; j<n-i; j++) { if (num[j+1] < num[j]) /* compare the two neighbors */ { tmp = num[j]; /* swap a[j] and a[j+1] */ num[j] = num[j+1]; num[j+1] = tmp; cout<<"Here are your numbers:"<<tmp<<endl; } } } } }



LinkBack URL
About LinkBacks



