Download presentation
Presentation is loading. Please wait.
Published byGregory May Modified over 9 years ago
1
Selection and Insertion Sort Mrs. C. Furman October 1, 2008
2
Selection Sort Find the smallest one, and swap it to the correct position. For an array of n elements, the array is sorted after n – 1 passes. After the kth pass, the first k elements are in their final sorted positions. Number of comparisons in first pass: n – 1 Number of comparisons in second pass: n – 2 … and so on. Therefore, total number of comparisons = (n -1) + (n – 2) + … + 2 + 1 = n(n-1) / 2 = O(n^2) Irrespective of the initial order of elements, selection sort makes the same number of comparisons. Thus, best case and worst case are the same.
3
Example 1: Map the passes for sorting: 7 3 5 1 9 1 3 5 7 9 – pass 1 1 3 5 7 9 – pass 2 1 3 5 7 9 – pass 3 1 3 5 7 9 – pass 4
4
Insertion Sort Compare to find the smallest, then move all others down and put it into its correct position. For an array of n elements, the array is sorted after n – 1 passes After the kth pass, a[0], a[1], … a[k] are sorted with respect to each other, but not necessarily in their final sorted positions. The worst case for insertion occurs if the array is initially sorted in reverse order, since this will lead to the maximum possible number of comparisons and moves: Number of comparisons in 1 st pass: 1 Number of comparisons in 2 nd pass: 2 …Total number = 1 + 2 + …+ (n – 1) + (n – 2) = n ( n - 1) / 2 = O(n^2)
5
Insertion Sort Cont. The best case for insertion sort occurs if the array is already sorted in increasing order. In this case, each pass through the array will involve just one comparison, which will indicate that “it” is in the right position with respect to the sorted list. Therefore, no elements will be moved. Total comparisons O(n). For the average case, insertion sort must still make n – 1 passes (O(n)). Each pass makes O(n) comparisons, so the total number of comparisons is O(n^2)
6
Example 2 Map the passes for sorting: 7 3 5 1 9 3 7 5 1 9 - 1 st pass 3 5 7 1 9 - 2 nd pass 1 3 5 7 9 - 3 rd pass 1 3 5 7 9 - 4 th pass
7
Example 3 An array of Integer is to be sorted biggest to smallest using the Insertion Sort method. If the array originally contains 1 7 9 5 2 12 What will it look like after the third pass of the for loop?
8
Example 4 When sorted biggest to smallest with Insertion Sort, which list will need the fewest changes of position for individual elements? a. 5, 1, 2, 3, 4, 9 b. 9, 5, 1, 4, 3, 2 c. 9, 4, 2, 5, 1, 3 d. 9, 3, 5, 1, 4, 2 e. 3, 2, 1, 9, 5, 4,
9
Example 5 When sorted biggest to smallest with Insertion Sort, which list will need the greatest number of changes in position? a. 5, 1, 2, 3, 4, 9 b. 9, 5, 1, 4, 3, 2 c. 9, 4, 2, 5, 1, 3 d. 9, 3, 5, 1, 4, 2 e. 3, 2, 1, 9, 5, 4
10
Example 6 If an array of Integer contains the following elements, what would the array look like after the third pass of Selection Sort, sorting from high to low? 8942-313109702
11
Example 7 A worst case situation for Insertion Sort would be I. A list in correct sorted order. II. A list sorted in reverse order. III. A list in random order.
12
Example 8 A certain algorithm sequentially examines a list of n random integers and then outputs the number of times 8 occurs in the list. What is the Big-O of the algorithm?
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.