Insertion Sort
Stable
In-place
• Time complexity Ω(n), Ѳ (𝒏𝟐 ), O (𝒏𝟐 )
• Space complexity O(1).
INSERTION-SORT(A)
1 for j 2 to length [A]
2 do key A[ j]
3 Insert A[j] into the sortted sequence A[1,..., j-1].
4 i j–1
5 while i > 0 and A[i] > key
6 do A[i+1] A[i]
7 ii–1
8 A[i +1] key
Merge Sort
Stable
Out-place
• Time complexity Ω(nlogn), Ѳ (nlogn), O (𝒏𝒍𝒐𝒈𝒏)
• Space complexity O(n).
Merge(A, p, q, r)
1. n1 q – p + 1
2. n2 r – q
3 for i 1 to n1
4 do L[i] A[p + i – 1]
5 for j 1 to n2
6 do R[j] A[q + j]
7 L[n1+1]
8 R[n2+1]
9 i1
10 j 1
11 for k p to r
12 do if L[i] R[j]
13 then A[k] L[i]
14 ii+1
15 else A[k] R[j]
16 jj+1
MAX_HEAPIFY
• Time complexity O(lgn) or O(h)
MAX‐HEAPIFY(A, i)
1. l ← LEFT(i)
2. r ← RIGHT(i)
3. if l ≤ heap‐size[A] and A[l] > A[i]
4. then largest ← l
5. else largest ← i
6. if r ≤ heap‐size[A] and a[r] > A[largest]
7. then largest ← r
8. if largest ≠ i
9. then exchange A[i] A[largest]
10. MAX‐HEAPIFY (A, largest)
Build max heap
• Time complexity O(n)
• O(nlgn) is not asymptotically tight.
BUILD‐MAX‐HEAP(A)
1. heap‐size[A] ← length[A]
2. for i ← ⌊length[A]/2⌋ downto 1
3. do MAX‐HEAPIFY(A,i)
Heap sort
Not Stable
In-place
• Time complexity Ω(nlogn), Ѳ (nlogn), O (𝒏𝒍𝒐𝒈𝒏)
• Space complexity O(1).
HEAPSORT(A)
1. BUILD‐MAX‐HEAP(A)
2. for i ← length[A] downto 2
3. do exchange A[1] A[i]
4. heap‐size[A] ← heap‐size[A] −1
5. MAX‐HEAPIFY(A, 1)
Priority Queue
HEAP‐MAXIMUM(A)
1. return A[1]
The running time of HEAP‐MAXIMUM is Θ(1).
HEAP‐EXTRACT‐MAX(A)
1. if heap‐size[A] < 1
2. then error “heap underflow”
3. max ← A[1]
4. A[1] ← A[heap‐size[A]]
5. heap‐size[A] ← heap‐size[A]−1
6. MAX‐HEAPIFY(A, 1)
7. return max
The running time of HEAP‐EXTRACT‐MAX is
O(lgn).
HEAP‐INCREASE‐KEY (A, i, key)
1. if key < A[i]
2. then error “new key is smaller thean current key”
3. A[i] ← key
4. While i > 1 and A[PARENT(i)] < A[i]
5. do exchange A[i] A[PARENT(i)]
6. i ← PARENT(i)
The running time is O(lgn).
MAX‐HEAP‐INSERT(A)
1. heap‐size[A] ← heap‐size[A]+1
2. A[heap‐size[A] ←
3. HEAP‐INCREASE‐KEY(A, heap‐size[A], key)
The running time is O(lgn).
Counting sort
Stable
Out-place
• Time complexity O (𝒏 + 𝒓)
• Space complexity O(n+r).
Radix sort
Stable
Out-place
• Time complexity O (𝒏 ∗ 𝒌/𝒅)
• Space complexity O(n+2^d).
Binary Search
• Time complexity O (logn)
• Space complexity O(1).