{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

(15)sorting-quick

# (15)sorting-quick - SIMPLE Sorting Sorting is a typical...

This preview shows pages 1–9. Sign up to view the full content.

SIMPLE Sorting Sorting is a typical operation to put the elements in an array in order . Internal Sorts [for small data sets] selection bubble (exchange) External Sorts [for large data sets]

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Selection Sort index (k) sm_index 0 2 swap 21, 9 1 1 swap 13, 13 2 3 swap 21, 15 3 4 swap 21, 17 21 15 9 13 17 15 17 9 13 21 9 9 15 21 21 13 17 15 21 21 9 13 17 17 21 21 15 15 9 13 17 Find smallest element, and put at the head of the list,repeat with remainder of list
Selection Sort void sort(double [5]); void swap(double [5], int, int); // prototypes void main(void) { int index; double my_list[ ] = {21, 13, 9, 15, 17}; sort(my_list); // function call cout<<"\nThe sorted array is: \n"; for(index=0; index<5; index++) cout<<'\t'<<my_list[index]<<endl; }

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Selection Sort void sort(double testArray[5]) { int n, k, sm_index, moves=0; double smallest; for(k=0; k<4; k++) // size-1 = number of passes { smallest=testArray[k]; sm_index=k; for(n=k+1; n<5; n++) // size = # elem. to look at if(testArray[n]<smallest) { smallest=testArray[n]; sm_index=n; } swap(testArray, sm_index, k); // call to swap() } }
Bubble Sort Put smaller first Put smaller first No change Put smaller first 21 25 25 13 9 17 17 21 21 25 25 13 9 17 9 9 25 21 21 13 17 9 25 13 21 17

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Bubble Sort Begin again and put smaller first No change Put smaller first 21 17 9 13 25 17 21 21 9 13 25 25 21 21 17 13 13 9 25 21 21 17 17 9 13 25
A Bubble Sort Function void bubble_sort(int array[ ], int length) { int j, k, flag=1, temp; for(j=1; j<=length && flag; j++) { flag=0; // false for(k=0; k < (length-j); k++) { if (array[k+1] > array[k]) // > low to high { temp=array[k+1]; // swap array[k+1]= array[k]; array[k]=temp; flag=1; // indicates a swap } } } } } } } } // has occurred

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Insertion sort: Pick up the cards one at a time. When a card is picked up put it in the “already picked up list” in its correct position.
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}