(15)sorting-quick

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

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

View Full Document Right Arrow Icon
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]
Background image of page 1

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

View Full DocumentRight Arrow Icon
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
Background image of page 2
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; }
Background image of page 3

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

View Full DocumentRight Arrow Icon
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() } }
Background image of page 4
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
Background image of page 5

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

View Full DocumentRight Arrow Icon
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
Background image of page 6
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
Background image of page 7

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

View Full DocumentRight Arrow Icon
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.
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 30

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

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online