// Interchange sort high to low // assume n[] is 10 big and it contains 10 numbers and is 'int' // compares each element with each one under it // visualize it as 9 sorts on an array that is getting smallerint g,r,c; for ( r=0; r <=8; r++) { for ( g=r+1;g<=9;g++) { if ( n[r] < n[g] ) { c=n[r]; // these 3 statements swap values n[r] = n[g]; // in the 2 cells being compared n[g] = c; } } } // bubble sort on the same array above // compares pairs of elements during a pass // an element can only move up one spot each pass // finished if nothing was swapped or at most n-1 passes int sw,r,c,g; do { sw = 0; for ( r=0; r <=8; r++) { g=r+1; if (n[r] < n[g] ) { c=n[r]; // next 3 statements swap 2 values n[r] = n[g]; n[g] = c; sw=1; } } } while ( sw = 1 } // QUICK SORT is coming soon // Sorting using pointer technique is coming |
Sunday, September 12, 2010
Sorting Arrays
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment