Linear and Binary Search

Slides:



Advertisements
Similar presentations
Arrays (Continue) Sorting and searching. outlines Sorting – Bubble sort Linear search – min and max Binary search.
Advertisements

Lesson 8 Searching and Sorting Arrays 1CS 1 Lesson 8 -- John Cole.
Ver. 1.0 Session 5 Data Structures and Algorithms Objectives In this session, you will learn to: Sort data by using quick sort Sort data by using merge.
the fourth iteration of this loop is shown here
Simple Sorting Algorithms
Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one
Algorithm Efficiency and Sorting
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 11 Sorting and Searching.
Searching and Sorting Arrays
Value Iteration 0: step 0. Insertion Sort Array index67 Iteration i. Repeatedly swap element i with.
Simple Sorting Algorithms. 2 Outline We are going to look at three simple sorting techniques: Bubble Sort, Selection Sort, and Insertion Sort We are going.
Lecture 5 Searching and Sorting Richard Gesick. The focus Searching - examining the contents of the array to see if an element exists within the array.
Lecture 4 on Data Structure Array. Prepared by, Jesmin Akhter, Lecturer, IIT, JU Searching : Linear search Searching refers to the operation of finding.
Data Strcutures.
CSC 211 Data Structures Lecture 13
Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one
קורס מחשב לרפואנים הרצאה 8: סיבוכיות ומיון ראובן בר-יהודה. © כל הזכויות שמורות לטכניון – מכון טכנולוגי לישראל.
Lecture -3 on Data structures Array. Prepared by, Jesmin Akhter, Lecturer, IIT, JU Array Data structures are classified as either linear or nonlinear.
Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.
Chapter 18: Searching and Sorting Algorithms. Objectives In this chapter, you will: Learn the various search algorithms Implement sequential and binary.
Searching & Sorting Programming 2. Searching Searching is the process of determining if a target item is present in a list of items, and locating it A.
1 Sorting (Bubble Sort, Insertion Sort, Selection Sort)
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
Chapter 9 Sorting. The efficiency of data handling can often be increased if the data are sorted according to some criteria of order. The first step is.
Searching and Sorting Searching: Sequential, Binary Sorting: Selection, Insertion, Shell.
CS1022 Computer Programming & Principles Lecture 2.2 Algorithms.
Computer Science 1620 Sorting. cases exist where we would like our data to be in ascending (descending order) binary searching printing purposes selection.
Sorting & Searching Geletaw S (MSC, MCITP). Objectives At the end of this session the students should be able to: – Design and implement the following.
Sorting and Searching Bubble Sort Linear Search Binary Search.
Chapter 16: Searching, Sorting, and the vector Type.
1 compares each element of the array with the search key. works well for small arrays or for unsorted arrays works for any table slow can put more commonly.
Searching and Sorting Searching algorithms with simple arrays
Sort Algorithm.
Chapter 16: Searching, Sorting, and the vector Type
Chapter 9: Sorting and Searching Arrays
CS212: Data Structures and Algorithms
Sorting Mr. Jacobs.
Sorting.
Lecture 14 Searching and Sorting Richard Gesick.
Introduction to Search Algorithms
Sorting.
Recitation 13 Searching and Sorting.
Simple Sorting Algorithms
Design and Analysis of Algorithms
Searching and Sorting Linear Search Binary Search ; Reading p
Bubble Sort Bubble sort is one way to sort an array of numbers. Adjacent values are swapped until the array is completely sorted. This algorithm gets its.
Binary Search Back in the days when phone numbers weren’t stored in cell phones, you might have actually had to look them up in a phonebook. How did you.
Sorting Algorithms IT12112 Lecture 07.
Selection Sort Sorted Unsorted Swap
Lecture 11 Searching and Sorting Richard Gesick.
Searching and Sorting Arrays
MSIS 655 Advanced Business Applications Programming
Sorting … and Insertion Sort.
Searching and Sorting 1-D Arrays
24 Searching and Sorting.
Lecture 16 Bubble Sort Merge Sort.
Simple Sorting Methods: Bubble, Selection, Insertion, Shell
Sorting "There's nothing in your head the sorting hat can't see. So try me on and I will tell you where you ought to be." -The Sorting Hat, Harry Potter.
Data Structures: Searching
Visit for More Learning Resources
Sorting Example Bubble Sort
Review of Searching and Sorting Algorithms
Simple Sorting Algorithms
Simple Sorting Algorithms
Simple Sorting Algorithms
Insertion Sort Array index Value Insertion sort.
Discrete Mathematics CS 2610
Module 8 – Searching & Sorting Algorithms
Visit for more Learning Resources
Presentation transcript:

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: Write: ITEM is not in the array DATA. Else: 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. [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. 6. 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 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.

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 3 4 5 6 77 42 35 12 101 5 1 2 3 4 5 6 5 12 35 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 pair-wise comparisons and swapping 1 2 3 4 5 6 77 42 35 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 pair-wise comparisons and swapping Swap 1 2 3 4 5 6 42 77 77 42 35 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 pair-wise comparisons and swapping 1 2 3 4 5 6 Swap 35 77 42 77 35 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 pair-wise comparisons and swapping 1 2 3 4 5 6 Swap 12 77 42 35 77 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 pair-wise comparisons and swapping 1 2 3 4 5 6 42 35 12 77 101 5 No need to swap

"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 1 2 3 4 5 6 Swap 5 101 42 35 12 77 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 pair-wise comparisons and swapping 1 2 3 4 5 6 101 42 35 12 77 5 Largest value correctly placed

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 2 3 4 5 6 101 42 35 12 77 5 Largest value correctly placed

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 77 12 35 42 5 1 2 3 4 5 6 101 N - 1 5 42 12 35 77 1 2 3 4 5 6 101 42 5 35 12 77 1 2 3 4 5 6 101 42 35 5 12 77 1 2 3 4 5 6 101 42 35 12 5 77 1 2 3 4 5 6 101

Reducing the Number of Comparisons 12 35 42 77 101 1 2 3 4 5 6 5 77 12 35 42 5 1 2 3 4 5 6 101 5 42 12 35 77 1 2 3 4 5 6 101 42 5 35 12 77 1 2 3 4 5 6 101 42 35 5 12 77 1 2 3 4 5 6 101

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 1 2 3 4 5 6 101

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 – 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) 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] 8 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”! 42 35 12 5 77 1 2 3 4 5 6 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 did_swap true to_do 7 index 98 23 45 14 6 67 33 42 1 2 3 4 5 6 7 8

An Animated Example N 8 did_swap false to_do 7 index 1 98 23 45 14 6 67 33 42 1 2 3 4 5 6 7 8

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

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

An Animated Example N 8 did_swap true to_do 7 index 2 23 98 45 14 6 67 33 42 1 2 3 4 5 6 7 8

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

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

An Animated Example N 8 did_swap true to_do 7 index 3 23 45 98 14 6 67 33 42 1 2 3 4 5 6 7 8

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

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

An Animated Example N 8 did_swap true to_do 7 index 4 23 45 14 98 6 67 33 42 1 2 3 4 5 6 7 8

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

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

An Animated Example N 8 did_swap true to_do 7 index 5 23 45 14 6 98 67 33 42 1 2 3 4 5 6 7 8

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

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

An Animated Example N 8 did_swap true to_do 7 index 6 23 45 14 6 67 98 33 42 1 2 3 4 5 6 7 8

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

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

An Animated Example N 8 did_swap true to_do 7 index 7 23 45 14 6 67 33 98 42 1 2 3 4 5 6 7 8

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

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

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 1 2 3 4 5 6 7 8

The Second “Bubble Up” N 8 did_swap false to_do 6 index 1 23 45 14 6 67 33 42 98 1 2 3 4 5 6 7 8

The Second “Bubble Up” N 8 did_swap false to_do 6 index 1 No Swap 23 45 14 6 67 33 42 98 1 2 3 4 5 6 7 8

The Second “Bubble Up” N 8 did_swap false to_do 6 index 2 23 45 14 6 67 33 42 98 1 2 3 4 5 6 7 8

The Second “Bubble Up” N 8 did_swap false to_do 6 index 2 Swap 23 45 14 6 67 33 42 98 1 2 3 4 5 6 7 8

The Second “Bubble Up” N 8 did_swap true to_do 6 index 2 Swap 23 14 45 67 33 42 98 1 2 3 4 5 6 7 8

The Second “Bubble Up” N 8 did_swap true to_do 6 index 3 23 14 45 6 67 33 42 98 1 2 3 4 5 6 7 8

The Second “Bubble Up” N 8 did_swap true to_do 6 index 3 Swap 23 14 45 67 33 42 98 1 2 3 4 5 6 7 8

The Second “Bubble Up” N 8 did_swap true to_do 6 index 3 Swap 23 14 6 45 67 33 42 98 1 2 3 4 5 6 7 8

The Second “Bubble Up” N 8 did_swap true to_do 6 index 4 23 14 6 45 67 33 42 98 1 2 3 4 5 6 7 8

The Second “Bubble Up” N 8 did_swap true to_do 6 index 4 No Swap 23 14 45 67 33 42 98 1 2 3 4 5 6 7 8

The Second “Bubble Up” N 8 did_swap true to_do 6 index 5 23 14 6 45 67 33 42 98 1 2 3 4 5 6 7 8

The Second “Bubble Up” N 8 did_swap true to_do 6 index 5 Swap 23 14 6 45 67 33 42 98 1 2 3 4 5 6 7 8

The Second “Bubble Up” N 8 did_swap true to_do 6 index 5 Swap 23 14 6 45 33 67 42 98 1 2 3 4 5 6 7 8

The Second “Bubble Up” N 8 did_swap true to_do 6 index 6 23 14 6 45 33 67 42 98 1 2 3 4 5 6 7 8

The Second “Bubble Up” N 8 did_swap true to_do 6 index 6 Swap 23 14 6 45 33 67 42 98 1 2 3 4 5 6 7 8

The Second “Bubble Up” N 8 did_swap true to_do 6 index 6 Swap 23 14 6 45 33 42 67 98 1 2 3 4 5 6 7 8

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 1 2 3 4 5 6 7 8

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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 1 2 3 4 5 6 7 8

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

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

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

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

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

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

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

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

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

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 1 2 3 4 5 6 7 8

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

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

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

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

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

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

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 1 2 3 4 5 6 7 8

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 1 2 3 4 5 6 7 8

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 3 4 6 2 Comparison Data Movement Sorted

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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 + 1 [End of step 4 loop.] if (Min != j), then: swap (a[j], a[Min]). Set j = j+1 [End of step 2 loop.] Return.

Insertion Sort

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.