Presentation is loading. Please wait.

Presentation is loading. Please wait.

QuickSort QuickSort is often called Partition Sort. It is a recursive method, in which the unsorted array is first rearranged so that there is some record,

Similar presentations


Presentation on theme: "QuickSort QuickSort is often called Partition Sort. It is a recursive method, in which the unsorted array is first rearranged so that there is some record,"— Presentation transcript:

1 QuickSort QuickSort is often called Partition Sort. It is a recursive method, in which the unsorted array is first rearranged so that there is some record, somewhere in the middle of the array, whose key is greater than all the keys to its left & less than or equal to all the keys to its right. Once this “middle is found, the same method can be used to sort the section of the array to the left, then sort the section to the right.

2 QuickSort Algorithm 1. If First < Last then 2. Partition the elements in the array (First, Last) so that the pivot value is in place ( position PivIndex). 3. Apply QuickSort to the first subarray (First, PivIndex -1) 4. Apply QuickSort to the second subarray (PivIndex + 1, Last). The two stopping Cases are: 1. (First = Last) - only one value in subarray, so sorted. 2. (First > Last) - no values in subarray, so sorted.

3 How do we Partition? 1. Define the pivot value as the content of Table[First] 2. Initialize Up to First and Down to last 3. Repeat 4. Increment Up until Up selects the first element greater than the pivot value 5. Decrement Down until it selects the first element less than or equal to the pivot value. 6. If Up < Down exchange their values. Until Up meets or passes Down. 7. Exchange Table[First] and Table[Down] 8. Define PivIndex as Down

4 QuickSort Example 447523643312437755 012 3456 78 First Last Has First exceeded Last? No! Pivot 44 Define the value in position First to be the Pivot.

5 QuickSort Example 447523643312437755 012 3456 78 FirstLast Pivot 44 Define Up To be First and Down to be last Up Down

6 QuickSort Example 447523643312437755 012 3456 78 FirstLast Pivot 44 Move Up to the first value > Pivot Up Down

7 QuickSort Example 447523643312437755 012 3456 78 FirstLast Pivot 44 Move Down to the first value <= Pivot Up Down

8 QuickSort Example 443323647512437755 012 3456 78 FirstLast Pivot 44 If Up < Down, exchange their values Up Down

9 QuickSort Example 443323647512437755 012 3456 78 FirstLast Pivot 44 Move Up to the first value > Pivot Up Down

10 QuickSort Example 443323647512437755 012 3456 78 FirstLast Pivot 44 Move Down to the first value <= Pivot UpDown

11 QuickSort Example 443323647555437712 012 3456 78 FirstLast Pivot 44 If Up < Down, exchange their values. UpDown

12 QuickSort Example 443323647555437712 012 3456 78 FirstLast Pivot 44 Move Up to the first value > Pivot UpDown

13 QuickSort Example 443323647555437712 012 3456 78 FirstLast Pivot 44 Move Down to the first value <= Pivot UpDown Up and Down have passed each other, so exchange the pivot value and the value in Down.

14 QuickSort Example 123323647555437744 012 3456 78 FirstLast Pivot 44 UpDown Up and Down have passed each other, so exchange the pivot value and the value in Down.

15 QuickSort Example 123323647555437744 012 3456 78 FirstLast Pivot 44 PivIndex Note that all values below PivIndex are <= Pivot and all values above PivIndex are > Pivot.

16 QuickSort Example 123323647555437744 012 3456 78 First 1 Last 2 Pivot 44 PivIndex This gives us two new subarrays to Partition Last 1 First 2

17 QuickSort Procedure Code void QuickSort(int Table[], int First, int Last) { int PivIndex; if (First < Last) { PivIndex = Partition(Table, First, Last); QuickSort(Table, First, PivIndex - 1); QuickSort(Table, PivIndex + 1, Last); }

18 Heap Sort How can a tree be represented in an array? 012 3456 12 57 19 87 154423

19 Heap Sort How can a tree be represented in an array? 12 012 3456 57 19 87 154423 Place the root of the tree in element 0 of the array (RootPos = 0).

20 Heap Sort How can a tree be represented in an array? 125719 012 3456 12 57 19 87 154423 Place the root of the tree in element 0 of the array (RootPos = 0). Place the root’s left child in element 1 : (RootPos*2 + 1) = 1 Place the root’s right child in element 2: (RootPos*2 + 2) = 2

21 Heap Sort How can a tree be represented in an array? 1257198715 012 3456 12 57 19 87 154423 Place the root of the tree in element 0 of the array (RootPos = 0). Place the root’s left child in element 1 : (RootPos*2 + 1) Place the root’s right child in element 2: (RootPos*2 + 2) The Children of 57 are placed in: Left Child (87): ParentPos*2 +1 = 1* 2 + 1 = 3 Right Child (15): ParentPos*2 + 2 = 1 *2 + 2 = 4

22 Heap Sort How can a tree be represented in an array? 1257198715 012 3456 12 57 19 87 154423 Place the root of the tree in element 0 of the array (RootPos = 0). Place the root’s left child in element 1 : (RootPos*2 + 1) Place the root’s right child in element 2: (RootPos*2 + 2) The Children of 19 are placed in: Left Child (44): ParentPos*2 +1 = 2* 2 + 1 = 5 Right Child (15): ParentPos*2 + 2 = 2 *2 + 2 = 6 4423

23 Heap Sort To perform the heap sort we must: 1. Create a heap (a tree with all nodes greater than their children) 2. Remove the root element from the heap one at a time, recomposing the heap.

24 Building the Heap 1. For each value in the array(0, n) 2. Place the value into the “tree” 3. Bubble the value as high as it can go (push the largest values to highest position)

25 Heap Sort How to build a heap? 1257198715 012 3456 12 Add Table[0] to tree Since it has no parent, we have a heap. 4423

26 Heap Sort How to build a heap? 1257198715 012 3456 12 Add Table[1] to tree Since 12 < 57, it is not a heap. Bubble it up as high as it can go. Exchange 4423 57

27 Heap Sort How to build a heap? 5712198715 012 3456 57 Exchange Since 57 >12 57 is as high as it can go, so we have a heap. 4423 12

28 Heap Sort How to build a heap? 5712198715 012 3456 57 Add Table[2] to tree Since 57 >19 so, we have a heap. 4423 1219

29 Heap Sort How to build a heap? 5712198715 012 3456 57 Add Table[3] to tree Since 87 >12 so, not a heap. 4423 1219 87

30 Heap Sort How to build a heap? 5787191215 012 3456 57 Add Table[3] to tree Since 87 >12 so, not a heap. Bubble it up. Exchange. Again 87 > 57, so not a heap. Bubble it up 4423 8719 12

31 Heap Sort How to build a heap? 8757191215 012 3456 87 Again 87 > 57, so not a heap. Bubble it up. Exchange. We now have a heap again. 4423 5719 12

32 Heap Sort How to build a heap? 8757191215 012 3456 87 Add Table[4] to tree 15 > 57, so a heap. 4423 5719 12 15

33 Heap Sort How to build a heap? 8757191215 012 3456 87 Add Table[5] to tree 44 > 19, so not a heap. 4423 5719 12 15 44

34 Heap Sort How to build a heap? 8757441215 012 3456 87 44 > 19, so not a heap. Bubble it up. Exchange. 44<87 Again we have a heap. 1923 5744 12 15 19

35 Heap Sort How to build a heap? 8757441215 012 3456 87 Add Table[6] to tree 23 <44 so, we have a heap. 1923 5744 12 15 1923

36 Heap Sort How to build a heap? 8757441215 012 3456 87 The whole table is now a heap! 23 5744 12 15 1923 19

37 Heap Sort Algorithm 1. Repeat n -1 times 2. Exchange the root value with the last value in the tree 3. “Drop” the last value from the tree 4. Reform the heap 5. Start at the root node of the current tree 6. If the root is larger than its children, stop- you have a heap. 7. If not, exchange the root with the largest child. 8. Consider this child to be the current root and repeat step 4

38 Heap Sort 8757441215 012 3456 87 Here is the heap! 1923 5744 12 15 1923

39 Heap Sort 8757441215 012 3456 87 Exchange the root with the last value in the tree 1923 5744 12 15 1923

40 Heap Sort 2357441215 012 3456 23 Exchange the root with the last value in the tree 1987 5744 12 15 1987

41 Heap Sort 2357441215 012 3456 23 Drop this last value from the tree -- it is now in the array in its sorted position! 1987 5744 12 15 19 87

42 Heap Sort 2357441215 012 3456 23 Drop this last value from the tree -- it is now in the array in its sorted position! 1987 5744 12 15 19 The sorted list The tree

43 Heap Sort 2357441215 012 3456 23 Reform the heap 1987 5744 12 15 19 The sorted list The tree

44 Heap Sort 2357441215 012 3456 23 Find the largest child of the current “root” and exchange with “root” 1987 5744 12 15 19 The sorted list The tree

45 Heap Sort 5723441215 012 3456 57 Now 23 is larger than both of its children so we have a heap again. 1987 2344 12 15 19 The sorted list The tree

46 Heap Sort 5723441215 012 3456 57 Exchange the root of the heap with the last value in the tree. 1987 2344 12 15 19 The sorted list The tree

47 Heap Sort 1923441215 012 3456 19 Exchange the root of the heap with the last value in the tree. 5787 2344 12 15 57 The sorted list The tree

48 Heap Sort 1923441215 012 3456 19 Drop the last element from the tree since the value is now in its sorted position. 5787 2344 12 15 57 The sorted list The tree

49 Heap Sort 1923441215 012 3456 19 Drop the last element from the tree since the value is now in its sorted position. 5787 2344 12 15 The sorted list The tree

50 Heap Sort 1923441215 012 3456 19 Reform the heap 5787 2344 12 15 The sorted list The tree

51 Heap Sort 1923441215 012 3456 19 Find the largest child of the current “root”. Exchange the values. 5787 2344 12 15 The sorted list The tree

52 Heap Sort 4423191215 012 3456 44 Since 19 has no children, we now have a heap again. 5787 2319 12 15 The sorted list The tree

53 Heap Sort 4423191215 012 3456 44 Swap the root with the last position in the tree. 5787 2319 12 15 The sorted list The tree

54 Heap Sort 1523191244 012 3456 15 Drop the last value from the tree. 5787 2319 12 The sorted list The tree 44

55 Heap Sort 1523191244 012 3456 15 Drop the last value from the tree. 5787 2319 12 The sorted list The tree

56 Heap Sort 1523191244 012 3456 15 Reform the heap by exchanging the root with its largest child 5787 2319 12 The sorted list The tree

57 Heap Sort 2315191244 012 3456 23 Reform the heap by exchanging the root with its largest child 5787 1519 12 The sorted list The tree

58 Heap Sort 2315191244 012 3456 23 Exchange the root with the last value in the tree 5787 1519 12 The sorted list The tree

59 Heap Sort 1215192344 012 3456 12 Exchange the root with the last value in the tree 5787 1519 23 The sorted list The tree

60 Heap Sort 1215192344 012 3456 12 Drop the last value from the tree 5787 1519 23 The sorted list The tree

61 Heap Sort 1215192344 012 3456 12 Drop the last value from the tree 5787 1519 The sorted list The tree

62 Heap Sort 1215192344 012 3456 12 Reform the heap 5787 1519 The sorted list The tree

63 Heap Sort 1215192344 012 3456 12 Exchange the root with the largest child 5787 1519 The sorted list The tree

64 Heap Sort 1915122344 012 3456 19 We have a heap 5787 1512 The sorted list The tree

65 Heap Sort 1915122344 012 3456 19 Exchange the root with the last position in the tree 5787 1512 The sorted list The tree

66 Heap Sort 1215192344 012 3456 12 Exchange the root with the last position in the tree 5787 15 19 The sorted list The tree

67 Heap Sort 1215192344 012 3456 12 Drop the last value from the tree 5787 15 19 The sorted list The tree

68 Heap Sort 1215192344 012 3456 12 Drop the last value from the tree 5787 15 The sorted list The tree

69 Heap Sort 1215192344 012 3456 12 Reform the heap 5787 15 The sorted list The tree

70 Heap Sort 1215192344 012 3456 12 Exchange the root with the largest child 5787 15 The sorted list The tree

71 Heap Sort 1512192344 012 3456 15 We have a heap 5787 12 The sorted list The tree

72 Heap Sort 1512192344 012 3456 15 Exchange the root with the last value in the tree 5787 12 The sorted list The tree

73 Heap Sort 1215192344 012 3456 12 Exchange the root with the last value in the tree 5787 15 The sorted list The tree

74 Heap Sort 1215192344 012 3456 12 Drop the last value from the tree 5787 15 The sorted list The tree

75 Heap Sort 1215192344 012 3456 12 Drop the last value from the tree 5787 The sorted list The tree

76 Heap Sort 1215192344 012 3456 12 The tree consists of a single value at this point which is in its proper place within the sorted list. 5787 The sorted list The tree

77 Heap Sort 1215192344 012 3456 The tree consists of a single value at this point which is in its proper place within the sorted list. The array is sorted. 5787 The sorted list The tree


Download ppt "QuickSort QuickSort is often called Partition Sort. It is a recursive method, in which the unsorted array is first rearranged so that there is some record,"

Similar presentations


Ads by Google