Presentation is loading. Please wait.

Presentation is loading. Please wait.

© Janice Regan, CMPT 102, Sept. 2006 0 CMPT 102 Introduction to Scientific Computer Programming Applications of Arrays Sorting and Searching.

Similar presentations


Presentation on theme: "© Janice Regan, CMPT 102, Sept. 2006 0 CMPT 102 Introduction to Scientific Computer Programming Applications of Arrays Sorting and Searching."— Presentation transcript:

1 © Janice Regan, CMPT 102, Sept. 2006 0 CMPT 102 Introduction to Scientific Computer Programming Applications of Arrays Sorting and Searching

2 © Janice Regan, CMPT 102, Sept. 2006 1 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 © Janice Regan, CMPT 102, Sept. 2006 2 Ascending / Descending Order 23312197754695378616532 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] 95867546373223191612573 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] 35712161923323746867595 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]

4 © Janice Regan, CMPT 102, Sept. 2006 3 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 © Janice Regan, CMPT 102, Sept. 2006 4 Sample Selection Sort: 1 Unsorted Array A[13] 23312197754695378616532 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] 23312197754695378616532 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 32312197754695378616532 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

6 © Janice Regan, CMPT 102, Sept. 2006 5 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] 323121977546953786165 32 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-1 323121977546953786165 32 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-1 351219775469537861623 32

7 © Janice Regan, CMPT 102, Sept. 2006 6 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] 35121977546953786162332 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-2 35121977546953786162332 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-2 35719127546953786162332

8 © Janice Regan, CMPT 102, Sept. 2006 7 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] 35719127546953786162332 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-3 35719127546953786162332 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] 35712197546953786162332

9 © Janice Regan, CMPT 102, Sept. 2006 8 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] 35712197546953786162332 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-4 35712197546953786162332 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] 35712167546953786192332

10 © Janice Regan, CMPT 102, Sept. 2006 9 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] 35712167546953786192332 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-5 35712167546953786192332 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-5 35712161946953786752332

11 © Janice Regan, CMPT 102, Sept. 2006 10 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] 35712161946953786752332 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-6 35712161946953786752332 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-6 35712161923953786754632

12 © Janice Regan, CMPT 102, Sept. 2006 11 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] 35712161923953786754632 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-7 35712161923953786754632 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-7 35712161923323786754695

13 © Janice Regan, CMPT 102, Sept. 2006 12 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] 35712161923323786754695 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] 35712161923323786754695 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] 35712161923323786754695

14 © Janice Regan, CMPT 102, Sept. 2006 13 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] 35712161923323786754695 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-9 35712161923323786754695 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-9 35712161923323746758695

15 © Janice Regan, CMPT 102, Sept. 2006 14 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] 35712161923323746758695 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-10 357121619233237467586 95 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-10 357121619233237468675 95

16 © Janice Regan, CMPT 102, Sept. 2006 15 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] 35712161923323746867595 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-10 35712161923323746867595 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-10 35712161923323746867595

17 © Janice Regan, CMPT 102, Sept. 2006 16 Selection sort function

18 © Janice Regan, CMPT 102, Sept. 2006 17 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 © Janice Regan, CMPT 102, Sept. 2006 18 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] 23312197754695378616532 Compare A[M-N] and A[M-N+I], put in order Iteration 1: N=M 31219237754695378616532 32312197754695378616532 Compare A[M-N+1] and A[M-N+2], put in order 31223197754695378616532 Compare A[M-N+2] and A[M-N+3], put in order 3121975234695378616532 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 31219752346953786165327

20 © Janice Regan, CMPT 102, Sept. 2006 19 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 31219462375953786165327 31219462375953786165327 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 31219462375379586165327 31219462375378695165327 31219462375378651695327 Compare A[M-N+10] and A[M-N+11], put in order 31219462375378659516327 Compare A[M-N+11] and A[M-N+12], put in order 31219462375378653216957

21 © Janice Regan, CMPT 102, Sept. 2006 20 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] 31219723467537865321695 Compare A[M-N] and A[M-N+I], put in order Iteration 2: N=M-1 31271923467537865321695 31219723467537865321695 Compare A[M-N+1] and A[M-N+2], put in order 31219723467537865321695 Compare A[M-N+2] and A[M-N+3], put in order 312746237537865321695 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 31274623753786532169519

22 © Janice Regan, CMPT 102, Sept. 2006 21 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 31274623753786532169519 31274623377586532169519 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 31274623377586532169519 31274623377558632169519 31274623377551632869519 Compare A[M-N+9] and A[M-N+10], put in order 31274623377551686329519

23 © Janice Regan, CMPT 102, Sept. 2006 22 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] 31271923463775516863295 Compare A[M-N] and A[M-N+I], put in order Iteration 3: N=M-2 37121923463775516863295 31271923463775516863295 Compare A[M-N+1] and A[M-N+2], put in order 37121923463775516863295 Compare A[M-N+2] and A[M-N+3], put in order 371246233775516863295 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 37124623377551686329519

24 © Janice Regan, CMPT 102, Sept. 2006 23 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 37123723467551686329519 37123723467551686329519 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 37123723465751686329519 37123723465167586329519 37123723465163286759519

25 © Janice Regan, CMPT 102, Sept. 2006 24 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 37123723465163286759519 37123723546163286759519 Compare A[M-N+8] and A[M-N+9], put in order 37123723516463286759519 37123723516324686759519 Iteration 4: N=M-3 No changes in first 6 comparisons then continue with

26 © Janice Regan, CMPT 102, Sept. 2006 25 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 37125233716324686759519 37125231637324686759519 37125231632374686759519 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 37123723516324686759519

27 © Janice Regan, CMPT 102, Sept. 2006 26 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 37121652332374686759519 37121652332374686759519 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 37122351632374686759519 37125231632374686759519

28 © Janice Regan, CMPT 102, Sept. 2006 27 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 37121916233237468675955 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 37121916233237468675955 37121619233237468675955 37121951623323746867595

29 © Janice Regan, CMPT 102, Sept. 2006 28 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 37519162332374686759512 37519162332374686759512 375 161923323746867595 37125161923323746867595

30 © Janice Regan, CMPT 102, Sept. 2006 29 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 35719162332374686759512 357 161923323746867595 35712161923323746867595 37512161923323746867595 All numbers are in order: We are done

31 © Janice Regan, CMPT 102, Sept. 2006 30 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 © Janice Regan, CMPT 102, Sept. 2006 31 Sample Quick Sort: 1

33 © Janice Regan, CMPT 102, Sept. 2006 32 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] 95 312327231637867554819 Pivot More than pivot 75312327231637869554819 Pivot More than pivot 75 312327231637869554819 Pivot More than pivotLess than pivot First element smaller than pivot 16312327239537867554819 Pivot Less than pivot More than pivot 75312327231637595864819 Pivot Less than pivot First element larger than pivot First element smaller than pivot

34 © Janice Regan, CMPT 102, Sept. 2006 33 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] 53123272316374895867519 531232723163719 Less than pivot Pivot, middle element 48958675 More than pivot 75312327231637595864819 Pivot Less than pivot First element larger than pivot More than pivot 75312327231637595864819 Pivot Less than pivot First element smaller than pivot More than pivot 75312327231637595864819 Pivot Less than pivot

35 © Janice Regan, CMPT 102, Sept. 2006 34 Sample Quick Sort: 4 First element smaller than pivot

36 © Janice Regan, CMPT 102, Sept. 2006 35 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] 35 23 3212371619 48 958675 123 19 32 37 23167 5 Less than pivot Pivot 48958675 More than pivot 53716373223121948 958675 7 35487163732231219 958675 Pivot, middle element 35487 16 373223 1219 958675 Pivot

37 © Janice Regan, CMPT 102, Sept. 2006 36 Sample Quick Sort: 6

38 © Janice Regan, CMPT 102, Sept. 2006 37 Sample Quick Sort: 7

39 © Janice Regan, CMPT 102, Sept. 2006 38 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] 35487371619322312 958675 Pivot 35487371623321912 958675 35487 32 1619372312 958675 Pivot 35487371623321912 95 86 75 35487371619322312 958675

40 © Janice Regan, CMPT 102, Sept. 2006 39 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 35487371623321912 7595 86 Pivot 35487371623321912 9575 86 Pivot 35487371623321912 7595 86 35487371623321912 7595 86 First element larger than pivot

41 © Janice Regan, CMPT 102, Sept. 2006 40 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 © Janice Regan, CMPT 102, Sept. 2006 41 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 © Janice Regan, CMPT 102, Sept. 2006 42 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 © Janice Regan, CMPT 102, Sept. 2006 43 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); }


Download ppt "© Janice Regan, CMPT 102, Sept. 2006 0 CMPT 102 Introduction to Scientific Computer Programming Applications of Arrays Sorting and Searching."

Similar presentations


Ads by Google