L9_sorting
L9_sorting
Sorting
Several pages are based on the notes by Dr. Bo Tang and Dr. Zongwei Luo
Sorting
Sorting Applications
?
Sorting
• Sorting problem
• Input: an array A[1…n] with n integers (characters, strings, etc., as long as
proper ordering can be established between pairs)
• Output: a sorted array A (in ascending or descending order)
• All examples in this lecture will be with integers and in ascending order.
Descending order sorting just needs reversing the comparison signs
Types of Sorting Algorithms
• Comparison-based sorting
• Only uses comparisons (<, =, >, ≤, ≥) between
pairs to determine the final sorted sequence
• Running time lower bounded by Ω(n log n)
• Both worst and average cases
• Both deterministic and randomized algorithms
• Insertion sort, Bubble sort, Selection sort,
Mergesort, Heapsort, Quicksort
• Other sorting algorithms
• Under certain conditions, may run faster than
O(n log n)
• Counting sort, radix sort, bucket sort
Sorting
• For a deck of playing cards, how would you sort it?
Selection Sort
• Idea of selection sort
• Start with empty hand, all cards on table
• Select the smallest card from table
• Insert the card into hand
Selection Sort
Selection Sort
Sort A[1…n] in ascending order:
5. k←j
6. swap A[i] and A[k]
Selection Sort Performance
• Best-case time complexity: O(n2)
• Worst-case time complexity: O(n2)
• Average time complexity: O(n2)
• Space complexity: O(1)
• In-place algorithm, no significant amount of additional space needed
Insertion Sort
• Idea of insertion sort
• For each new card, insert it into the correct position among the existing set of
cards
• Correct position: keep comparing with the previous card, swap if in reverse
order
Insertion Sort
Insertion Sort
Sort A[1…n] in ascending order:
1. for integer i ← 2 to n
2. for integer j ← i downto 1 Cost: 1+2+…+n-
3. if A[ j-1] > A[ j ] then 1=O(n2)
• For Mergesort, a = 2, b = 2, c = 1
• So the time complexity for mergesort is O(n log n)
Mergesort Performance
• Best-case time complexity: O(n log n)
• Worst-case time complexity: O(n log n)
• Average time complexity: O(n log n)
• Space complexity: depends on the implementation, normally O(n)
• Stable (why?)
Randomized Algorithms
• So far, all our algorithms are deterministic
• Quicksort, the fastest sorting algorithm in practice, may include
randomization
• Randomized algorithms are not ambiguous – generating an outcome in a
randomized manner is unambiguous
• With randomized algorithms, we talk about expected running time
• Example: if we have n distinct numbers in array A and want to find the
position of one of the numbers c, we can randomly select an integer i
between 1 to n, and see if A[i]=c
• Expected running time: O(n)
A very bad randomized sorting algorithm…
• If we want to sort a deck of n cards, we can throw them into the air
until they fall on the ground exactly sorted
• Obviously extremely inefficient
• Throwing the cards is a randomized operation
• Expected running time: O(n!)
• Bogosort/slowsort/stupid sort
Quicksort
• Idea of quicksort
• (Randomly, or with some rules) pick an integer p in A, and call it pivot
• Rearrange the integers in A such that
• All the integers smaller than p are positioned before p
• All the integers larger than p are positioned after p
• Quicksort the part of A before p recursively
• Quicksort the part of A after p recursively
13
13 43
43 81
81
31
31 57
57 26
26 65
13
13 81
81 92
92 75
75
00
92
92 4343 65
65
31
31 57
57 26
26
75
75 00
0013
1326
2631
3143 57 65
4357 75
75 81
8192
92
0013
1326
2631
3143
4357
5765
6575
75 81
8192
92
Quicksort
• Quicksort A[1…n], lo = 1, hi = n:
1. if lo < hi then
2. p ← partition(A, lo, hi)
3. Quicksort(A, lo, p-1)
4. Quicksort(A, p+1, hi)
Quicksort (Hoare’s scheme) Swap with A[lo] also fine,
just remember to swap
back.
Assumed basic operation Alternatively, we can just
• Partition(A, lo, hi): which takes constant select the last element
time as pivot
1. p ← SELECTPIVOT(lo, hi), swap A[p] and A[hi]; pivot ← A[hi]
2. L ← lo-1, R ← hi Find a value in left side
3. while true greater than pivot
and a value in right side
4. do L++ while A[L] < pivot smaller than pivot
Algorithm
{
initialize count[ ];
0 1 88 100 while (read in a student’s record)
count insert to list count[stdnt.grade];
for (i=0; i<k; i++) {
if (count[i])
output list count[i];
}
}
Counting Sort Performance
• Assume that the there are k “bin”s
• Counting sort takes O(n + k) time
• Space complexity O(n + k)