//cocktail sort #include<iostream> using namespace std; void cocktail_sort(int list[],int list_length){ // the first element of list has index 0 int i; int bottom = 0; int top = list_length - 1; bool swapped = true; while(swapped == true) // if no elements have been swapped, then the list is sorted { swapped = false; for(i = bottom; i < top; i = i + 1) { if(list[i] > list[i + 1]) // test whether the two elements are in the correct order { swap(list[i], list[i + 1]); // let the two elements change places swapped = true; } } // decreases top the because the element with the largest value in the unsorted // part of the list is now on the position top top = top - 1; for(i = top; i > bottom; i = i - 1) { if(list[i] < list[i - 1]) { swap(list[i], list[i - 1]); swapped = true; } } // increases bottom because the element with the smallest value in the unsorted // part of the list is now on the position bottom bottom = bottom + 1; } } int main(int argc,char* argv[]) { int i; int list[]={3,2,1,5,6,7,9,0,100}; cocktail_sort(list,9); for(i=0;i<9;i++) cout<<list[i]<<" "; cout<<endl; return 0 ; }