selection sort compiles but does not sort the array as desired
here is the program that i had wrote plese compile on your machine and suggest the error
#include<iostream> using namespace std; void select(int *const,int const,bool(*)(int,int)); void swap(int *const,int *const); bool ascending (int,int); bool descending(int,int); int main() { const int arraysize=5; int a[arraysize]={23,1,34,12,7}; int order; cout<<"\nunsorted array is:"; for(int i=0;i<arraysize;i++) cout<<" "<<a[i]; cout<<endl; cout<<"\n enter order 1 for listing of elements in ascending order \n 2 for listing in descending order "<<endl; cin>>order; if(order==1) { cout<<"\n array in ascending order is :"; select(a,arraysize,ascending); } else { cout<<"\n array in descending order is :"; select(a,arraysize,descending); } for(int i=0;i<arraysize;i++) cout<<" "<<a[i]; cout<<endl; return 0; } void select(int *const arr,const int size,bool (*compare)(int ,int )) { int smallorlargest; for(int i=0;i<size;i++) { smallorlargest=i; for(int j=i+1;j<size;j++) { if(!(*compare)( arr[smallorlargest],arr[j])) { smallorlargest=j; } swap( &arr[smallorlargest],&arr[i] ); } } } void swap(int *const a,int *const b) { int x=*a; *a=*b; *b=x; } bool ascending(int x,int y) { return x < y; } bool descending(int p,int q) { return p > q; } |
Code:
#include<iostream> Regards lali.cpp |
ppppppppppppppppppppppppppp
|
All times are GMT -5. The time now is 07:26 AM. |