Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Algorithms CSCI 235, Fall 2015 Lecture 11 Elementary Sorts.

Similar presentations


Presentation on theme: "1 Algorithms CSCI 235, Fall 2015 Lecture 11 Elementary Sorts."— Presentation transcript:

1 1 Algorithms CSCI 235, Fall 2015 Lecture 11 Elementary Sorts

2 2 Sorting algorithms The problem: Determine the sorted order of a number of elements by comparing them to one another. Given 1)an array A[1... n] of values 2)A comparison predicate, less(v1, v2) that determines v1<v2 Then Modify A so that lesseq(A[i], A[i+1]) for 0 < i < n (lesseq determines whether A[i] <= A[i+1]) Measure the running time of this comparison based sorting by the number of less operations performed.

3 3 Notes on Sorting A[i... j] is a contiguous segment of A between indices i and j, inclusive. If i > j then A[i... j] is empty. The array is usually an array of records with a key field to be sorted. The satellite data in the record is sorted along with the key. FredMaryJaneKey could be name or number. 2 1 2 To test whether v1 = v2 we can use the functions eq(v1, v2) and lesseq(v1, v2). eq(a, b) return not(less(a, b)) and not(less(b,a)) lesseq(a,b) return less(a, b) or eq(a, b)

4 4 More notes In some contexts it is helpful to assume all the elements in the array are distinct. In some cases we may want to measure the number of assignments to arrays and to temporary variables. We may want to model the temporary space required (i.e. memory used). An algorithm is in place if the amount of temporary space is constant (does not change as n increases). A sorting algorithm is stable if it preserves the relative positions of sorted elements. 2 1 2 3 4 a b c d e 1 2 2 3 4 b a c d e Alphabetical order maintained Sort by numbers

5 5 A few more notes Array based sorting algorithms can be adapted to lists. A function that swaps two elements is useful: swap(A, i, j) temp  A[i] A[i]  A[j] A[j]  temp

6 6 Insertion Sort Idea: Sort items one at a time into previously sorted group. Invariant: After the ith step of the algorithm, A[1..i] is sorted.

7 7 Insertion sort algorithm Insertion-Sort(A) for i  2 to length[A] do Insert(A, i) Insert(A, i) key  A[i] j  i while j > 1 and less(key, A[j-1]) do A[j]  A[j-1] j  j-1 {Insert key} A[j]  key

8 8 Is it Stable and in place? Stepping through the insertion sort algorithm: a b c d 2 1 1 2 3 4 A Index: Example: Array already sorted alphabetically. Sort by numbers as key. If the algorithm is stable, the letters associated with the same number will stay in order relative to one another. We will step through the algorithm in class.

9 9 Running time Already showed that worst case running time is  (n 2 ) This is true for both the recursive version and for the iterative version (as derived earlier in class). Insertion-Sort(A) for i  2 to length[A] do Insert(A, i) Array size to be solved decreases by 1 each time through loop. T(n) = T(n-1) + cost of Insert Insert(A, i) key  A[i] j  i while j > 1 and less(key, A[j-1]) do A[j]  A[j-1] j  j-1 {Insert key} A[j]  key Array size to be solved decreases by 1 each time through loop. T 2 (n) = T 2 (n-1) + cost of assignments = T 2 (n-1) + 1 T 2 (n) =  (n) T(n) = T(n-1) + n T(n) =  (n 2 )

10 10 Selection Sort Idea: On the ith step of the algorithm, store into A[i] the smallest element of A[i..n]. Invariant: After step i, the elements A[1..i] are in their final sorted positions.

11 11 Selection sort algorithm Selection-Sort(A) for i  1 to length[A] - 1 do swap(A, i, Min-Index(A, i, length[A])) Min-Index(A, lo, hi) {Assume lo and hi are legal subscripts, and hi >= lo.} min_index  lo for i  lo + 1 to hi do if less(A[i], A[min_index]) then min-index  i return min_index Is Selection sort stable? Is it in place? We will work through an example in class.

12 12 Running time Selection-Sort(A) for i  1 to length[A] - 1 do swap(A, i, Min-Index(A, i, length[A])) Min-Index(A, lo, hi) {Assume lo and hi are legal subscripts, and hi >= lo.} min_index  lo for i  lo + 1 to hi do if less(A[i], A[min_index]) then min-index  i return min_index T(n) = T(n-1) + cost of Min-Index T 2 (n) = T 2 (n-1) + cost of assignments and less( ) = T 2 (n-1) + 1 T 2 (n) =  (n) T(n) = T(n-1) + n T(n) =  (n 2 )


Download ppt "1 Algorithms CSCI 235, Fall 2015 Lecture 11 Elementary Sorts."

Similar presentations


Ads by Google