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

Info iconThis preview shows page 1. Sign up to view the full content.

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

Unformatted text preview: Selection Sort Sorting Quadratic Sorts 7A 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 1 This algorithm works in place, meaning it uses its own storage to perform the sort. 2 Selection Sort Selection Sort Example i UNSORTED General Idea: SORTED j min A i A SORTED UNSORTED Loop invariant: A[0..i-1] are sorted in non-decreasing order. 3 66 11 11 11 11 11 11 11 11 44 44 22 22 22 22 22 22 22 99 99 99 33 33 33 33 33 33 55 55 55 55 44 44 44 44 44 11 66 66 66 66 55 55 55 55 88 88 88 88 88 88 66 66 66 22 22 44 44 55 66 88 77 77 77 77 77 77 77 77 77 88 88 33 33 33 99 99 99 99 99 99 4 Run time analysis Insertion Sort Worst Case: Search for 1st min: Search for 2nd min: ... Search for 2nd-to-last min: Total comparisons: (n-1) + (n-2) + ... + 2 + 1 Average Case: Best Case: n-1 comparisons n-2 comparisons 1 comparison = O(_________) = O(_________) = O(_________) 5 Let A be an array of n elements, and we wish to sort these elements in non-decreasing order. Basic Algorithm: Set i = 1 While i < n do the following: Set item = A[i]. Shift A[j] to A[j+1], ∀j, j < i, where A[j] > item. Let k be the smallest j above, or k=i if no shifts. Set A[k] = item. Add 1 to i. This algorithm also works in place. 6 1 Insertion Sort Insertion Sort Example i UNSORTED 66 44 44 44 11 11 11 11 11 44 66 66 55 44 44 22 22 22 99 99 99 66 55 55 44 44 33 55 55 55 99 66 66 55 55 44 11 11 11 11 99 88 66 66 55 88 88 88 88 88 99 88 77 66 22 22 22 22 22 22 99 88 77 77 77 77 77 77 77 77 99 88 33 33 33 33 33 33 33 33 99 8 General Idea: k SORTED insert i SORTED UNSORTED Loop invariant: A[0..i-1] are sorted in non-decreasing order. 7 Run time analysis Stable Sorts Worst Case (when does this occur?): Insert 2nd element: 1 comparison Insert 3rd element: 2 comparisons ... Insert last element: n-1 comparisons Total comparisons: 1 + 2 + ... + (n-1) = O(_________) = O(_________) = O(_________) 9 A sort is stable if two elements with the same value maintain their same relative order before and after the sort is performed. x After stable sort: x x x Average Case: Best Case: Is selection sort stable? Is insertion sort stable? 10 Quadratic Sorts Tree Sort 24 84 41 50 96 98 Quadratic sorts have a worst-case order of complexity of O(n2) Selection sort always performs poorly, even on a sequence of sorted elements! Insertion sort performs much better if the elements are sorted or nearly sorted. Another famous quadratic sort: “Bubble sort” 11 Build a binary search 13 37 tree out of the elements. Traverse the tree using an inorder traversal to get the elements in increasing order. Worst case order of complexity: O(n2) to build the binary search tree (Why?) O(n) to traverse the binary tree. (Why?) Total: O(n2) + O(n) = _______ 12 2 ...
View Full Document

This note was uploaded on 12/19/2009 for the course CS 121 taught by Professor Reid-miller during the Spring '09 term at Carnegie Mellon.

Ask a homework question - tutors are online