Transcript Lecture 11
Algorithms
CSCI 235, Fall 2015
Lecture 11
Elementary Sorts
1
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.
2
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.
Fred
2
Mary Jane
1
2
Key could be name or number.
• 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)
3
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 Sort by numbers
1 2 2 3 4
b a c d e
Alphabetical order maintained
4
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
5
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.
6
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
7
Is it Stable and in place?
Stepping through the insertion sort algorithm:
A a b c d
2 1 2 1
Index: 1 2 3 4
•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.
8
Running time
Already showed that worst case running time is Q(n2)
This is true for both the recursive version and for the iterative version
(as derived earlier in class).
Insertion-Sort(A)
Array size to be solved decreases by 1
for i <- 2 to length[A]
each time through loop.
do Insert(A, i)
T(n) = T(n-1) + cost of Insert
Insert(A, i)
Array size to be solved decreases by
key <- A[i]
1 each time through loop.
j <- i
T2(n) = T2(n-1) + cost of assignments
while j > 1 and less(key, A[j-1])
do A[j] <- A[j-1]
= T2(n-1) + 1
j <- j-1
T2(n) = Q(n)
{Insert key}
T(n) = T(n-1) + n
A[j] <- key
T(n) = Q(n2)
9
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.
10
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.
11
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
T2(n) = T2(n-1) + cost of
assignments and less( )
= T2(n-1) + 1
T2(n) = Q(n)
T(n) = T(n-1) + n
T(n) = Q(n2)
12