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
|
#include<iostream>
using namespace std;
double swap (int[ ], int, int);
main(){
int slot[10]={8,3,1,5,7,10,4,9,6,2};
int n=10,i;
int lower, upper, sortflag, sml, scan;
lower=0;
upper=n-1;
sortflag=1;
while((lower<upper)&&(sortflag==1)){
sml=lower;
sortflag=0;
scan=lower+1;
while(scan<=upper-lower){
if(slot[scan]>slot[scan+1]){
swap(slot,scan,scan+1);
sortflag=1;
if(slot[scan]<slot[sml]) sml=scan;
}//IF
scan++;
}//WHILE
swap(slot,lower,sml);
upper=upper-1;
lower=lower+1;
}//WHILE
cout<<"AFTER SORT:"<<endl;
for(i=0;i<n;i++) cout<<slot[i]<<" ";
cout<<endl;
return 0; }//MAIN
double swap(int slot[], int i,int j){
int temp;
temp=slot[i];
slot[i]=slot[j];
slot[j]=temp;
return 0; }//SWAP
| |