5-Selection Sort-Linear-Time
5-Selection Sort-Linear-Time
Tech
Course Code: AIML303/AIDS303/IOT303
Course Name: Design & Analysis of Algorithms
• The Selection Sort algorithm finds the lowest value in an array and moves it to
the front of the array.
• Time Complexity: θ(n2) (in all three cases: Best, Worst, Average)
Bubble Sort
• The simplest sorting algorithm that works by repeatedly swapping the adjacent
elements if they are in the wrong order.
• Time Complexity: θ(n2) (in all three cases: Best, Worst, Average)
Comparison Sorting Review
• Merge sort:
• Divide-and-conquer:
• Split array in half
• Recursively sort sub-arrays
• Linear-time merge step
• Pro’s:
• O(n lg n) worst case - asymptotically optimal for
comparison sorts
• Con’s:
• Doesn’t sort in place
Comparison Sorting Review
• Heap sort:
• Uses the very useful heap data structure
• Complete binary tree
• Heap property: parent key > children’s keys
• Pro’s:
• O(n lg n) worst case - asymptotically optimal for
comparison sorts
• Sorts in place
• Con’s:
• Fair amount of shuffling memory around
Comparison Sorting Review
• Quick sort:
• Divide-and-conquer:
• Partition array into two sub-arrays, recursively sort
• All of first sub-array < all of second sub-array
• Pro’s:
• O(n lg n) average case
• Sorts in place
• Fast in practice (why?)
• Con’s:
• O(n2) worst case
• Naïve implementation: worst case on sorted input
• Good partitioning makes this very unlikely.
Sorting in Linear Time
7
Non-Comparison Based Sorting
for i 1 to k
do C[i] 0
for j 1 to n
do C[A[ j]] C[A[ j]] + 1 ⊳ C[i] = |{key = i}|
for i 2 to k
do C[i] C[i] + C[i–1] ⊳ C[i] = |{key i}|
for j n downto 1
do B[C[A[ j]]] A[ j]
C[A[ j]] C[A[ j]] – 1
Counting-sort example
1 2 3 4 5 1 2 3 4
A: 4 1 3 4 3 C:
B:
Loop 1
1 2 3 4 5 1 2 3 4
A: 4 1 3 4 3 C: 0 0 0 0
B:
for i 1 to k
do C[i] 0
Loop 2
1 2 3 4 5 1 2 3 4
A: 4 1 3 4 3 C: 0 0 0 1
B:
for j 1 to n
do C[A[ j]] C[A[ j]] + 1 ⊳ C[i] = |{key = i}|
Loop 2
1 2 3 4 5 1 2 3 4
A: 4 1 3 4 3 C: 1 0 0 1
B:
for j 1 to n
do C[A[ j]] C[A[ j]] + 1 ⊳ C[i] = |{key = i}|
Loop 2
1 2 3 4 5 1 2 3 4
A: 4 1 3 4 3 C: 1 0 1 1
B:
for j 1 to n
do C[A[ j]] C[A[ j]] + 1 ⊳ C[i] = |{key = i}|
Loop 2
1 2 3 4 5 1 2 3 4
A: 4 1 3 4 3 C: 1 0 1 2
B:
for j 1 to n
do C[A[ j]] C[A[ j]] + 1 ⊳ C[i] = |{key = i}|
Loop 2
1 2 3 4 5 1 2 3 4
A: 4 1 3 4 3 C: 1 0 2 2
B:
for j 1 to n
do C[A[ j]] C[A[ j]] + 1 ⊳ C[i] = |{key = i}|
Loop 3
1 2 3 4 5 1 2 3 4
A: 4 1 3 4 3 C: 1 0 2 2
B: C': 1 1 2 2
for i 2 to k
do C[i] C[i] + C[i–1] ⊳ C[i] = |{key i}|
Loop 3
1 2 3 4 5 1 2 3 4
A: 4 1 3 4 3 C: 1 0 2 2
B: C': 1 1 3 2
for i 2 to k
do C[i] C[i] + C[i–1] ⊳ C[i] = |{key i}|
Loop 3
1 2 3 4 5 1 2 3 4
A: 4 1 3 4 3 C: 1 0 2 2
B: C': 1 1 3 5
for i 2 to k
do C[i] C[i] + C[i–1] ⊳ C[i] = |{key i}|
Loop 4
1 2 3 4 5 1 2 3 4
A: 4 1 3 4 3 C: 1 1 3 5
B: 3 C': 1 1 2 5
for j n downto 1
do B[C[A[ j]]] A[ j]
C[A[ j]] C[A[ j]] – 1
Loop 4
1 2 3 4 5 1 2 3 4
A: 4 1 3 4 3 C: 1 1 2 5
B: 3 4 C': 1 1 2 4
for j n downto 1
do B[C[A[ j]]] A[ j]
C[A[ j]] C[A[ j]] – 1
Loop 4
1 2 3 4 5 1 2 3 4
A: 4 1 3 4 3 C: 1 1 2 4
B: 3 3 4 C': 1 1 1 4
for j n downto 1
do B[C[A[ j]]] A[ j]
C[A[ j]] C[A[ j]] – 1
Loop 4
1 2 3 4 5 1 2 3 4
A: 4 1 3 4 3 C: 1 1 1 4
B: 1 3 3 4 C': 0 1 1 4
for j n downto 1
do B[C[A[ j]]] A[ j]
C[A[ j]] C[A[ j]] – 1
Loop 4
1 2 3 4 5 1 2 3 4
A: 4 1 3 4 3 C: 0 1 1 4
B: 1 3 3 4 4 C': 0 1 1 3
for j n downto 1
do B[C[A[ j]]] A[ j]
C[A[ j]] C[A[ j]] – 1
Analysis
for i 1 to k
(k) do C[i] 0
for j 1 to n
(n) do C[A[ j]] C[A[ j]] + 1
for i 2 to k
(k) do C[i] C[i] + C[i–1]
for j n downto 1
(n) do B[C[A[ j]]] A[ j]
C[A[ j]] C[A[ j]] – 1
(n + k)
Running time
A: 4 1 3 4 3
B: 1 3 3 4 4
• Could we use counting sort to sort 32 bit integers? Why or why not?
Counting Sort Review
• Intuitively, you might sort on the most significant digit, then the second
msd, etc.
• Problem: lots of intermediate piles of cards (read: scratch arrays) to
keep track of
• Key idea: sort the least significant digit first
RadixSort(A, d)
for i=1 to d
StableSort(A) on digit i
Radix Sort Example
Radix Sort Correctness
Bucket-Sort(A)
1. n = length[A]
2. for i = 1 to n
3. do insert A[i] into list B[floor of nA[i]]
4. for i = 0 to n-1
5. do sort list i with Insertion-Sort
6. Concatenate lists B[0], B[1],…,B[n-1]
39
Bucket Sort
Bucket-Sort(A, x, y)
1. divide interval [x, y) into n equal-sized subintervals (buckets)
2. distribute the n input keys into the buckets
3. sort the numbers in each bucket (e.g., with insertion sort)
4. scan the (sorted) buckets in order and produce output array
Running time of bucket sort: O(n) expected time
Step 1: O(1) for each interval = O(n) time total.
Step 2: O(n) time.
Step 3: The expected number of elements in each bucket is O(1),
so total is O(n)
Step 4: O(n) time to scan the n buckets containing a total of n
input elements
40
Bucket Sort Example
Bucket Sort Review
43