© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Applications of Arrays Sorting and Searching
© Janice Regan, CMPT 102, Sept Putting numbers into order There are many approaches to taking an array of numbers and putting those numbers into ascending or descending order. Insertion Sort (see lab 6) Selection Sort Bubble Sort Quick Sort
© Janice Regan, CMPT 102, Sept Ascending / Descending Order Unsorted Array A[13] A[0]A[1]A{2]A[3]A[4]A[5}A[6]A[7]A[8]A[9]A[11]A[10]A[12] Sorted Array A[13]: Descending order A[0]A[1]A{2]A[3]A[4]A[5}A[6]A[7]A[8]A[9]A[11]A[10]A[12] Sorted Array A[13]: Ascending order A[0]A[1]A{2]A[3]A[4]A[5}A[6]A[7]A[8]A[9]A[11]A[10]A[12]
© Janice Regan, CMPT 102, Sept Selection Sort A method to transform the unsorted array into a sorted array. This example sorts into ascending order, sorting into descending order is a parallel process. Apply a simple algorithm to an array of length M Start with N=M Consider the last N elements in the array, A[M-N] to A[M] Find the element that contains the smallest value in the group of elements defined in (2). Record the index I at which this smallest value is found. Smallest = A[i] Swap the smallest value with the (M-N)th element A[M-N] swaped with A[i] the smallest element Decrease N by 1 and return to step 2 (until N<0)
© Janice Regan, CMPT 102, Sept Sample Selection Sort: 1 Unsorted Array A[13] A[0]A[1]A{2]A[3]A[4]A[5}A[6]A[7]A[8]A[9]A[11]A[10]A[12] A[0]A[1]A{2]A[3]A[4]A[5}A[6]A[7]A[8]A[9]A[11]A[10]A[12] Smallest element is A[1], switch with A[M-N]=A[0] Iteration 1: N=M A[0]A[1]A{2]A[3]A[4]A[5}A[6]A[7]A[8]A[9]A[11]A[10]A[12] After Iteration 1: M=N
© Janice Regan, CMPT 102, Sept Sample Selection Sort: 2 A[0]A[1]A{2]A[3]A[4]A[5}A[6]A[7]A[8]A[9]A[11]A[10]A[12] A[0]A[1]A{2]A[3]A[4]A[5}A[6]A[7]A[8]A[9]A[11]A[10]A[12] Smallest element is A[10], switch with A[M-N]=A[1] Iteration 2: N=M A[0]A[1]A{2]A[3]A[4]A[5}A[6]A[7]A[8]A[9]A[11]A[10]A[12] After Iteration 2: N=M
© Janice Regan, CMPT 102, Sept Sample Selection Sort: 3 A[0]A[1]A{2]A[3]A[4]A[5}A[6]A[7]A[8]A[9]A[11]A[10]A[12] A[0]A[1]A{2]A[3]A[4]A[5}A[6]A[7]A[8]A[9]A[11]A[10]A[12] Smallest element is A[4], switch with A[M-N]=A[2] Iteration 3: N=M A[0]A[1]A{2]A[3]A[4]A[5}A[6]A[7]A[8]A[9]A[11]A[10] A[12] After Iteration 3: N=M
© Janice Regan, CMPT 102, Sept Sample Selection Sort: 4 After Iteration 4: N=M-3 A[0]A[1]A{2]A[3]A[4]A[5}A[6]A[7]A[8]A[9]A[11]A[10]A[12] A[0]A[1]A{2]A[3]A[4]A[5}A[6]A[7]A[8]A[9]A[11]A[10]A[12] Smallest element is A[4], switch with A[M-N]=A[3] Iteration 4: N=M A[0]A[1]A{2]A[3]A[4]A[5}A[6]A[7]A[8]A[9]A[11]A[10] A[12]
© Janice Regan, CMPT 102, Sept Sample Selection Sort: 5 After Iteration 5: N=M-4 A[0]A[1]A{2]A[3]A[4]A[5}A[6]A[7]A[8]A[9]A[11]A[10]A[12] A[0]A[1]A{2]A[3]A[4]A[5}A[6]A[7]A[8]A[9]A[11]A[10]A[12] Smallest element is A[11], switch with A[M-N]=A[4] Iteration 5: N=M A[0]A[1]A{2]A[3]A[4]A[5}A[6]A[7]A[8]A[9]A[11]A[10] A[12]
© Janice Regan, CMPT 102, Sept Sample Selection Sort: 6 A[0]A[1]A{2]A[3]A[4]A[5}A[6]A[7]A[8]A[9]A[11]A[10]A[12] A[0]A[1]A{2]A[3]A[4]A[5}A[6]A[7]A[8]A[9]A[11]A[10]A[12] Smallest element is A[11], switch with A[M-N]=A[5] Iteration 6: N=M A[0]A[1]A{2]A[3]A[4]A[5}A[6]A[7]A[8]A[9]A[11]A[10] A[12] After Iteration 6: N=M
© Janice Regan, CMPT 102, Sept Sample Selection Sort: 7 A[0]A[1]A{2]A[3]A[4]A[5}A[6]A[7]A[8]A[9]A[11]A[10]A[12] A[0]A[1]A{2]A[3]A[4]A[5}A[6]A[7]A[8]A[9]A[11]A[10]A[12] Smallest element is A[10], switch with A[M-N]=A[6] Iteration 7: N=M A[0]A[1]A{2]A[3]A[4]A[5}A[6]A[7]A[8]A[9]A[11]A[10] A[12] After Iteration 7: N=M
© Janice Regan, CMPT 102, Sept Sample Selection Sort: 8 A[0]A[1]A{2]A[3]A[4]A[5}A[6]A[7]A[8]A[9]A[11]A[10]A[12] A[0]A[1]A{2]A[3]A[4]A[5}A[6]A[7]A[8]A[9]A[11]A[10]A[12] Smallest element is A[12], switch with A[M-N]=A[7] Iteration 8: N=M A[0]A[1]A{2]A[3]A[4]A[5}A[6]A[7]A[8]A[9]A[11]A[10] A[12] After Iteration 8: N=M
© Janice Regan, CMPT 102, Sept Sample Selection Sort: 9 Smallest element is A[8], switch with A[M-N]=A[8] Iteration 9: N=M-8 After Iteration 9: N=M-8 A[0]A[1]A{2]A[3]A[4]A[5}A[6]A[7]A[8]A[9]A[11]A[10]A[12] A[0]A[1]A{2]A[3]A[4]A[5}A[6]A[7]A[8]A[9]A[11]A[10]A[12] A[0]A[1]A{2]A[3]A[4]A[5}A[6]A[7]A[8]A[9]A[11]A[10] A[12]
© Janice Regan, CMPT 102, Sept Sample Selection Sort: 10 A[12] A[0]A[1]A{2]A[3]A[4]A[5}A[6]A[7]A[8]A[9]A[11]A[10] A[0]A[1]A{2]A[3]A[4]A[5}A[6]A[7]A[8]A[9]A[11]A[10] Smallest element is A[10], switch with A[M-N]=A[9] Iteration 10: N=M A[0]A[1]A{2]A[3]A[4]A[5}A[6]A[7]A[8]A[9]A[11]A[10]A[12] After Iteration 10: N=M
© Janice Regan, CMPT 102, Sept Sample Selection Sort: 11 A[0]A[1]A{2]A[3]A[4]A[5}A[6]A[7]A[8]A[9]A[11]A[10]A[12] A[0]A[1]A{2]A[3]A[4]A[5}A[6]A[7]A[8]A[9]A[11]A[10]A[12] Smallest element is A[11], switch with A[M-N]=A[10] Iteration 11: N=M A[0]A[1]A{2]A[3]A[4]A[5}A[6]A[7]A[8]A[9]A[11]A[10]A[12] After Iteration 11: N=M
© Janice Regan, CMPT 102, Sept Sample Selection Sort: 12 A[0]A[1]A{2]A[3]A[4]A[5}A[6]A[7]A[8]A[9]A[11]A[10]A[12] A[0]A[1]A{2]A[3]A[4]A[5}A[6]A[7]A[8]A[9]A[11]A[10]A[12] Smallest element is A[12], switch with A[M-N]=A[12] Iteration 12: N=M A[0]A[1]A{2]A[3]A[4]A[5}A[6]A[7]A[8]A[9]A[11]A[10]A[12] After Iteration 13: N=M
© Janice Regan, CMPT 102, Sept Selection sort function
© Janice Regan, CMPT 102, Sept Bubble Sort A method to transform the unsorted array into a sorted array. This example sorts into ascending order, sorting into descending order is a parallel process. Apply a simple algorithm to an array of length M Start with N=0 Consider the array elements A[0] to A[M-1-N] For each I, 0 <= i <M-1-N Compare A[i] and A[i+1] and put in order A[M-1-N] now contains the largest number, it is in order Increase N by 1 and return to step 2 (until N=M-1)
© Janice Regan, CMPT 102, Sept Sample Bubble Sort: 1 Compare A[M-N+5] and A[M-N+6], put in order A[0]A[1]A{2]A[3]A[4]A[5}A[6]A[7]A[8]A[9]A[11]A[10]A[12] Compare A[M-N] and A[M-N+I], put in order Iteration 1: N=M Compare A[M-N+1] and A[M-N+2], put in order Compare A[M-N+2] and A[M-N+3], put in order Compare A[M-N+3] and A[M-N+4], put in order 7 Compare A[M-N+4] and A[M-N+5], put in order
© Janice Regan, CMPT 102, Sept Sample Bubble Sort: 2 A[0]A[1]A{2]A[3]A[4]A[5}A[6]A[7]A[8]A[9]A[11]A[10]A[12] Compare A[M-N+6] and A[M-N+7], put in order Compare A[M-N+7] and A[M-N+8], put in order Compare A[M-N+8] and A[M-N+9], put in order Compare A[M-N+9] and A[M-N+10], put in order Compare A[M-N+10] and A[M-N+11], put in order Compare A[M-N+11] and A[M-N+12], put in order
© Janice Regan, CMPT 102, Sept Sample Bubble Sort: 3 A[0]A[1]A{2]A[3]A[4]A[5}A[6]A[7]A[8]A[9]A[11]A[10]A[12] Compare A[M-N] and A[M-N+I], put in order Iteration 2: N=M Compare A[M-N+1] and A[M-N+2], put in order Compare A[M-N+2] and A[M-N+3], put in order Compare A[M-N+3] and A[M-N+4], put in order 19 Compare A[M-N+4] and A[M-N+5], put in order Compare A[M-N+5] and A[M-N+6], put in order
© Janice Regan, CMPT 102, Sept Sample Bubble Sort: 4 A[0]A[1]A{2]A[3]A[4]A[5}A[6]A[7]A[8]A[9]A[11]A[10]A[12] Compare A[M-N+6] and A[M-N+7], put in order Compare A[M-N+7] and A[M-N+8], put in order Compare A[M-N+8] and A[M-N+9], put in order Compare A[M-N+9] and A[M-N+10], put in order Compare A[M-N+9] and A[M-N+10], put in order
© Janice Regan, CMPT 102, Sept Sample Bubble Sort: 5 A[0]A[1]A{2]A[3]A[4]A[5}A[6]A[7]A[8]A[9]A[11]A[10]A[12] Compare A[M-N] and A[M-N+I], put in order Iteration 3: N=M Compare A[M-N+1] and A[M-N+2], put in order Compare A[M-N+2] and A[M-N+3], put in order Compare A[M-N+3] and A[M-N+4], put in order 19 Compare A[M-N+4] and A[M-N+5], put in order Compare A[M-N+5] and A[M-N+6], put in order
© Janice Regan, CMPT 102, Sept Sample Bubble Sort: 6 A[0]A[1]A{2]A[3]A[4]A[5}A[6]A[7]A[8]A[9]A[11]A[10]A[12] Compare A[M-N+6] and A[M-N+7], put in order Compare A[M-N+7] and A[M-N+8], put in order Compare A[M-N+8] and A[M-N+9], put in order Compare A[M-N+9] and A[M-N+10], put in order
© Janice Regan, CMPT 102, Sept Sample Bubble Sort: 7 A[0]A[1]A{2]A[3]A[4]A[5}A[6]A[7]A[8]A[9]A[11]A[10]A[12] Compare A[M-N+6] and A[M-N+7], put in order Compare A[M-N+7] and A[M-N+8], put in order Compare A[M-N+8] and A[M-N+9], put in order Iteration 4: N=M-3 No changes in first 6 comparisons then continue with
© Janice Regan, CMPT 102, Sept Sample Bubble Sort: 8 A[0]A[1]A{2]A[3]A[4]A[5}A[6]A[7]A[8]A[9]A[11]A[10]A[12] Compare A[M-N+6] and A[M-N+7], put in order Compare A[M-N+7] and A[M-N+8], put in order Iteration 5: N=M-4 No changes in first 5 comparisons then continue with Compare A[M-N+5] and A[M-N+6], put in order
© Janice Regan, CMPT 102, Sept Sample Bubble Sort: 9 A[0]A[1]A{2]A[3]A[4]A[5}A[6]A[7]A[8]A[9]A[11]A[10]A[12] Compare A[M-N+6] and A[M-N+7], put in order Compare A[M-N+4] and A[M-N+5], put in order Iteration 6: N=M-5 No changes in first 4 comparisons then continue with Compare A[M-N+5] and A[M-N+6], put in order
© Janice Regan, CMPT 102, Sept Sample Bubble Sort: 10 A[0]A[1]A{2]A[3]A[4]A[5}A[6]A[7]A[8]A[9]A[11]A[10]A[12] Compare A[M-N+3] and A[M-N+4], put in order Compare A[M-N+4] and A[M-N+5], put in order Iteration 7: N=M-6 No changes in first 3 comparisons then continue with Compare A[M-N+5] and A[M-N+6], put in order
© Janice Regan, CMPT 102, Sept Sample Bubble Sort: 11 A[0]A[1]A{2]A[3]A[4]A[5}A[6]A[7]A[8]A[9]A[11]A[10]A[12] Compare A[M-N+3] and A[M-N+4], put in order Compare A[M-N+4] and A[M-N+5], put in order Iteration 8: N=M-7 No changes in first 3 comparisons then continue with Compare A[M-N+2] and A[M-N+3], put in order
© Janice Regan, CMPT 102, Sept Sample Bubble Sort: 12 A[0]A[1]A{2]A[3]A[4]A[5}A[6]A[7]A[8]A[9]A[11]A[10]A[12] Compare A[M-N+3] and A[M-N+4], put in order Compare A[M-N+1] and A[M-N+2], put in order Iteration 9: N=M-8 No changes in first 3 comparisons then continue with Compare A[M-N+2] and A[M-N+3], put in order All numbers are in order: We are done
© Janice Regan, CMPT 102, Sept Bubble Sort Function void BubbleSort(int InArray[], int M, int N) { int end; int j; int k; int temp; int sorted=FALSE; end = N; while(!sorted) { sorted = TRUE; for( k=M; k<end; k++) { if(InArray[k] > InArray[k+1] ) { sorted = FALSE; temp = InArray[k]; InArray[k] = InArray[k+1]; InArray[k+1] = temp; } } end--; }
© Janice Regan, CMPT 102, Sept Sample Quick Sort: 1
© Janice Regan, CMPT 102, Sept Sample Quick Sort: 2 A[0]A[1]A{2]A[3]A[4]A[5}A[6]A[7]A[8]A[9]A[11]A[10]A[12] Pivot More than pivot Pivot More than pivot Pivot More than pivotLess than pivot First element smaller than pivot Pivot Less than pivot More than pivot Pivot Less than pivot First element larger than pivot First element smaller than pivot
© Janice Regan, CMPT 102, Sept Sample Quick Sort: 3 A[0]A[1]A{2]A[3]A[4]A[5}A[6]A[7]A[8]A[9]A[11]A[10]A[12] Less than pivot Pivot, middle element More than pivot Pivot Less than pivot First element larger than pivot More than pivot Pivot Less than pivot First element smaller than pivot More than pivot Pivot Less than pivot
© Janice Regan, CMPT 102, Sept Sample Quick Sort: 4 First element smaller than pivot
© Janice Regan, CMPT 102, Sept Sample Quick Sort: 5 First element larger than pivot A[0]A[1]A{2]A[3]A[4]A[5}A[6]A[7]A[8]A[9]A[11]A[10]A[12] Less than pivot Pivot More than pivot Pivot, middle element Pivot
© Janice Regan, CMPT 102, Sept Sample Quick Sort: 6
© Janice Regan, CMPT 102, Sept Sample Quick Sort: 7
© Janice Regan, CMPT 102, Sept Sample Quick Sort: 8 A[0]A[1]A{2]A[3]A[4]A[5}A[6]A[7]A[8]A[9]A[11]A[10] A[12] Pivot Pivot
© Janice Regan, CMPT 102, Sept Sample Quick Sort: 9 A[0]A[1]A{2]A[3]A[4]A[5}A[6]A[7]A[8]A[9]A[11]A[10]A[12] Pivot Pivot Pivot First element larger than pivot
© Janice Regan, CMPT 102, Sept Quicksort function: 1 void Quicksort( int InArray[], int loBound, int hiBound ) { int pivot; int loSwap; int hiSwap; int temp; /* Zero or one item to sort */ if (loBound >= hiBound) { return; } /* Two items to sort */ if (hiBound-loBound == 1) { if (InArray[loBound] > InArray[hiBound]) { temp = InArray[loBound]; InArray[loBound] = InArray[hiBound]; InArray[hiBound] = temp; } return; }
© Janice Regan, CMPT 102, Sept Quicksort function: 2 /* 3 or more items to sort */ pivot = InArray[(loBound+hiBound)/2]; InArray[(loBound+hiBound)/2] = InArray[loBound]; InArray[loBound] = pivot; loSwap = loBound + 1; hiSwap = hiBound;
© Janice Regan, CMPT 102, Sept Quicksort function: 3 do { while (loSwap <= hiSwap && InArray[loSwap] <= pivot) { loSwap++; } while (InArray[hiSwap] > pivot) { hiSwap--; } if (loSwap < hiSwap) { temp = InArray[loSwap]; InArray[loSwap] = InArray[hiSwap]; InArray[hiSwap] = temp; } } while (loSwap < hiSwap);
© Janice Regan, CMPT 102, Sept Quicksort function: 4 /* put pivot back in correct position */ InArray[loBound] = InArray[hiSwap]; InArray[hiSwap] = pivot; Quicksort(InArray, loBound, hiSwap-1); Quicksort(InArray, hiSwap+1, hiBound); }