Linear and Binary Search Linear Search The sequential
Linear and Binary Search
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.
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. 3. If ITEM = DATA[K], then: Set LOC : = K. 4. Set K : = K + 1. [Increments counter. ] [End of Step 2 loop. ] 5. [Successful? ] If Loc = 0, then: Else: Write: ITEM is not in the array DATA. Write: LOC is the location of the ITEM. [End of If structure. ] 6. Exit.
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).
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.
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. 1. 2. 3. 4. 5. 6. [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. 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. [End of If. ] Exit.
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 2 T milliseconds to run on an array of n 2 elements. • For example, if it takes 3 ms to search 10, 000 elements, then it should take about 6 ms to search 100, 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.
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.
Bubble Sort
Example • Sorting takes an unordered collection and makes it an ordered one. 1 2 42 77 1 5 3 2 5 4 35 6 101 12 35 3 12 4 5 5 6 42 77 101
"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 pairwise comparisons and swapping 1 77 2 3 42 4 35 5 6 12 101 5
"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 pairwise comparisons and swapping 1 2 7742 Swap 4277 3 4 35 5 6 12 101 5
"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 pairwise comparisons and swapping 1 42 2 3 4 7735 Swap 3577 5 6 12 101 5
"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 pairwise comparisons and swapping 1 42 2 3 35 4 5 77 7712 Swap 12 6 101 5
"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 pairwise comparisons and swapping 1 42 2 3 35 4 12 5 6 77 101 No need to swap 5
"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 pairwise comparisons and swapping 1 42 2 3 35 4 12 5 6 77 1015 Swap 101 5
"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 pairwise comparisons and swapping 1 42 2 3 35 4 12 5 6 77 Largest value correctly placed 5 101
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 1 42 2 3 35 4 12 5 6 77 Largest value correctly placed 5 101
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.
“Bubbling” All the Elements 1 2 42 1 35 2 35 N-1 1 5 3 2 5 5 4 4 4 35 5 101 77 101 6 5 5 6 42 35 3 12 4 6 77 5 3 2 5 42 35 12 1 3 2 4 12 12 12 1 3 5 6 42
Reducing the Number of Comparisons 1 2 77 1 42 2 42 1 12 3 2 35 5 4 4 4 35 101 5 5 101 77 101 6 77 5 6 5 5 6 42 5 3 5 4 6 12 42 3 2 5 12 12 12 1 3 2 4 35 35 35 1 3 5 6 42
Reducing the Number of Comparisons • On the Nth “bubble up”, we only need to do MAX-N comparisons. • For example: – This is the 4 th “bubble up” – MAX is 6 – Thus we have 2 comparisons to do 1 12 2 3 35 4 5 5 6 42 77 101
Algorithm for sorting an array (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 – 1. 2. 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.
Complexity of Bubble sort f(n) = (n-1)+(n-2)+ …. + 2+1=n(n-1)/2=(n^2)/2+O(n)=O(n^2)
Another aspect of Bubble sort (Efficient) 1 2 3 4 5 6 7 8 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 Flag: =false [assume no interchange occur in current pass] Repeat steps 6 for J=1 to N-I-1 [execute pass] 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] I: =I+1 [Increment pass counter] [End of step 3 outer Loop] Return
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”! 1 5 2 3 12 4 35 5 6 42 77 101
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. ”
An Animated Example N 8 to_do 7 true did_swap index 98 1 23 45 14 2 3 4 5 6 6 7 67 8 33 42
An Animated Example N 8 to_do 7 index 1 98 1 false did_swap 23 45 14 2 3 4 5 6 6 7 67 8 33 42
An Animated Example N 8 to_do 7 index 1 false did_swap Swap 98 1 23 45 14 2 3 4 5 6 6 7 67 8 33 42
An Animated Example N 8 to_do 7 index 1 true did_swap Swap 23 1 98 45 14 2 3 4 5 6 6 7 67 8 33 42
An Animated Example N 8 to_do 7 index 2 23 1 true did_swap 98 45 14 2 3 4 5 6 6 7 67 8 33 42
An Animated Example N 8 to_do 7 index 2 true did_swap Swap 23 1 98 45 14 2 3 4 5 6 6 7 67 8 33 42
An Animated Example N 8 to_do 7 index 2 true did_swap Swap 23 1 45 98 14 2 3 4 5 6 6 7 67 8 33 42
An Animated Example N 8 to_do 7 index 3 23 1 true did_swap 45 98 14 2 3 4 5 6 6 7 67 8 33 42
An Animated Example N 8 to_do 7 index 3 true did_swap Swap 23 1 45 98 14 2 3 4 5 6 6 7 67 8 33 42
An Animated Example N 8 to_do 7 index 3 true did_swap Swap 23 1 45 14 98 2 3 4 5 6 6 7 67 8 33 42
An Animated Example N 8 to_do 7 index 4 23 1 true did_swap 45 14 98 2 3 4 5 6 6 7 67 8 33 42
An Animated Example N 8 to_do 7 index 4 true did_swap Swap 23 1 45 14 98 2 3 4 5 6 6 7 67 8 33 42
An Animated Example N 8 to_do 7 index 4 true did_swap Swap 23 1 45 14 6 2 3 4 5 6 98 7 67 8 33 42
An Animated Example N 8 to_do 7 index 5 23 1 true did_swap 45 14 6 2 3 4 5 6 98 7 67 8 33 42
An Animated Example N 8 to_do 7 index 5 true did_swap Swap 23 1 45 14 6 2 3 4 5 6 98 7 67 8 33 42
An Animated Example N 8 to_do 7 index 5 true did_swap Swap 23 1 45 14 6 2 3 4 5 6 67 7 98 8 33 42
An Animated Example N 8 to_do 7 index 6 23 1 true did_swap 45 14 6 2 3 4 5 6 67 7 98 8 33 42
An Animated Example N 8 to_do 7 index 6 true did_swap Swap 23 1 45 14 6 2 3 4 5 6 67 7 98 8 33 42
An Animated Example N 8 to_do 7 index 6 true did_swap Swap 23 1 45 14 6 2 3 4 5 6 67 7 33 8 98 42
An Animated Example N 8 to_do 7 index 7 23 1 true did_swap 45 14 6 2 3 4 5 6 67 7 33 8 98 42
An Animated Example N 8 to_do 7 index 7 true did_swap Swap 23 1 45 14 6 2 3 4 5 6 67 7 33 8 98 42
An Animated Example N 8 to_do 7 index 7 true did_swap Swap 23 1 45 14 6 2 3 4 5 6 67 7 33 8 42 98
After First Pass of Outer Loop N 8 to_do 7 index 8 23 1 true did_swap Finished first “Bubble Up” 45 14 6 2 3 4 5 6 67 7 33 8 42 98
The Second “Bubble Up” N 8 to_do 6 index 1 23 1 false did_swap 45 14 6 2 3 4 5 6 67 7 33 8 42 98
The Second “Bubble Up” N 8 to_do 6 index 1 false did_swap No Swap 23 1 45 14 6 2 3 4 5 6 67 7 33 8 42 98
The Second “Bubble Up” N 8 to_do 6 index 2 23 1 false did_swap 45 14 6 2 3 4 5 6 67 7 33 8 42 98
The Second “Bubble Up” N 8 to_do 6 index 2 false did_swap Swap 23 1 45 14 6 2 3 4 5 6 67 7 33 8 42 98
The Second “Bubble Up” N 8 to_do 6 index 2 true did_swap Swap 23 1 14 45 6 2 3 4 5 6 67 7 33 8 42 98
The Second “Bubble Up” N 8 to_do 6 index 3 23 1 true did_swap 14 45 6 2 3 4 5 6 67 7 33 8 42 98
The Second “Bubble Up” N 8 to_do 6 index 3 true did_swap Swap 23 1 14 45 6 2 3 4 5 6 67 7 33 8 42 98
The Second “Bubble Up” N 8 to_do 6 index 3 true did_swap Swap 23 1 14 6 45 2 3 4 5 6 67 7 33 8 42 98
The Second “Bubble Up” N 8 to_do 6 index 4 23 1 true did_swap 14 6 45 2 3 4 5 6 67 7 33 8 42 98
The Second “Bubble Up” N 8 to_do 6 index 4 true did_swap No Swap 23 1 14 6 45 2 3 4 5 6 67 7 33 8 42 98
The Second “Bubble Up” N 8 to_do 6 index 5 23 1 true did_swap 14 6 45 2 3 4 5 6 67 7 33 8 42 98
The Second “Bubble Up” N 8 to_do 6 index 5 true did_swap Swap 23 1 14 6 45 2 3 4 5 6 67 7 33 8 42 98
The Second “Bubble Up” N 8 to_do 6 index 5 true did_swap Swap 23 1 14 6 45 2 3 4 5 6 33 7 67 8 42 98
The Second “Bubble Up” N 8 to_do 6 index 6 23 1 true did_swap 14 6 45 2 3 4 5 6 33 7 67 8 42 98
The Second “Bubble Up” N 8 to_do 6 index 6 true did_swap Swap 23 1 14 6 45 2 3 4 5 6 33 7 67 8 42 98
The Second “Bubble Up” N 8 to_do 6 index 6 true did_swap Swap 23 1 14 6 45 2 3 4 5 6 33 7 42 8 67 98
After Second Pass of Outer Loop N 8 to_do 6 index 7 23 1 true did_swap Finished second “Bubble Up” 14 6 45 2 3 4 5 6 33 7 42 8 67 98
The Third “Bubble Up” N 8 to_do 5 index 1 23 1 false did_swap 14 6 45 2 3 4 5 6 33 7 42 8 67 98
The Third “Bubble Up” N 8 to_do 5 index 1 false did_swap Swap 23 1 14 6 45 2 3 4 5 6 33 7 42 8 67 98
The Third “Bubble Up” N 8 to_do 5 index 1 true did_swap Swap 14 1 23 6 45 2 3 4 5 6 33 7 42 8 67 98
The Third “Bubble Up” N 8 to_do 5 index 2 14 1 true did_swap 23 6 45 2 3 4 5 6 33 7 42 8 67 98
The Third “Bubble Up” N 8 to_do 5 index 2 true did_swap Swap 14 1 23 6 45 2 3 4 5 6 33 7 42 8 67 98
The Third “Bubble Up” N 8 to_do 5 index 2 true did_swap Swap 14 1 6 23 45 2 3 4 5 6 33 7 42 8 67 98
The Third “Bubble Up” N 8 to_do 5 index 3 14 1 true did_swap 6 23 45 2 3 4 5 6 33 7 42 8 67 98
The Third “Bubble Up” N 8 to_do 5 index 3 true did_swap No Swap 14 1 6 23 45 2 3 4 5 6 33 7 42 8 67 98
The Third “Bubble Up” N 8 to_do 5 index 4 14 1 true did_swap 6 23 45 2 3 4 5 6 33 7 42 8 67 98
The Third “Bubble Up” N 8 to_do 5 index 4 true did_swap Swap 14 1 6 23 45 2 3 4 5 6 33 7 42 8 67 98
The Third “Bubble Up” N 8 to_do 5 index 4 true did_swap Swap 14 1 6 23 33 2 3 4 5 6 45 7 42 8 67 98
The Third “Bubble Up” N 8 to_do 5 index 5 14 1 true did_swap 6 23 33 2 3 4 5 6 45 7 42 8 67 98
The Third “Bubble Up” N 8 to_do 5 index 5 true did_swap Swap 14 1 6 23 33 2 3 4 5 6 45 7 42 8 67 98
The Third “Bubble Up” N 8 to_do 5 index 5 true did_swap Swap 14 1 6 23 33 2 3 4 5 6 42 7 45 8 67 98
After Third Pass of Outer Loop N 8 to_do 5 index 6 14 1 true did_swap Finished third “Bubble Up” 6 23 33 2 3 4 5 6 42 7 45 8 67 98
The Fourth “Bubble Up” N 8 to_do 4 index 1 14 1 false did_swap 6 23 33 2 3 4 5 6 42 7 45 8 67 98
The Fourth “Bubble Up” N 8 to_do 4 index 1 false did_swap Swap 14 1 6 23 33 2 3 4 5 6 42 7 45 8 67 98
The Fourth “Bubble Up” N 8 to_do 4 index 1 true did_swap Swap 6 1 14 23 33 2 3 4 5 6 42 7 45 8 67 98
The Fourth “Bubble Up” N 8 to_do 4 index 2 6 1 true did_swap 14 23 33 2 3 4 5 6 42 7 45 8 67 98
The Fourth “Bubble Up” N 8 to_do 4 index 2 true did_swap No Swap 6 1 14 23 33 2 3 4 5 6 42 7 45 8 67 98
The Fourth “Bubble Up” N 8 to_do 4 index 3 6 1 true did_swap 14 23 33 2 3 4 5 6 42 7 45 8 67 98
The Fourth “Bubble Up” N 8 to_do 4 index 3 true did_swap No Swap 6 1 14 23 33 2 3 4 5 6 42 7 45 8 67 98
The Fourth “Bubble Up” N 8 to_do 4 index 4 6 1 true did_swap 14 23 33 2 3 4 5 6 42 7 45 8 67 98
The Fourth “Bubble Up” N 8 to_do 4 index 4 true did_swap No Swap 6 1 14 23 33 2 3 4 5 6 42 7 45 8 67 98
After Fourth Pass of Outer Loop N 8 to_do 4 index 5 6 1 true did_swap Finished fourth “Bubble Up” 14 23 33 2 3 4 5 6 42 7 45 8 67 98
The Fifth “Bubble Up” N 8 to_do 3 index 1 6 1 false did_swap 14 23 33 2 3 4 5 6 42 7 45 8 67 98
The Fifth “Bubble Up” N 8 to_do 3 index 1 false did_swap No Swap 6 1 14 23 33 2 3 4 5 6 42 7 45 8 67 98
The Fifth “Bubble Up” N 8 to_do 3 index 2 6 1 false did_swap 14 23 33 2 3 4 5 6 42 7 45 8 67 98
The Fifth “Bubble Up” N 8 to_do 3 index 2 false did_swap No Swap 6 1 14 23 33 2 3 4 5 6 42 7 45 8 67 98
The Fifth “Bubble Up” N 8 to_do 3 index 3 6 1 false did_swap 14 23 33 2 3 4 5 6 42 7 45 8 67 98
The Fifth “Bubble Up” N 8 to_do 3 index 3 false did_swap No Swap 6 1 14 23 33 2 3 4 5 6 42 7 45 8 67 98
After Fifth Pass of Outer Loop N 8 to_do 3 index 4 6 1 false did_swap Finished fifth “Bubble Up” 14 23 33 2 3 4 5 6 42 7 45 8 67 98
Finished “Early” N 8 to_do 3 index 4 false did_swap 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. 6 1 14 23 33 2 3 4 5 6 42 7 45 8 67 98
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
Selection Sort
Selection Sort 5 1 Comparison Data Movement Sorted 3 4 6 2
Selection Sort 5 1 Comparison Data Movement Sorted 3 4 6 2
Selection Sort 5 1 Comparison Data Movement Sorted 3 4 6 2
Selection Sort 5 1 Comparison Data Movement Sorted 3 4 6 2
Selection Sort 5 1 Comparison Data Movement Sorted 3 4 6 2
Selection Sort 5 1 Comparison Data Movement Sorted 3 4 6 2
Selection Sort 5 1 Comparison Data Movement Sorted 3 4 6 2
Selection Sort 5 1 Min Comparison Data Movement Sorted 3 4 6 2
Selection Sort 1 5 Comparison Data Movement Sorted 3 4 6 2
Selection Sort 1 5 Comparison Data Movement Sorted 3 4 6 2
Selection Sort 1 5 Comparison Data Movement Sorted 3 4 6 2
Selection Sort 1 5 Comparison Data Movement Sorted 3 4 6 2
Selection Sort 1 5 Comparison Data Movement Sorted 3 4 6 2
Selection Sort 1 5 Comparison Data Movement Sorted 3 4 6 2
Selection Sort 1 5 Comparison Data Movement Sorted 3 4 6 2
Selection Sort 1 5 3 4 6 2 Min Comparison Data Movement Sorted
Selection Sort 1 2 Comparison Data Movement Sorted 3 4 6 5
Selection Sort 1 2 Comparison Data Movement Sorted 3 4 6 5
Selection Sort 1 2 Comparison Data Movement Sorted 3 4 6 5
Selection Sort 1 2 Comparison Data Movement Sorted 3 4 6 5
Selection Sort 1 2 Comparison Data Movement Sorted 3 4 6 5
Selection Sort 1 2 3 Min Comparison Data Movement Sorted 4 6 5
Selection Sort 1 2 Comparison Data Movement Sorted 3 4 6 5
Selection Sort 1 2 Comparison Data Movement Sorted 3 4 6 5
Selection Sort 1 2 Comparison Data Movement Sorted 3 4 6 5
Selection Sort 1 2 3 4 Min Comparison Data Movement Sorted 6 5
Selection Sort 1 2 Comparison Data Movement Sorted 3 4 6 5
Selection Sort 1 2 Comparison Data Movement Sorted 3 4 6 5
Selection Sort 1 2 Comparison Data Movement Sorted 3 4 6 5
Selection Sort 1 2 3 4 6 5 Min Comparison Data Movement Sorted
Selection Sort 1 2 Comparison Data Movement Sorted 3 4 5 6
Selection Sort 1 2 3 DONE! Comparison Data Movement Sorted 4 5 6
Selection Sort Selection_Sort (A, n) 1. Set j = 1. 2. Repeat Step 2 to 4 while j <= n – 1: 3. Set Min = j and i = j+1. 4. Repeat step 5 while i <= n: 5. if(a[i] < a[Min]), then: Min = i. 6. Set i = i + 1 [End of step 4 loop. ] 7. if (Min != j), then: 8. swap (a[j], a[Min]). 9. Set j = j+1 10. [End of step 2 loop. ] 11. Return.
Insertion Sort
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. Array index Value 0 1 2 3 5 6 7 8 9 2. 78 7. 42 0. 56 1. 12 1. 17 0. 32 6. 21 4. 42 3. 14 7. 71 Iteration 0: step 0. 137 4
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. Array index Value 0 1 2 3 5 6 7 8 9 2. 78 7. 42 0. 56 1. 12 1. 17 0. 32 6. 21 4. 42 3. 14 7. 71 Iteration 1: step 0. 138 4
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. Array index Value 0 1 2 3 5 6 7 8 9 2. 78 0. 56 7. 42 0. 56 1. 12 1. 17 0. 32 6. 21 4. 42 3. 14 7. 71 Iteration 2: step 0. 139 4
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. Array index Value 0 1 2 3 5 6 7 8 9 2. 78 0. 56 7. 42 1. 17 0. 32 6. 21 4. 42 3. 14 7. 71 Iteration 2: step 1. 140 4
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. Array index Value 0 1 2 3 5 6 7 8 9 0. 56 2. 78 7. 42 1. 17 0. 32 6. 21 4. 42 3. 14 7. 71 Iteration 2: step 2. 141 4
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. Array index Value 0 1 2 3 5 6 7 8 9 0. 56 2. 78 1. 12 7. 42 1. 17 0. 32 6. 21 4. 42 3. 14 7. 71 Iteration 3: step 0. 142 4
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. Array index Value 0 1 2 3 5 6 7 8 9 0. 56 1. 12 2. 78 1. 12 7. 42 1. 17 0. 32 6. 21 4. 42 3. 14 7. 71 Iteration 3: step 1. 143 4
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. Array index Value 0 1 2 3 5 6 7 8 9 0. 56 1. 12 2. 78 7. 42 1. 17 0. 32 6. 21 4. 42 3. 14 7. 71 Iteration 3: step 2. 144 4
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. Array index Value 0 1 2 3 5 6 7 8 9 0. 56 1. 12 2. 78 1. 17 7. 42 1. 17 0. 32 6. 21 4. 42 3. 14 7. 71 Iteration 4: step 0. 145 4
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. Array index Value 0 1 2 3 5 6 7 8 9 0. 56 1. 12 1. 17 2. 78 1. 17 7. 42 0. 32 6. 21 4. 42 3. 14 7. 71 Iteration 4: step 1. 146 4
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. Array index Value 0 1 2 3 5 6 7 8 9 0. 56 1. 12 1. 17 2. 78 7. 42 0. 32 6. 21 4. 42 3. 14 7. 71 Iteration 4: step 2. 147 4
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. Array index Value 0 1 2 3 5 6 7 8 9 0. 56 1. 12 1. 17 2. 78 0. 32 7. 42 0. 32 6. 21 4. 42 3. 14 7. 71 Iteration 5: step 0. 148 4
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. Array index Value 0 1 2 3 5 6 7 8 9 0. 56 1. 12 1. 17 0. 32 2. 78 0. 32 7. 42 6. 21 4. 42 3. 14 7. 71 Iteration 5: step 1. 149 4
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. Array index Value 0 1 2 3 5 6 7 8 9 0. 56 1. 12 0. 32 1. 17 0. 32 2. 78 7. 42 6. 21 4. 42 3. 14 7. 71 Iteration 5: step 2. 150 4
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. Array index Value 0 1 2 3 5 6 7 8 9 0. 56 0. 32 1. 12 0. 32 1. 17 2. 78 7. 42 6. 21 4. 42 3. 14 7. 71 Iteration 5: step 3. 151 4
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. Array index Value 0 1 2 3 5 6 7 8 9 0. 56 0. 32 1. 12 1. 17 2. 78 7. 42 6. 21 4. 42 3. 14 7. 71 Iteration 5: step 4. 152 4
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. Array index Value 0 1 2 3 5 6 7 8 9 0. 32 0. 56 1. 12 1. 17 2. 78 7. 42 6. 21 4. 42 3. 14 7. 71 Iteration 5: step 5. 153 4
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. Array index Value 0 1 2 3 5 6 7 8 9 0. 32 0. 56 1. 12 1. 17 2. 78 6. 21 7. 42 6. 21 4. 42 3. 14 7. 71 Iteration 6: step 0. 154 4
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. Array index Value 0 1 2 3 5 6 7 8 9 0. 32 0. 56 1. 12 1. 17 2. 78 6. 21 7. 42 4. 42 3. 14 7. 71 Iteration 6: step 1. 155 4
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. Array index Value 0 1 2 3 5 6 7 8 9 0. 32 0. 56 1. 12 1. 17 2. 78 6. 21 4. 42 7. 42 4. 42 3. 14 7. 71 Iteration 7: step 0. 156 4
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. Array index Value 0 1 2 3 5 6 7 8 9 0. 32 0. 56 1. 12 1. 17 2. 78 4. 42 6. 21 4. 42 7. 42 3. 14 7. 71 Iteration 7: step 1. 157 4
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. Array index Value 0 1 2 3 5 6 7 8 9 0. 32 0. 56 1. 12 1. 17 2. 78 4. 42 6. 21 7. 42 3. 14 7. 71 Iteration 7: step 2. 158 4
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. Array index Value 0 1 2 3 5 6 7 8 9 0. 32 0. 56 1. 12 1. 17 2. 78 4. 42 6. 21 3. 14 7. 42 3. 14 7. 71 Iteration 8: step 0. 159 4
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. Array index Value 0 1 2 3 5 6 7 8 9 0. 32 0. 56 1. 12 1. 17 2. 78 4. 42 3. 14 6. 21 3. 14 7. 42 7. 71 Iteration 8: step 1. 160 4
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. Array index Value 0 1 2 3 5 6 7 8 9 0. 32 0. 56 1. 12 1. 17 2. 78 3. 14 4. 42 3. 14 6. 21 7. 42 7. 71 Iteration 8: step 2. 161 4
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. Array index Value 0 1 2 3 5 6 7 8 9 0. 32 0. 56 1. 12 1. 17 2. 78 3. 14 4. 42 6. 21 7. 42 7. 71 Iteration 8: step 3. 162 4
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. Array index Value 0 1 2 3 5 6 7 8 9 0. 32 0. 56 1. 12 1. 17 2. 78 3. 14 4. 42 6. 21 7. 42 7. 71 Iteration 9: step 0. 163 4
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. Array index Value 0 1 2 3 5 6 7 8 9 0. 32 0. 56 1. 12 1. 17 2. 78 3. 14 4. 42 6. 21 7. 42 7. 71 Iteration 10: DONE. 164 4
Algorithm Insertion_Sort (A, n) 1. 2. 3. 4. 5. 6. 7. 8. 9. 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.
- Slides: 165