selection and insertion sort mrs. c. furman october 1, 2008

12
Selection and Insertion Sort Mrs. C. Furman October 1, 2008

Upload: gregory-may

Post on 17-Dec-2015

217 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Selection and Insertion Sort Mrs. C. Furman October 1, 2008

Selection and Insertion Sort

Mrs. C. Furman

October 1, 2008

Page 2: Selection and Insertion Sort Mrs. C. Furman October 1, 2008

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 – 1Number 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.

Page 3: Selection and Insertion Sort Mrs. C. Furman October 1, 2008

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

Page 4: Selection and Insertion Sort Mrs. C. Furman October 1, 2008

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 1st pass: 1Number of comparisons in 2nd pass: 2…Total number = 1 + 2 + …+ (n – 1) + (n – 2) = n ( n - 1) / 2 =

O(n^2)

Page 5: Selection and Insertion Sort Mrs. C. Furman October 1, 2008

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)

Page 6: Selection and Insertion Sort Mrs. C. Furman October 1, 2008

Example 2

Map the passes for sorting:

7 3 5 1 9

3 7 5 1 9 - 1st pass

3 5 7 1 9 - 2nd pass

1 3 5 7 9 - 3rd pass

1 3 5 7 9 - 4th pass

Page 7: Selection and Insertion Sort Mrs. C. Furman October 1, 2008

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?

Page 8: Selection and Insertion Sort Mrs. C. Furman October 1, 2008

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, 9b. 9, 5, 1, 4, 3, 2c. 9, 4, 2, 5, 1, 3d. 9, 3, 5, 1, 4, 2e. 3, 2, 1, 9, 5, 4,

Page 9: Selection and Insertion Sort Mrs. C. Furman October 1, 2008

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

Page 10: Selection and Insertion Sort Mrs. C. Furman October 1, 2008

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?

89 42 -3 13 109 70 2

Page 11: Selection and Insertion Sort Mrs. C. Furman October 1, 2008

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.

Page 12: Selection and Insertion Sort Mrs. C. Furman October 1, 2008

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?