Presentation is loading. Please wait.

Presentation is loading. Please wait.

Sorting.

Similar presentations


Presentation on theme: "Sorting."— Presentation transcript:

1 Sorting

2 Types of Sorting Internal Sort Main Memory External Sort Disk
E.g. Bubble sort Selection exchange Shell Sort Insertion Sort Quick Sort etc. External Sort Disk

3 Sorting Stability Repetitive records should sort properly and in same order. E.G. Employee database (Bob, Manager) (John, Worker) (Bob, Team Leader) (Mery, P.A) (Bob, Manager) (Bob, Team Leader) (John, Worker) (Mery, P.A)

4 Efficiency Compare Various Techniques with respect to Time Complexity.
Compare No. of Passes required for sorting

5 Bubble Sort

6 Sorting takes an unordered collection and makes it an ordered one.
77 42 35 12 101 5 5 12 35 42 77 101

7 "Bubbling Up" the Largest Element
Traverse a collection of elements Move from the front to the end “Bubble” the largest value to the end using pair-wise comparisons and swapping 77 42 35 12 101 5

8 "Bubbling Up" the Largest Element
Traverse a collection of elements Move from the front to the end “Bubble” the largest value to the end using pair-wise comparisons and swapping Swap 42 77 77 42 35 12 101 5

9 "Bubbling Up" the Largest Element
Traverse a collection of elements Move from the front to the end “Bubble” the largest value to the end using pair-wise comparisons and swapping Swap 35 77 42 77 35 12 101 5

10 "Bubbling Up" the Largest Element
Traverse a collection of elements Move from the front to the end “Bubble” the largest value to the end using pair-wise comparisons and swapping Swap 12 77 42 35 77 12 101 5

11 "Bubbling Up" the Largest Element
Traverse a collection of elements Move from the front to the end “Bubble” the largest value to the end using pair-wise comparisons and swapping 42 35 12 77 101 5 No need to swap

12 "Bubbling Up" the Largest Element
Traverse a collection of elements Move from the front to the end “Bubble” the largest value to the end using pair-wise comparisons and swapping Swap 5 101 42 35 12 77 101 5

13 "Bubbling Up" the Largest Element
Traverse a collection of elements Move from the front to the end “Bubble” the largest value to the end using pair-wise comparisons and swapping 101 42 35 12 77 5 Largest value correctly placed

14 bubble sort Flag=1 E=N-1 While Flag=1 Exit Flag=0 Loop: j=1 To E E=E-1
If A(j)>A(j+1) Then Swap A(j),A(j+1) Loop end j E=E-1 While End Exit Inner loop Outer loop

15 LB No, Swap isn’t built in. Procedure Swap(a, b) t isoftype Num t <- a a <- b b <- t endprocedure // Swap

16 items of Interest Notice that only the largest value is correctly placed All other values are still out of order So we need to repeat this process 101 42 35 12 77 5 Largest value correctly placed

17 Repeat “Bubble Up” How Many Times?
If we have N elements… And if each time we bubble an element, we place it in its correct location… Then we repeat the “bubble up” process N – 1 times. This guarantees we’ll correctly place all N elements.

18 “Bubbling” All the Elements
77 12 35 42 5 101 N - 1 5 42 12 35 77 101 42 5 35 12 77 101 42 35 5 12 77 101 42 35 12 5 77 101

19 Reducing the Number of Comparisons
12 35 42 77 101 5 77 12 35 42 5 101 5 42 12 35 77 101 42 5 35 12 77 101 42 35 5 12 77 101

20 Reducing the Number of Comparisons
On the Nth “bubble up”, we only need to do MAX-N comparisons. For example: This is the 4th “bubble up” MAX is 6 Thus we have 2 comparisons to do 42 5 35 12 77 101

21 Putting It All Together

22 N is … // Size of Array Arr_Type definesa Array[1
N is … // Size of Array Arr_Type definesa Array[1..N] of Num Procedure Swap(n1, n2 isoftype in/out Num) temp isoftype Num temp <- n1 n1 <- n2 n2 <- temp endprocedure // Swap

23 procedure Bubblesort(A isoftype in/out Arr_Type) to_do, index isoftype Num to_do <- N – 1 loop exitif(to_do = 0) index <- 1 exitif(index > to_do) if(A[index] > A[index + 1]) then Swap(A[index], A[index + 1]) endif index <- index + 1 endloop to_do <- to_do - 1 endprocedure // Bubblesort Inner loop Outer loop

24 Already Sorted Collections?
What if the collection was already sorted? What if only a few elements were out of place and after a couple of “bubble ups,” the collection was sorted? We want to be able to detect this and “stop early”! 42 35 12 5 77 101

25 Using a Boolean “Flag” We can use a boolean variable to deteriteme if any swapping occurred during the “bubble up.” If no swapping occurred, then we know that the collection is already sorted! This boolean “flag” needs to be reset after each “bubble up.”

26 did_swap isoftype Boolean did_swap <- true loop exitif ((to_do = 0) OR NOT(did_swap)) index <- 1 did_swap <- false exitif(index > to_do) if(A[index] > A[index + 1]) then Swap(A[index], A[index + 1]) endif index <- index + 1 endloop to_do <- to_do - 1

27 An Animated Example N 8 did_swap true to_do 7 index 98 23 45 14 6 67
33 42

28 An Animated Example N 8 did_swap false to_do 7 index 1 98 23 45 14 6
67 33 42

29 An Animated Example N 8 did_swap false to_do 7 index 1 Swap 98 23 45
14 6 67 33 42

30 An Animated Example N 8 did_swap true to_do 7 index 1 Swap 23 98 45 14
6 67 33 42

31 An Animated Example N 8 did_swap true to_do 7 index 2 23 98 45 14 6 67
33 42

32 An Animated Example N 8 did_swap true to_do 7 index 2 Swap 23 98 45 14
6 67 33 42

33 An Animated Example N 8 did_swap true to_do 7 index 2 Swap 23 45 98 14
6 67 33 42

34 An Animated Example N 8 did_swap true to_do 7 index 3 23 45 98 14 6 67
33 42

35 An Animated Example N 8 did_swap true to_do 7 index 3 Swap 23 45 98 14
6 67 33 42

36 An Animated Example N 8 did_swap true to_do 7 index 3 Swap 23 45 14 98
6 67 33 42

37 An Animated Example N 8 did_swap true to_do 7 index 4 23 45 14 98 6 67
33 42

38 An Animated Example N 8 did_swap true to_do 7 index 4 Swap 23 45 14 98
6 67 33 42

39 An Animated Example N 8 did_swap true to_do 7 index 4 Swap 23 45 14 6
98 67 33 42

40 An Animated Example N 8 did_swap true to_do 7 index 5 23 45 14 6 98 67
33 42

41 An Animated Example N 8 did_swap true to_do 7 index 5 Swap 23 45 14 6
98 67 33 42

42 An Animated Example N 8 did_swap true to_do 7 index 5 Swap 23 45 14 6
67 98 33 42

43 An Animated Example N 8 did_swap true to_do 7 index 6 23 45 14 6 67 98
33 42

44 An Animated Example N 8 did_swap true to_do 7 index 6 Swap 23 45 14 6
67 98 33 42

45 An Animated Example N 8 did_swap true to_do 7 index 6 Swap 23 45 14 6
67 33 98 42

46 An Animated Example N 8 did_swap true to_do 7 index 7 23 45 14 6 67 33
98 42

47 An Animated Example N 8 did_swap true to_do 7 index 7 Swap 23 45 14 6
67 33 98 42

48 An Animated Example N 8 did_swap true to_do 7 index 7 Swap 23 45 14 6
67 33 42 98

49 After First Pass of Outer Loop
N 8 did_swap true to_do 7 Finished first “Bubble Up” index 8 23 45 14 6 67 33 42 98

50 The Second “Bubble Up” N 8 did_swap false to_do 6 index 1 23 45 14 6
67 33 42 98

51 The Second “Bubble Up” N 8 did_swap false to_do 6 index 1 No Swap 23
45 14 6 67 33 42 98

52 The Second “Bubble Up” N 8 did_swap false to_do 6 index 2 23 45 14 6
67 33 42 98

53 The Second “Bubble Up” N 8 did_swap false to_do 6 index 2 Swap 23 45
14 6 67 33 42 98

54 The Second “Bubble Up” N 8 did_swap true to_do 6 index 2 Swap 23 14 45
67 33 42 98

55 The Second “Bubble Up” N 8 did_swap true to_do 6 index 3 23 14 45 6 67
33 42 98

56 The Second “Bubble Up” N 8 did_swap true to_do 6 index 3 Swap 23 14 45
67 33 42 98

57 The Second “Bubble Up” N 8 did_swap true to_do 6 index 3 Swap 23 14 6
45 67 33 42 98

58 The Second “Bubble Up” N 8 did_swap true to_do 6 index 4 23 14 6 45 67
33 42 98

59 The Second “Bubble Up” N 8 did_swap true to_do 6 index 4 No Swap 23 14
45 67 33 42 98

60 The Second “Bubble Up” N 8 did_swap true to_do 6 index 5 23 14 6 45 67
33 42 98

61 The Second “Bubble Up” N 8 did_swap true to_do 6 index 5 Swap 23 14 6
45 67 33 42 98

62 The Second “Bubble Up” N 8 did_swap true to_do 6 index 5 Swap 23 14 6
45 33 67 42 98

63 The Second “Bubble Up” N 8 did_swap true to_do 6 index 6 23 14 6 45 33
67 42 98

64 The Second “Bubble Up” N 8 did_swap true to_do 6 index 6 Swap 23 14 6
45 33 67 42 98

65 The Second “Bubble Up” N 8 did_swap true to_do 6 index 6 Swap 23 14 6
45 33 42 67 98

66 After Second Pass of Outer Loop
8 did_swap true to_do 6 Finished second “Bubble Up” index 7 23 14 6 45 33 42 67 98

67 The Third “Bubble Up” N 8 did_swap false to_do 5 index 1 23 14 6 45 33
42 67 98

68 The Third “Bubble Up” N 8 did_swap false to_do 5 index 1 Swap 23 14 6
45 33 42 67 98

69 The Third “Bubble Up” N 8 did_swap true to_do 5 index 1 Swap 14 23 6
45 33 42 67 98

70 The Third “Bubble Up” N 8 did_swap true to_do 5 index 2 14 23 6 45 33
42 67 98

71 The Third “Bubble Up” N 8 did_swap true to_do 5 index 2 Swap 14 23 6
45 33 42 67 98

72 The Third “Bubble Up” N 8 did_swap true to_do 5 index 2 Swap 14 6 23
45 33 42 67 98

73 The Third “Bubble Up” N 8 did_swap true to_do 5 index 3 14 6 23 45 33
42 67 98

74 The Third “Bubble Up” N 8 did_swap true to_do 5 index 3 No Swap 14 6
23 45 33 42 67 98

75 The Third “Bubble Up” N 8 did_swap true to_do 5 index 4 14 6 23 45 33
42 67 98

76 The Third “Bubble Up” N 8 did_swap true to_do 5 index 4 Swap 14 6 23
45 33 42 67 98

77 The Third “Bubble Up” N 8 did_swap true to_do 5 index 4 Swap 14 6 23
33 45 42 67 98

78 The Third “Bubble Up” N 8 did_swap true to_do 5 index 5 14 6 23 33 45
42 67 98

79 The Third “Bubble Up” N 8 did_swap true to_do 5 index 5 Swap 14 6 23
33 45 42 67 98

80 The Third “Bubble Up” N 8 did_swap true to_do 5 index 5 Swap 14 6 23
33 42 45 67 98

81 After Third Pass of Outer Loop
N 8 did_swap true to_do 5 Finished third “Bubble Up” index 6 14 6 23 33 42 45 67 98

82 The Fourth “Bubble Up” N 8 did_swap false to_do 4 index 1 14 6 23 33
42 45 67 98

83 The Fourth “Bubble Up” N 8 did_swap false to_do 4 index 1 Swap 14 6 23
33 42 45 67 98

84 The Fourth “Bubble Up” N 8 did_swap true to_do 4 index 1 Swap 6 14 23
33 42 45 67 98

85 The Fourth “Bubble Up” N 8 did_swap true to_do 4 index 2 6 14 23 33 42
45 67 98

86 The Fourth “Bubble Up” N 8 did_swap true to_do 4 index 2 No Swap 6 14
23 33 42 45 67 98

87 The Fourth “Bubble Up” N 8 did_swap true to_do 4 index 3 6 14 23 33 42
45 67 98

88 The Fourth “Bubble Up” N 8 did_swap true to_do 4 index 3 No Swap 6 14
23 33 42 45 67 98

89 The Fourth “Bubble Up” N 8 did_swap true to_do 4 index 4 6 14 23 33 42
45 67 98

90 The Fourth “Bubble Up” N 8 did_swap true to_do 4 index 4 No Swap 6 14
23 33 42 45 67 98

91 After Fourth Pass of Outer Loop
N 8 did_swap true to_do 4 Finished fourth “Bubble Up” index 5 6 14 23 33 42 45 67 98

92 The Fifth “Bubble Up” N 8 did_swap false to_do 3 index 1 6 14 23 33 42
45 67 98

93 The Fifth “Bubble Up” N 8 did_swap false to_do 3 index 1 No Swap 6 14
23 33 42 45 67 98

94 The Fifth “Bubble Up” N 8 did_swap false to_do 3 index 2 6 14 23 33 42
45 67 98

95 The Fifth “Bubble Up” N 8 did_swap false to_do 3 index 2 No Swap 6 14
23 33 42 45 67 98

96 The Fifth “Bubble Up” N 8 did_swap false to_do 3 index 3 6 14 23 33 42
45 67 98

97 The Fifth “Bubble Up” N 8 did_swap false to_do 3 index 3 No Swap 6 14
23 33 42 45 67 98

98 After Fifth Pass of Outer Loop
N 8 did_swap false to_do 3 Finished fifth “Bubble Up” index 4 6 14 23 33 42 45 67 98

99 Finished “Early” N 8 did_swap false to_do 3
We didn’t do any swapping, so all of the other elements must be correctly placed. We can “skip” the last two passes of the outer loop. index 4 6 14 23 33 42 45 67 98

100 Time Complexity Best Case n Average Case  n2 Worst Case n2

101 Insertion Sort

102 Insertion Sort while some elements unsorted:
Using linear search, find the location in the sorted portion where the 1st element of the unsorted portion should be inserted Move all the elements after the insertion location up one position to make space for the new element

103 the fourth iteration of this loop is shown here
Insertion Sort 45 38 60 45 60 66 45 66 79 47 13 74 36 21 94 22 57 16 29 81 the fourth iteration of this loop is shown here

104 item = 60 i j item =60 i j item = 30 i j item = 30 i j item = 20 i j item = 20 j i item = 20 i j

105 Pseudo-code Procedure_Insertion sort(a,n) start Loop: j=1 To n
item=a[j] i=j-1 while((i>=0)&& (item<a[i]) a[i+1]=a[i] i=i-1 End while loop a[i+1]=item Loop End Exit

106 Time complexity Best case: n – Linear Average Case n2
Worst case n(n+1)/2 n2 - Quadratic

107 Selection Sort

108 At start min=a[0]=40 min= 30 i j min = 20 j i min = 60 i j min = 30 i j min = 60 i j min = 40 j i min = 40 i j

109 Pseudo-code Procedure_selection sort(a,n) start Loop: i=0 To n-1
min =i; Loop: j=1 To n If a[min]>a[j] Then min=j Inner loop of j End if a[i]>a[min] then Swap(a[i],a[min]) Outer Loop of I End Exit

110 Time Complexity_ Selection
Best Case n2 Average Case n2 Worst Case n2

111 Mergesort

112 Divide and Conquer Divide and Conquer cuts the problem in half each time, but uses the result of both halves: cut the problem in half until the problem is trivial solve for both halves combine the solutions

113 Merging The key to Merge Sort is merging two sorted lists into one, such that if you have two lists X (x1x2…xm) and Y(y1y2…yn) the resulting list is Z(z1z2…zm+n) Example: L1 = { } L2 = { } merge(L1, L2) = { }………………Donald E. Knuth (page 160).

114 Merging (cont.) X: 3 10 23 54 Y: 1 5 25 75 Result:

115 Merging (cont.) X: 3 10 23 54 Y: 5 25 75 Result: 1

116 Merging (cont.) X: 10 23 54 Y: 5 25 75 Result: 1 3

117 Merging (cont.) X: 10 23 54 Y: 25 75 Result: 1 3 5

118 Merging (cont.) X: 23 54 Y: 25 75 Result: 1 3 5 10

119 Merging (cont.) X: 54 Y: 25 75 Result: 1 3 5 10 23

120 Merging (cont.) X: 54 Y: 75 Result: 1 3 5 10 23 25

121 Merging (cont.) X: Y: 75 Result: 1 3 5 10 23 25 54

122 Merging (cont.) X: Y: Result: 1 3 5 10 23 25 54 75

123 Merge sort -A divide-and-conquer algorithm:
Divide the unsorted array into 2 halves until the sub-arrays only contain one element Merge the sub-problem solutions together: Compare the sub-array’s first elements Remove the smallest element and put it into the result array Continue the process until all elements have been put into the result array

124 98 23 45 14 6 67 33 42

125 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42

126 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14

127 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 98 23

128 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 98 23 Merge

129 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 98 23 23 Merge

130 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 98 23 23 98 Merge

131 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 98 23 45 14 23 98

132 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 98 23 45 14 23 98 Merge

133 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 98 23 45 14 23 98 14 Merge

134 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 98 23 45 14 23 98 14 45 Merge

135 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 98 23 45 14 23 98 14 45 Merge

136 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 98 23 45 14 23 98 14 45 14 Merge

137 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 98 23 45 14 23 98 14 45 14 23 Merge

138 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 98 23 45 14 23 98 14 45 14 23 45 Merge

139 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 98 23 45 14 23 98 14 45 14 23 45 98 Merge

140 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 23 98 14 45 14 23 45 98

141 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 23 98 14 45 14 23 45 98

142 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 23 98 14 45 14 23 45 98 Merge

143 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 23 98 14 45 6 14 23 45 98 Merge

144 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 23 98 14 45 6 67 14 23 45 98 Merge

145 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 23 98 14 45 6 67 14 23 45 98

146 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 23 98 14 45 6 67 14 23 45 98 Merge

147 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 23 98 14 45 6 67 33 14 23 45 98 Merge

148 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 23 98 14 45 6 67 33 42 14 23 45 98 Merge

149 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 23 98 14 45 6 67 33 42 14 23 45 98 Merge

150 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 23 98 14 45 6 67 33 42 14 23 45 98 6 Merge

151 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 23 98 14 45 6 67 33 42 14 23 45 98 6 33 Merge

152 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 23 98 14 45 6 67 33 42 14 23 45 98 6 33 42 Merge

153 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 23 98 14 45 6 67 33 42 14 23 45 98 6 33 42 67 Merge

154 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 23 98 14 45 6 67 33 42 14 23 45 98 6 33 42 67 Merge

155 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 23 98 14 45 6 67 33 42 14 23 45 98 6 33 42 67 6 Merge

156 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 23 98 14 45 6 67 33 42 14 23 45 98 6 33 42 67 6 14 Merge

157 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 23 98 14 45 6 67 33 42 14 23 45 98 6 33 42 67 6 14 23 Merge

158 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 23 98 14 45 6 67 33 42 14 23 45 98 6 33 42 67 6 14 23 33 Merge

159 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 23 98 14 45 6 67 33 42 14 23 45 98 6 33 42 67 6 14 23 33 42 Merge

160 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 23 98 14 45 6 67 33 42 14 23 45 98 6 33 42 67 6 14 23 33 42 45 Merge

161 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 23 98 14 45 6 67 33 42 14 23 45 98 6 33 42 67 6 14 23 33 42 45 67 Merge

162 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 23 98 14 45 6 67 33 42 14 23 45 98 6 33 42 67 6 14 23 33 42 45 67 98 Merge

163 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 23 98 14 45 6 67 33 42 14 23 45 98 6 33 42 67 6 14 23 33 42 45 67 98

164 98 23 45 14 6 67 33 42 6 14 23 33 42 45 67 98

165 Algorithm Mergesort(Passed an array) if array size > 1 Divide array in half Call Mergesort on first half. Call Mergesort on second half. Merge two halves. Merge(Passed two arrays) Compare leading element in each array Select lower and place in new array. (If one input array is empty then place remainder of other array in output array)

166 Pseudo code Mergesort(A,low,high) If (low<high)then { Mid:= (low+high)/2 //split list in 2 Mergesort(A,low,mid) //1st sublist Mergesort(A,mid+1,high) //2nd sublist Merge(A,low,mid,high) // merging of 2 list }

167 Merge(A[0…n-1], low, mid, high) k=low i=low j= mid+1 Loop: while(i<=mid &&j<=high) do if(A[i]<=A[j]) then temp[k]=A[i] increment i increment k else temp[k]=A[j] increment j while loop ends

168 //copy remaining elements of left part(sublist 1) while(i<=mid)d0
temp[k]=A[i] increment i Increment k Loop ends //copy remaining elements of right part(sublist 2) while(j<=high)do temp[k]=A[j] increment j increment k

169 Summary Divide the unsorted collection into two
Until the sub-arrays only contain one element Then merge the sub-problem solutions together Best Case, Average Case, and Worst Case = O(N logN)


Download ppt "Sorting."

Similar presentations


Ads by Google