1 / 44

CMPT 102 Introduction to Scientific Computer Programming

CMPT 102 Introduction to Scientific Computer Programming. Applications of Arrays Sorting and Searching. 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

Download Presentation

CMPT 102 Introduction to Scientific Computer Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CMPT 102Introduction to Scientific Computer Programming Applications of Arrays Sorting and Searching

  2. 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

  3. Unsorted Array A[13] 95 23 3 3 5 86 7 75 12 19 12 46 16 37 7 32 19 75 46 23 23 19 32 95 37 16 37 46 86 12 75 5 7 86 16 5 32 95 3 A[0] A[0] A[0] A[1] A[1] A[1] A{2] A{2] A{2] A[3] A[3] A[3] A[4] A[4] A[4] A[5} A[5} A[5} A[6] A[6] A[6] A[7] A[7] A[7] A[8] A[8] A[8] A[9] A[9] A[9] A[10] A[10] A[10] A[11] A[11] A[11] A[12] A[12] A[12] Sorted Array A[13]: Descending order Sorted Array A[13]: Ascending order Ascending / Descending Order

  4. 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)

  5. 3 23 23 3 3 23 12 12 12 19 19 19 7 7 7 75 75 75 46 46 46 95 95 95 37 37 37 86 86 86 5 5 5 16 16 16 32 32 32 A[0] A[0] A[0] A[1] A[1] A[1] A{2] A{2] A{2] A[3] A[3] A[3] A[4] A[4] A[4] A[5} A[5} A[5} A[6] A[6] A[6] A[7] A[7] A[7] A[8] A[8] A[8] A[9] A[9] A[9] A[10] A[10] A[10] A[11] A[11] A[11] A[12] A[12] A[12] Iteration 1: N=M Smallest element is A[1], switch with A[M-N]=A[0] After Iteration 1: M=N Sample Selection Sort: 1 Unsorted Array A[13]

  6. 32 32 32 3 3 3 23 23 5 12 12 12 19 19 19 7 7 7 75 75 75 46 46 46 95 95 95 37 37 37 86 86 86 5 5 23 16 16 16 A[0] A[0] A[0] A[1] A[1] A[1] A{2] A{2] A{2] A[3] A[3] A[3] A[4] A[4] A[4] A[5} A[5} A[5} A[6] A[6] A[6] A[7] A[7] A[7] A[8] A[8] A[8] A[9] A[9] A[9] A[10] A[10] A[10] A[11] A[11] A[11] A[12] A[12] A[12] Iteration 2: N=M-1 Smallest element is A[10], switch with A[M-N]=A[1] After Iteration 2: N=M-1 Sample Selection Sort: 2

  7. 3 5 12 19 7 75 46 95 37 86 23 16 32 A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[10] A[11] A[12] Iteration 3: N=M-2 3 5 12 19 7 75 46 95 37 86 23 16 32 A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[10] A[11] A[12] Smallest element is A[4], switch with A[M-N]=A[2] After Iteration 3: N=M-2 3 5 7 19 12 75 46 95 37 86 23 16 32 A[12] A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[10] A[11] Sample Selection Sort: 3

  8. 3 5 7 19 12 75 46 95 37 86 23 16 32 A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[10] A[11] A[12] Iteration 4: N=M-3 3 5 7 19 12 75 46 95 37 86 23 16 32 A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[10] A[11] A[12] Smallest element is A[4], switch with A[M-N]=A[3] 3 5 7 12 19 75 46 95 37 86 23 16 32 A[12] A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[10] A[11] Sample Selection Sort: 4 After Iteration 4: N=M-3

  9. 3 5 7 12 19 75 46 95 37 86 23 16 32 A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[10] A[11] A[12] Iteration 5: N=M-4 3 5 7 12 19 75 46 95 37 86 23 16 32 A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[10] A[11] A[12] Smallest element is A[11], switch with A[M-N]=A[4] 3 5 7 12 16 75 46 95 37 86 23 19 32 A[12] A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[10] A[11] Sample Selection Sort: 5 After Iteration 5: N=M-4

  10. 3 5 7 12 16 75 46 95 37 86 23 19 32 A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[10] A[11] A[12] Iteration 6: N=M-5 3 5 7 12 16 75 46 95 37 86 23 19 32 A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[10] A[11] A[12] Smallest element is A[11], switch with A[M-N]=A[5] After Iteration 6: N=M-5 3 5 7 12 16 19 46 95 37 86 23 75 32 A[12] A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[10] A[11] Sample Selection Sort: 6

  11. 3 5 7 12 16 19 46 95 37 86 23 75 32 A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[10] A[11] A[12] Iteration 7: N=M-6 3 5 7 12 16 19 46 95 37 86 23 75 32 A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[10] A[11] A[12] Smallest element is A[10], switch with A[M-N]=A[6] After Iteration 7: N=M-6 3 5 7 12 16 19 23 95 37 86 46 75 32 A[12] A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[10] A[11] Sample Selection Sort: 7

  12. 3 5 7 12 16 19 23 95 37 86 46 75 32 A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[10] A[11] A[12] Iteration 8: N=M-7 3 5 7 12 16 19 23 95 37 86 46 75 32 A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[10] A[11] A[12] Smallest element is A[12], switch with A[M-N]=A[7] After Iteration 8: N=M-7 3 5 7 12 16 19 23 32 37 86 46 75 95 A[12] A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[10] A[11] Sample Selection Sort: 8

  13. 3 5 7 12 16 19 23 32 37 86 46 75 95 A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[10] A[11] A[12] 3 5 7 12 16 19 23 32 37 86 46 75 95 A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[10] A[11] A[12] 3 5 7 12 16 19 23 32 37 86 46 75 95 A[12] A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[10] A[11] Sample Selection Sort: 9 Iteration 9: N=M-8 Smallest element is A[8], switch with A[M-N]=A[8] After Iteration 9: N=M-8

  14. 3 5 7 12 16 19 23 32 37 86 46 75 95 A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[10] A[11] Iteration 10: N=M-9 3 5 7 12 16 19 23 32 37 86 46 75 95 A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[10] A[11] Smallest element is A[10], switch with A[M-N]=A[9] After Iteration 10: N=M-9 3 5 7 12 16 19 23 32 37 46 86 75 95 A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[10] A[11] A[12] Sample Selection Sort: 10 A[12] A[12]

  15. 3 5 7 12 16 19 23 32 37 46 86 75 95 A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[10] A[11] A[12] Iteration 11: N=M-10 3 5 7 12 16 19 23 32 37 46 86 75 95 A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[10] A[11] A[12] Smallest element is A[11], switch with A[M-N]=A[10] After Iteration 11: N=M-10 3 5 7 12 16 19 23 32 37 46 75 86 95 A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[10] A[11] A[12] Sample Selection Sort: 11

  16. 3 5 7 12 16 19 23 32 37 46 75 86 95 A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[10] A[11] A[12] Iteration 12: N=M-10 3 5 7 12 16 19 23 32 37 46 75 86 95 A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[10] A[11] A[12] Smallest element is A[12], switch with A[M-N]=A[12] After Iteration 13: N=M-10 3 5 7 12 16 19 23 32 37 46 75 86 95 A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[10] A[11] A[12] Sample Selection Sort: 12

  17. Selection sort function

  18. 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)

  19. Sample Bubble Sort: 1 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[10] A[11] A[12] 23 3 12 19 7 75 46 95 37 86 5 16 32 Compare A[M-N] and A[M-N+I], put in order 3 23 12 19 7 75 46 95 37 86 5 16 32 Compare A[M-N+1] and A[M-N+2], put in order 3 12 23 19 7 75 46 95 37 86 5 16 32 Compare A[M-N+2] and A[M-N+3], put in order 3 12 19 23 7 75 46 95 37 86 5 16 32 Compare A[M-N+3] and A[M-N+4], put in order 3 12 19 7 23 75 46 95 37 86 5 16 32 Compare A[M-N+4] and A[M-N+5], put in order 3 12 19 7 23 75 46 95 37 86 5 16 32 Compare A[M-N+5] and A[M-N+6], put in order

  20. 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[10] A[11] A[12] 3 12 19 7 23 46 75 95 37 86 5 16 32 Compare A[M-N+6] and A[M-N+7], put in order 3 12 19 7 23 46 75 95 37 86 5 16 32 Compare A[M-N+7] and A[M-N+8], put in order 3 12 19 7 23 46 75 37 95 86 5 16 32 Compare A[M-N+8] and A[M-N+9], put in order 3 12 19 7 23 46 75 37 86 95 5 16 32 Compare A[M-N+9] and A[M-N+10], put in order 3 12 19 7 23 46 75 37 86 5 95 16 32 Compare A[M-N+10] and A[M-N+11], put in order 3 12 19 7 23 46 75 37 86 5 16 95 32 Compare A[M-N+11] and A[M-N+12], put in order 3 12 19 7 23 46 75 37 86 5 16 32 95

  21. Iteration 2: N=M-1 A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[10] A[11] A[12] 3 12 19 7 23 46 75 37 86 5 16 32 95 Compare A[M-N] and A[M-N+I], put in order 3 12 19 7 23 46 75 37 86 5 16 32 95 Compare A[M-N+1] and A[M-N+2], put in order 3 12 19 7 23 46 75 37 86 5 16 32 95 Compare A[M-N+2] and A[M-N+3], put in order 3 12 7 19 23 46 75 37 86 5 16 32 95 Compare A[M-N+3] and A[M-N+4], put in order 3 12 7 19 23 46 75 37 86 5 16 32 95 Compare A[M-N+4] and A[M-N+5], put in order 3 12 7 19 23 46 75 37 86 5 16 32 95 Compare A[M-N+5] and A[M-N+6], put in order Sample Bubble Sort: 3

  22. 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[10] A[11] A[12] 3 12 7 19 23 46 75 37 86 5 16 32 95 Compare A[M-N+6] and A[M-N+7], put in order 3 12 7 19 23 46 37 75 86 5 16 32 95 Compare A[M-N+7] and A[M-N+8], put in order 3 12 7 19 23 46 37 75 86 5 16 32 95 Compare A[M-N+8] and A[M-N+9], put in order 3 12 7 19 23 46 37 75 5 86 16 32 95 Compare A[M-N+9] and A[M-N+10], put in order 3 12 7 19 23 46 37 75 5 16 86 32 95 Compare A[M-N+9] and A[M-N+10], put in order 3 12 7 19 23 46 37 75 5 16 32 86 95

  23. Sample Bubble Sort: 5 Iteration 3: N=M-2 A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[10] A[11] A[12] 3 12 7 19 23 46 37 75 5 16 32 86 95 Compare A[M-N] and A[M-N+I], put in order 3 12 7 19 23 46 37 75 5 16 32 86 95 Compare A[M-N+1] and A[M-N+2], put in order 3 7 12 19 23 46 37 75 5 16 32 86 95 Compare A[M-N+2] and A[M-N+3], put in order 3 7 12 19 23 46 37 75 5 16 32 86 95 Compare A[M-N+3] and A[M-N+4], put in order 3 7 12 19 23 46 37 75 5 16 32 86 95 Compare A[M-N+4] and A[M-N+5], put in order 3 7 12 19 23 46 37 75 5 16 32 86 95 Compare A[M-N+5] and A[M-N+6], put in order

  24. 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[10] A[11] A[12] 3 7 12 19 23 37 46 75 5 16 32 86 95 Compare A[M-N+6] and A[M-N+7], put in order 3 7 12 19 23 37 46 75 5 16 32 86 95 Compare A[M-N+7] and A[M-N+8], put in order 3 7 12 19 23 37 46 5 75 16 32 86 95 Compare A[M-N+8] and A[M-N+9], put in order 3 7 12 19 23 37 46 5 16 75 32 86 95 Compare A[M-N+9] and A[M-N+10], put in order 3 7 12 19 23 37 46 5 16 32 75 86 95

  25. Sample Bubble Sort: 7 Iteration 4: N=M-3 No changes in first 6 comparisons then continue with A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[10] A[11] A[12] 3 7 12 19 23 37 46 5 16 32 75 86 95 Compare A[M-N+6] and A[M-N+7], put in order 3 7 12 19 23 37 5 46 16 32 75 86 95 Compare A[M-N+7] and A[M-N+8], put in order 3 7 12 19 23 37 5 16 46 32 75 86 95 Compare A[M-N+8] and A[M-N+9], put in order 3 7 12 19 23 37 5 16 32 46 75 86 95

  26. Sample Bubble Sort: 8 Iteration 5: N=M-4 No changes in first 5 comparisons then continue with A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[10] A[11] A[12] 3 7 12 19 23 37 5 16 32 46 75 86 95 Compare A[M-N+5] and A[M-N+6], put in order 3 7 12 19 23 5 37 16 32 46 75 86 95 Compare A[M-N+6] and A[M-N+7], put in order 3 7 12 19 23 5 16 37 32 46 75 86 95 Compare A[M-N+7] and A[M-N+8], put in order 3 7 12 19 23 5 16 32 37 46 75 86 95

  27. Sample Bubble Sort: 9 Iteration 6: N=M-5 No changes in first 4 comparisons then continue with A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[10] A[11] A[12] 3 7 12 19 23 5 16 32 37 46 75 86 95 Compare A[M-N+4] and A[M-N+5], put in order 3 7 12 19 5 23 16 32 37 46 75 86 95 Compare A[M-N+5] and A[M-N+6], put in order 3 7 12 19 5 16 23 32 37 46 75 86 95 Compare A[M-N+6] and A[M-N+7], put in order 3 7 12 19 5 16 23 32 37 46 75 86 95

  28. Sample Bubble Sort: 10 Iteration 7: N=M-6 No changes in first 3 comparisons then continue with A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[10] A[11] A[12] 3 7 12 19 5 16 23 32 37 46 75 86 95 Compare A[M-N+3] and A[M-N+4], put in order 3 7 12 5 19 16 23 32 37 46 75 86 95 Compare A[M-N+4] and A[M-N+5], put in order 3 7 12 5 16 19 23 32 37 46 75 86 95 Compare A[M-N+5] and A[M-N+6], put in order 3 7 12 5 16 19 23 32 37 46 75 86 95

  29. Sample Bubble Sort: 11 Iteration 8: N=M-7 No changes in first 3 comparisons then continue with A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[10] A[11] A[12] 3 7 12 5 16 19 23 32 37 46 75 86 95 Compare A[M-N+2] and A[M-N+3], put in order 3 7 5 12 16 19 23 32 37 46 75 86 95 Compare A[M-N+3] and A[M-N+4], put in order 3 7 5 12 16 19 23 32 37 46 75 86 95 Compare A[M-N+4] and A[M-N+5], put in order 3 7 5 12 16 19 23 32 37 46 75 86 95

  30. Sample Bubble Sort: 12 Iteration 9: N=M-8 No changes in first 3 comparisons then continue with A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[10] A[11] A[12] 3 7 5 12 16 19 23 32 37 46 75 86 95 Compare A[M-N+1] and A[M-N+2], put in order 3 5 7 12 16 19 23 32 37 46 75 86 95 Compare A[M-N+2] and A[M-N+3], put in order 3 5 7 12 16 19 23 32 37 46 75 86 95 Compare A[M-N+3] and A[M-N+4], put in order 3 5 7 12 16 19 23 32 37 46 75 86 95 All numbers are in order: We are done

  31. 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--; } }

  32. A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[10] A[11] A[12] 23 3 12 19 7 75 46 95 37 86 5 16 32 Pivot, middle element Less than pivot 46 32 3 12 19 7 75 23 95 37 86 5 16 Pivot First element larger than pivot Less than pivot 32 3 12 19 7 75 23 95 37 86 5 16 48 Pivot First element smaller than pivot Less than pivot More than pivot 48 3 12 19 7 32 23 95 37 86 5 16 75 Pivot Less than pivot More than pivot 48 3 12 19 7 32 23 95 37 86 5 16 75 Pivot First element larger than pivot Sample Quick Sort: 1

  33. More than pivot Less than pivot 48 3 12 19 7 32 23 95 37 86 5 16 75 Pivot First element smaller than pivot Less than pivot More than pivot 95 48 3 12 19 7 32 23 16 37 86 5 75 Pivot Less than pivot More than pivot 48 3 12 19 7 32 23 16 37 86 5 95 75 Pivot First element larger than pivot More than pivot Less than pivot 75 48 3 12 19 7 32 23 16 37 86 5 95 Pivot First element smaller than pivot Less than pivot More than pivot 48 3 12 19 7 32 23 16 37 5 86 95 75 Pivot 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[10] A[11] A[12]

  34. Less than pivot More than pivot 48 3 12 19 7 32 23 16 37 5 86 95 75 Pivot First element larger than pivot Less than pivot More than pivot 48 3 12 19 7 32 23 16 37 5 86 95 75 Pivot First element smaller than pivot Less than pivot More than pivot 5 3 12 19 7 32 23 16 37 48 86 95 75 Pivot 5 3 12 19 7 32 23 16 37 48 86 95 75 Less than pivot 5 3 12 19 7 32 23 16 37 48 86 95 75 Pivot, middle element 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[10] A[11] A[12]

  35. A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[10] A[11] A[12] Less than pivot 7 5 37 3 12 19 32 23 16 48 86 95 75 Pivot First element larger than pivot Less than pivot 7 3 12 19 5 32 23 16 37 48 86 95 75 Pivot First element smaller than pivot Less than pivot More than pivot 3 5 19 12 32 23 16 7 37 48 86 95 75 Pivot Less than pivot 7 3 5 19 12 32 16 37 23 48 86 95 75 Pivot First element larger than pivot Less than pivot 7 3 5 19 12 32 23 16 37 48 86 95 75 Pivot Sample Quick Sort: 4 First element smaller than pivot

  36. A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[10] A[11] A[12] Less than pivot More than pivot 5 19 3 7 12 32 23 16 37 48 86 95 75 Pivot 5 3 7 19 37 16 32 23 12 48 86 95 75 3 5 7 19 12 32 16 37 23 48 86 95 75 86 95 75 3 5 7 19 37 16 32 23 12 48 Pivot, middle element Pivot 19 12 86 95 75 3 5 7 37 32 23 48 16 First element larger than pivot Sample Quick Sort: 5

  37. A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[10] A[11] A[12] 3 5 7 16 37 19 32 23 12 48 86 95 75 Pivot First element smaller than pivot 86 95 75 3 5 7 16 12 19 32 23 37 48 Pivot 3 5 7 16 12 19 32 23 37 48 86 95 75 Pivot First element larger than pivot More than pivot Pivot 86 95 75 3 5 7 16 12 19 32 23 37 48 First element smaller than pivot More than pivot 12 16 86 95 75 3 5 7 19 32 23 48 37 Pivot Sample Quick Sort: 6

  38. A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[10] A[11] A[12] 86 95 75 3 5 7 12 16 19 32 23 37 48 86 95 75 3 5 7 12 16 19 32 23 37 48 Pivot Pivot 86 95 75 3 5 7 12 16 32 19 23 37 48 Pivot 86 95 75 3 5 7 12 16 32 19 23 37 48 First element larger than pivot 86 95 75 3 5 7 12 16 32 19 23 37 48 First element smaller than pivot Pivot First element smaller than pivot Sample Quick Sort: 7

  39. A[0] A[1] A{2] A[3] A[4] A[5} A[6] A[7] A[8] A[9] A[10] A[11] A[12] 86 95 75 3 5 7 12 16 23 19 37 48 32 Pivot 86 95 75 3 5 7 12 16 23 19 32 37 48 86 95 75 3 5 7 12 16 23 19 32 37 48 86 95 75 3 5 7 12 16 19 23 32 37 48 Pivot Pivot 75 86 3 5 7 12 16 19 23 32 37 48 95 Sample Quick Sort: 8

  40. 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[10] A[11] A[12] Pivot 3 5 7 12 16 19 23 32 37 48 86 95 75 First element larger than pivot 3 5 7 12 16 19 23 32 37 48 86 75 95 Pivot 75 95 3 5 7 12 16 19 23 32 37 48 86 3 5 7 12 16 19 23 32 37 48 86 75 95 Pivot

  41. 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; }

  42. 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;

  43. 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);

  44. 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); }

More Related