{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Unit07A

# Unit07A - Selection Sort Sorting Quadratic Sorts 7A Let A...

This preview shows pages 1–2. Sign up to view the full content.

1 1 Sorting Quadratic Sorts 7A 2 Selection Sort Let A be an array of n elements, and we wish to sort these elements in non-decreasing order. Basic Algorithm: Set i = 0. While i < n do the following: Find j, i < j < n-1, such that A[j] < A[k], k, i < k < n-1. Swap A[j] with A[i] Add 1 to i This algorithm works in place , meaning it uses its own storage to perform the sort. 3 Selection Sort General Idea: SORTED UNSORTED SORTED UNSORTED j min Loop invariant: A[0..i-1] are sorted in non-decreasing order. A A i i 4 Selection Sort Example 66 44 99 55 11 88 22 77 33 11 44 99 55 66 88 22 77 33 11 22 99 55 66 88 44 77 33 11 22 33 55 66 88 44 77 99 11 22 33 44 66 88 55 77 99 11 22 33 44 55 88 66 77 99 11 22 33 44 55 66 88 77 99 11 22 33 44 55 66 77 88 99 11 22 33 44 55 66 77 88 99 5 Run time analysis Worst Case: Search for 1 st min: n-1 comparisons Search for 2 nd min: n-2 comparisons ... Search for 2 nd -to-last min: 1 comparison Total comparisons: (n-1) + (n-2) + ... + 2 + 1 = O(_________) Average Case: = O(_________) Best Case: = O(_________) 6 Insertion Sort Let A be an array of n elements, and we wish to sort these elements in non-decreasing order.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}