Presentation is loading. Please wait.

Presentation is loading. Please wait.

Linear and Binary Search

Similar presentations


Presentation on theme: "Linear and Binary Search"— Presentation transcript:

1 Linear and Binary Search

2 Linear Search The sequential search (also called the linear search) is the simplest search algorithm. It is also the least efficient. It simply examines each element sequentially, starting with the first element, until it finds the key element or it reaches the end of the array. Example: If you were looking for someone on a moving passenger train, you would use a sequential search.

3 Algorithm A linear array DATA with N elements and a specific ITEM of information are given. This algorithm finds the location LOC of ITEM in the array DATA or sets LOC = 0. 1. [Initialize] Set K := 1 and LOC := 0. 2. Repeat Steps 3 and 4 while LOC = 0 and K <= N. If ITEM = DATA[K], then: Set LOC := K. Set K := K + 1. [Increments counter.] [End of Step 2 loop.] 5. [Successful?] If Loc = 0, then: Write: ITEM is not in the array DATA. Else: Write: LOC is the location of the ITEM. [End of If structure.] 6. Exit.

4 Performance The sequential search runs in O(n) time.
This means that, on average, the running time is proportional to the number of elements in the array. So if everything else is the same, then applying the sequential search to an array twice as long will take about twice as long, on average. If x is in the sequence, say at x = si with i < n, then the loop will iterate i times. In that case, the running time is proportional to i, which is O(n) since i < n. If x is not in the sequence, then the loop will iterate n times, making the running time proportional to n, which is O(n).

5 The Binary Search Algorithm
The binary search is the standard algorithm for searching through a sorted sequence. It is much more efficient than the sequential search, but it does require that the elements be in order. It repeatedly divides the sequence in two, each time restricting the search to the half that would contain the element. You might use the binary search to look up a word in a dictionary.

6 Algorithm (Binary search) BINARY(DATA, LB, UB, ITEM, LOC)
Here DATA is a sorted array with lower bound LB and upper bound UB, and ITEM is a given item of information. The variables BEG, END and MID denote, resp., the beginning, end, and middle locations of a segment of elements of DATA. The algo finds the location LOC of ITEM in DATA or sets LOC = NULL. [Initialize segment variables.] Set BEG := LB, END := UB and MID := Int((BEG + END) / 2). Repeat steps 3 and 4 while BEG <= END and DATA[MID] != ITEM. 3. If ITEM < DATA[MID], then: Set END := MID – 1. Else: Set BEG := MID + 1. [End of If.] Set MID := INT((BEG + END)/2). [End of Step 2 loop.] If DATA[MID] = ITEM, then: Set LOC := MID. Else: Set LOC := NULL. Exit.

7 Performance The binary search runs in O(lg n) time.
This means that, on average, the running time is proportional to the logarithm of the number of elements in the array. So if it takes an average of T milliseconds to run on an array of n elements, then will take an average of 2T milliseconds to run on an array of n2 elements. For example, if it takes 3 ms to search 10,000 elements, then it should take about 6 ms to search 100,000,000 elements! Each iteration of the loop searches a subarray that is less than half as long as the subarray on the previous iteration. Thus the total number of iterations is no more than the number of times that the length n can be divided by 2. That number is lg n. And the total running time is roughly proportional to the number of iterations that the loop makes.

8 Comparison between Binary and Linear search
Binary search operates on sorted lists. Linear search can operate on unsorted lists as well. Binary search is complex as compared to linear search. Linear search is simple and straightforward to implement than the binary search. Binary search is considered to be a more efficient method that could be used with large lists. But, linear search is too slow to be used with large lists due to its O(n) average case performance. Number of comparisons are less. More number of comparisons are required if the items are present in the later part of the array or its elements are more. Works well with arrays and not on linked lists. Works with arrays and linked lists.

9 Bubble Sort

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

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 77 42 35 12 101 5

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 42 77 77 42 35 12 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 Swap 35 77 42 77 35 12 101 5

14 "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

15 "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

16 "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

17 "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

18 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

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

20 “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

21 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

22 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

23 Algorithm for sorting an array (Bubble sort)
(Bubble sort) BUBBLE (DATA, N) Here DATA is an array with N elements. This algorithm sorts the elements in DATA. 1. Repeat Steps 2 and 3 for K = 1 to N – Set PTR := 1. [Initialize pass pointer PTR.] 3. Repeat while PTR <= (N – K): [Executes pass.] (a) If DATA[PTR] > DATA[PTR + 1], then: Interchange DATA[PTR] and DATA[PTR + 1]. [End of If structure.] (b) Set PTR := PTR + 1. [End of inner loop.] [End of Step 1 outer loop.] 4. Exit.

24 Complexity of Bubble sort
f(n) = (n-1)+(n-2)+ ….+ 2+1=n(n-1)/2=(n^2)/2+O(n)=O(n^2)

25 Another aspect of Bubble sort (Efficient)
Set I:=1 [Initialize pass counter] Set FLAG:=true [Initialize Flag to execute Pass 1] Repeat steps 4 to while I<=N-1 and Flag=true 4 Flag:=false [assume no interchange occur in current pass] 5 Repeat steps 6 for J=1 to N-I-1 [execute pass] 6 If (A[J]>A[J+1]) then a) Interchange A[J] and A[J+1] b) Flag:= true [Indicates next pass will execute] [End of If Structure] [End of Inner Loop] 7 I:=I+1 [Increment pass counter] [End of step3 outer Loop] Return

26 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

27 Using a Boolean “Flag” We can use a boolean variable to determine 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.”

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

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

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

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

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

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

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

35 An Animated Example N 8 did_swap true to_do 7 index 3 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 98 14
6 67 33 42

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

38 An Animated Example N 8 did_swap true to_do 7 index 4 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 98
6 67 33 42

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

41 An Animated Example N 8 did_swap true to_do 7 index 5 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
98 67 33 42

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

44 An Animated Example N 8 did_swap true to_do 7 index 6 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 98 33 42

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

47 An Animated Example N 8 did_swap true to_do 7 index 7 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 98 42

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

50 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

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

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

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

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

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

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

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

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

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

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

61 The Second “Bubble Up” N 8 did_swap true to_do 6 index 5 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 67 33 42 98

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

64 The Second “Bubble Up” N 8 did_swap true to_do 6 index 6 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 67 42 98

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

67 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

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

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

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

71 The Third “Bubble Up” N 8 did_swap true to_do 5 index 2 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 23 6
45 33 42 67 98

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

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

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

76 The Third “Bubble Up” N 8 did_swap true to_do 5 index 4 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
45 33 42 67 98

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

79 The Third “Bubble Up” N 8 did_swap true to_do 5 index 5 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 45 42 67 98

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

82 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

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

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

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

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

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

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

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

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

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

92 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

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

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

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

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

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

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

99 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

100 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

101 Summary “Bubble Up” algorithm will move largest value to its correct location (to the right) Repeat “Bubble Up” until all elements are correctly placed: Maximum of N-1 times Can finish early if no swapping occurs We reduce the number of elements we compare each time one is correctly placed

102 Selection Sort

103 Selection Sort 5 1 3 4 6 2 Comparison Data Movement Sorted

104 Selection Sort 5 1 3 4 6 2 Comparison Data Movement Sorted

105 Selection Sort 5 1 3 4 6 2 Comparison Data Movement Sorted

106 Selection Sort 5 1 3 4 6 2 Comparison Data Movement Sorted

107 Selection Sort 5 1 3 4 6 2 Comparison Data Movement Sorted

108 Selection Sort 5 1 3 4 6 2 Comparison Data Movement Sorted

109 Selection Sort 5 1 3 4 6 2 Comparison Data Movement Sorted

110 Selection Sort 5 1 3 4 6 2 Min Comparison Data Movement Sorted

111 Selection Sort 1 5 3 4 6 2 Comparison Data Movement Sorted

112 Selection Sort 1 5 3 4 6 2 Comparison Data Movement Sorted

113 Selection Sort 1 5 3 4 6 2 Comparison Data Movement Sorted

114 Selection Sort 1 5 3 4 6 2 Comparison Data Movement Sorted

115 Selection Sort 1 5 3 4 6 2 Comparison Data Movement Sorted

116 Selection Sort 1 5 3 4 6 2 Comparison Data Movement Sorted

117 Selection Sort 1 5 3 4 6 2 Comparison Data Movement Sorted

118 Selection Sort 1 5 3 4 6 2 Min Comparison Data Movement Sorted

119 Selection Sort 1 2 3 4 6 5 Comparison Data Movement Sorted

120 Selection Sort 1 2 3 4 6 5 Comparison Data Movement Sorted

121 Selection Sort 1 2 3 4 6 5 Comparison Data Movement Sorted

122 Selection Sort 1 2 3 4 6 5 Comparison Data Movement Sorted

123 Selection Sort 1 2 3 4 6 5 Comparison Data Movement Sorted

124 Selection Sort 1 2 3 4 6 5 Min Comparison Data Movement Sorted

125 Selection Sort 1 2 3 4 6 5 Comparison Data Movement Sorted

126 Selection Sort 1 2 3 4 6 5 Comparison Data Movement Sorted

127 Selection Sort 1 2 3 4 6 5 Comparison Data Movement Sorted

128 Selection Sort 1 2 3 4 6 5 Min Comparison Data Movement Sorted

129 Selection Sort 1 2 3 4 6 5 Comparison Data Movement Sorted

130 Selection Sort 1 2 3 4 6 5 Comparison Data Movement Sorted

131 Selection Sort 1 2 3 4 6 5 Comparison Data Movement Sorted

132 Selection Sort 1 2 3 4 6 5 Min Comparison Data Movement Sorted

133 Selection Sort 1 2 3 4 5 6 Comparison Data Movement Sorted

134 Selection Sort 1 2 3 4 5 6 DONE! Comparison Data Movement Sorted

135 Selection Sort Selection_Sort (A, n) 1. Set j = 1.
2. Repeat Step 2 to 4 while j <= n – 1: Set Min = j and i = j+1. Repeat step 5 while i <= n: if(a[i] < a[Min]), then: Min = i. Set i = i [End of step 4 loop.] if (Min != j), then: swap (a[j], a[Min]). Set j = j+1 [End of step 2 loop.] Return.

136 Insertion Sort

137 11/16/2018 Insertion Sort Iteration i. Repeatedly swap element i with the one to its left if smaller. Property. After ith iteration, a[0] through a[i] contain first i+1 elements in ascending order. 2 3 4 5 1 8 9 Array index 6 7 Value 2.78 7.42 0.56 1.12 1.17 0.32 6.21 4.42 3.14 7.71 Iteration 0: step 0.

138 Insertion Sort 11/16/2018 Iteration i. Repeatedly swap element i with the one to its left if smaller. Property. After ith iteration, a[0] through a[i] contain first i+1 elements in ascending order. 2 3 4 5 1 8 9 Array index 6 7 Value 2.78 7.42 0.56 1.12 1.17 0.32 6.21 4.42 3.14 7.71 Iteration 1: step 0.

139 Insertion Sort 11/16/2018 Iteration i. Repeatedly swap element i with the one to its left if smaller. Property. After ith iteration, a[0] through a[i] contain first i+1 elements in ascending order. 2 3 4 5 1 8 9 Array index 6 7 Value 2.78 7.42 0.56 7.42 0.56 1.12 1.17 0.32 6.21 4.42 3.14 7.71 Iteration 2: step 0.

140 Insertion Sort 11/16/2018 Iteration i. Repeatedly swap element i with the one to its left if smaller. Property. After ith iteration, a[0] through a[i] contain first i+1 elements in ascending order. 2 3 4 5 1 8 9 Array index 6 7 Value 0.56 2.78 2.78 0.56 7.42 1.12 1.17 0.32 6.21 4.42 3.14 7.71 Iteration 2: step 1.

141 Insertion Sort 11/16/2018 Iteration i. Repeatedly swap element i with the one to its left if smaller. Property. After ith iteration, a[0] through a[i] contain first i+1 elements in ascending order. 2 3 4 5 1 8 9 Array index 6 7 Value 0.56 2.78 7.42 1.12 1.17 0.32 6.21 4.42 3.14 7.71 Iteration 2: step 2.

142 Insertion Sort 11/16/2018 Iteration i. Repeatedly swap element i with the one to its left if smaller. Property. After ith iteration, a[0] through a[i] contain first i+1 elements in ascending order. 2 3 4 5 1 8 9 Array index 6 7 Value 0.56 2.78 1.12 7.42 7.42 1.12 1.17 0.32 6.21 4.42 3.14 7.71 Iteration 3: step 0.

143 Insertion Sort 11/16/2018 Iteration i. Repeatedly swap element i with the one to its left if smaller. Property. After ith iteration, a[0] through a[i] contain first i+1 elements in ascending order. 2 3 4 5 1 8 9 Array index 6 7 Value 0.56 2.78 1.12 2.78 1.12 7.42 1.17 0.32 6.21 4.42 3.14 7.71 Iteration 3: step 1.

144 Insertion Sort 11/16/2018 Iteration i. Repeatedly swap element i with the one to its left if smaller. Property. After ith iteration, a[0] through a[i] contain first i+1 elements in ascending order. 2 3 4 5 1 8 9 Array index 6 7 Value 0.56 1.12 2.78 7.42 1.17 0.32 6.21 4.42 3.14 7.71 Iteration 3: step 2.

145 Insertion Sort 11/16/2018 Iteration i. Repeatedly swap element i with the one to its left if smaller. Property. After ith iteration, a[0] through a[i] contain first i+1 elements in ascending order. 2 3 4 5 1 8 9 Array index 6 7 Value 0.56 1.12 2.78 7.42 1.17 7.42 1.17 0.32 6.21 4.42 3.14 7.71 Iteration 4: step 0.

146 Insertion Sort 11/16/2018 Iteration i. Repeatedly swap element i with the one to its left if smaller. Property. After ith iteration, a[0] through a[i] contain first i+1 elements in ascending order. 2 3 4 5 1 8 9 Array index 6 7 Value 0.56 1.12 1.17 2.78 2.78 1.17 7.42 0.32 6.21 4.42 3.14 7.71 Iteration 4: step 1.

147 Insertion Sort 11/16/2018 Iteration i. Repeatedly swap element i with the one to its left if smaller. Property. After ith iteration, a[0] through a[i] contain first i+1 elements in ascending order. 2 3 4 5 1 8 9 Array index 6 7 Value 0.56 1.12 1.17 2.78 7.42 0.32 6.21 4.42 3.14 7.71 Iteration 4: step 2.

148 Insertion Sort 11/16/2018 Iteration i. Repeatedly swap element i with the one to its left if smaller. Property. After ith iteration, a[0] through a[i] contain first i+1 elements in ascending order. 2 3 4 5 1 8 9 Array index 6 7 Value 0.56 1.12 1.17 2.78 7.42 0.32 7.42 0.32 6.21 4.42 3.14 7.71 Iteration 5: step 0.

149 Insertion Sort 11/16/2018 Iteration i. Repeatedly swap element i with the one to its left if smaller. Property. After ith iteration, a[0] through a[i] contain first i+1 elements in ascending order. 2 3 4 5 1 8 9 Array index 6 7 Value 0.56 1.12 1.17 0.32 2.78 2.78 0.32 7.42 6.21 4.42 3.14 7.71 Iteration 5: step 1.

150 Insertion Sort 11/16/2018 Iteration i. Repeatedly swap element i with the one to its left if smaller. Property. After ith iteration, a[0] through a[i] contain first i+1 elements in ascending order. 2 3 4 5 1 8 9 Array index 6 7 Value 0.56 1.12 0.32 1.17 1.17 0.32 2.78 7.42 6.21 4.42 3.14 7.71 Iteration 5: step 2.

151 Insertion Sort 11/16/2018 Iteration i. Repeatedly swap element i with the one to its left if smaller. Property. After ith iteration, a[0] through a[i] contain first i+1 elements in ascending order. 2 3 4 5 1 8 9 Array index 6 7 Value 0.56 0.32 1.12 1.12 0.32 1.17 2.78 7.42 6.21 4.42 3.14 7.71 Iteration 5: step 3.

152 Insertion Sort 11/16/2018 Iteration i. Repeatedly swap element i with the one to its left if smaller. Property. After ith iteration, a[0] through a[i] contain first i+1 elements in ascending order. 2 3 4 5 1 8 9 Array index 6 7 Value 0.32 0.56 0.56 0.32 1.12 1.17 2.78 7.42 6.21 4.42 3.14 7.71 Iteration 5: step 4.

153 Insertion Sort 11/16/2018 Iteration i. Repeatedly swap element i with the one to its left if smaller. Property. After ith iteration, a[0] through a[i] contain first i+1 elements in ascending order. 2 3 4 5 1 8 9 Array index 6 7 Value 0.32 0.56 1.12 1.17 2.78 7.42 6.21 4.42 3.14 7.71 Iteration 5: step 5.

154 Insertion Sort 11/16/2018 Iteration i. Repeatedly swap element i with the one to its left if smaller. Property. After ith iteration, a[0] through a[i] contain first i+1 elements in ascending order. 2 3 4 5 1 8 9 Array index 6 7 Value 0.32 0.56 1.12 1.17 2.78 6.21 7.42 7.42 6.21 4.42 3.14 7.71 Iteration 6: step 0.

155 Insertion Sort 11/16/2018 Iteration i. Repeatedly swap element i with the one to its left if smaller. Property. After ith iteration, a[0] through a[i] contain first i+1 elements in ascending order. 2 3 4 5 1 8 9 Array index 6 7 Value 0.32 0.56 1.12 1.17 2.78 6.21 7.42 4.42 3.14 7.71 Iteration 6: step 1.

156 Insertion Sort 11/16/2018 Iteration i. Repeatedly swap element i with the one to its left if smaller. Property. After ith iteration, a[0] through a[i] contain first i+1 elements in ascending order. 2 3 4 5 1 8 9 Array index 6 7 Value 0.32 0.56 1.12 1.17 2.78 6.21 7.42 4.42 7.42 4.42 3.14 7.71 Iteration 7: step 0.

157 Insertion Sort 11/16/2018 Iteration i. Repeatedly swap element i with the one to its left if smaller. Property. After ith iteration, a[0] through a[i] contain first i+1 elements in ascending order. 2 3 4 5 1 8 9 Array index 6 7 Value 0.32 0.56 1.12 1.17 2.78 4.42 6.21 6.21 4.42 7.42 3.14 7.71 Iteration 7: step 1.

158 Insertion Sort 11/16/2018 Iteration i. Repeatedly swap element i with the one to its left if smaller. Property. After ith iteration, a[0] through a[i] contain first i+1 elements in ascending order. 2 3 4 5 1 8 9 Array index 6 7 Value 0.32 0.56 1.12 1.17 2.78 4.42 6.21 7.42 3.14 7.71 Iteration 7: step 2.

159 Insertion Sort 11/16/2018 Iteration i. Repeatedly swap element i with the one to its left if smaller. Property. After ith iteration, a[0] through a[i] contain first i+1 elements in ascending order. 2 3 4 5 1 8 9 Array index 6 7 Value 0.32 0.56 1.12 1.17 2.78 4.42 6.21 7.42 3.14 7.42 3.14 7.71 Iteration 8: step 0.

160 Insertion Sort 11/16/2018 Iteration i. Repeatedly swap element i with the one to its left if smaller. Property. After ith iteration, a[0] through a[i] contain first i+1 elements in ascending order. 2 3 4 5 1 8 9 Array index 6 7 Value 0.32 0.56 1.12 1.17 2.78 4.42 6.21 3.14 6.21 3.14 7.42 7.71 Iteration 8: step 1.

161 Insertion Sort 11/16/2018 Iteration i. Repeatedly swap element i with the one to its left if smaller. Property. After ith iteration, a[0] through a[i] contain first i+1 elements in ascending order. 2 3 4 5 1 8 9 Array index 6 7 Value 0.32 0.56 1.12 1.17 2.78 3.14 4.42 4.42 3.14 6.21 7.42 7.71 Iteration 8: step 2.

162 Insertion Sort 11/16/2018 Iteration i. Repeatedly swap element i with the one to its left if smaller. Property. After ith iteration, a[0] through a[i] contain first i+1 elements in ascending order. 2 3 4 5 1 8 9 Array index 6 7 Value 0.32 0.56 1.12 1.17 2.78 3.14 4.42 6.21 7.42 7.71 Iteration 8: step 3.

163 Insertion Sort 11/16/2018 Iteration i. Repeatedly swap element i with the one to its left if smaller. Property. After ith iteration, a[0] through a[i] contain first i+1 elements in ascending order. 2 3 4 5 1 8 9 Array index 6 7 Value 0.32 0.56 1.12 1.17 2.78 3.14 4.42 6.21 7.42 7.71 Iteration 9: step 0.

164 Insertion Sort 11/16/2018 Iteration i. Repeatedly swap element i with the one to its left if smaller. Property. After ith iteration, a[0] through a[i] contain first i+1 elements in ascending order. 2 3 4 5 1 8 9 Array index 6 7 Value 0.32 0.56 1.12 1.17 2.78 3.14 4.42 6.21 7.42 7.71 Iteration 10: DONE.

165 Algorithm Insertion_Sort (A, n) Repeat for i = 1 to n: Set j = i
Repeat while j >1 and a[j] < a[j-1]: Set temp = a[j] Set a[j] = a[j-1] Set a[j-1] = temp Set j = j-1 [End of step 3 loop.] [End of step 1 loop.] Return.


Download ppt "Linear and Binary Search"

Similar presentations


Ads by Google