Week 02 (Complexity of Sorting Algorithms)
Week 02 (Complexity of Sorting Algorithms)
Sorting Algorithms
Course Code: CSC2211 Course Title: Algorithms
1. Sorting Algorithms
Insertion Sort
Selection Sort
Bubble Sort
Merge Sort
Quick Sort
Counting Sort
Sorting
Simple sorting methods use roughly n * n
comparisons
Insertion sort
Selection sort
Bubble sort
Example: 6, 4, 1, 8, 5
6 |4 1 8 5
4 6|1 8 5
1 4 6|8 5
1 4 6 8|5
1 4 5 6 8
Selection Sorting
Concept for sort in ascending order:
Locate smallest element in array. Exchange it with element in
position 0
Min Min Locate next smallest element in array. Exchange it with element in
value Index position 1.
8 0 Continue until all elements are arranged in order
5 1
1 3
5 1
3 5
7 2
5 5
8 3
7 5
9 4
8 5
Selection Sort Algorithm
void selectionSort(int array[], int n)
{
int select, minIndex, minValue;
18 26 32 6 43 15 9 1 22 26 19 55 37 43 99 2
18 26 32 6 43 15 9 1 22 26 19 55 37 43 99 2
18 26 32 6 43 15 9 1 22 26 19 55 37 43 99 2
18 26 32 6 43 15 9 1 22 26 19 55 37 43 99 2
18 26 32 6 43 15 9 1 22 26 19 55 37 43 99 2
Merge Sort Example
18 26 32 6 43 15 9 1 6 18 26 32 1 9 15 43
43
18 26 32 6 43 15 9 1 18 26 6 32 15 43 1 9
18 26 32 6 43 15 9 1 18 26 32 6 43 15 9 1
26 32 6 43 15 9 1
Exercise
98 23 45 14 6 67 33 42
Merge Sort Algorithm
Elements to sort /
Recursion-tree Method
Recursion Trees
• Show successive expansions of recurrences using trees.
• Keep track of the time spent on the subproblems of a divide and
conquer algorithm.
• Help organize the algebraic bookkeeping necessary to solve a
recurrence.
Running time of Merge Sort: T(n) = O(1) if n = 1
T(n) = 2T(n/2) + O(n) if n > 1
Rewrite the recurrence as T(n) = c if n = 1
T(n) = 2T(n/2) + cn if n > 1
For the original problem, Each of the size n/2 problems has
we have a cost of cn, plus a cost of cn/2 plus two
two subproblems each of subproblems, each costing T(n/4).
size (n/2) and running time
T(n/2).
cn
cn
Cost of divide
and merge.
cn/2 cn/2
T(n/2) T(n/2)
T(n/4) T(n/4) T(n/4) T(n/4)
Cost of sorting
subproblems.
Recursion Tree for Merge Sort
Continue expanding until the problem size reduces to 1.
cn cn
cn/2 cn/2 cn
log2n + 1
c c c c c c cn
Total : cnlog2n+ cn
Recursion Tree for Merge Sort
• The idea is based on of choosing one element as a pivot element and partitioning the
array around it such that:
Left side of pivot contains all the elements that are less than the pivot element.
Right side contains all elements greater than the pivot.
• It reduces the space complexity and removes the use of the auxiliary array that is used
in merge sort.
• Selecting a random pivot in an array results in an improved time complexity in most of
the cases.
Quicksort
Pivot
2 8 7 1 3 5 6 4
Input array:
2 1 3 4 7 5 6 8
Quicksort: Partition
i=p-1
P, j Pivot
2 8 7 1 3 5 6 4
Quicksort: Partition
When ith element is smaller than Pivot,
i=1
increase the value of i by 1
j=1
j
2<4 – i++ Pivot
2 8 7 1 3 5 6 4
Quicksort: Partition
i=1
j=2 8>4 – increase j
j Pivot
2 8 7 1 3 5 6 4
Quicksort: Partition
i=1
j=3 7>4 – increase j
j Pivot
2 8 7 1 3 5 6 4
Quicksort: Partition
i=2
j=4 1<4 – increase j and i++
j Pivot
2 8 7 1 3 5 6 4
Quicksort: Partition
i=2
j=4 1<4 – swap
j Pivot
2 1 7 8 3 5 6 4
Quicksort: Partition
i=3
j=5 3<4 – increase j and i++
j Pivot
2 1 7 8 3 5 6 4
Quicksort: Partition
i=3
j=5 3<4 – increase j and i++
j Pivot
2 1 7 8 3 5 6 4
Quicksort: Partition
i=3
j=5 3<4 – swap
j Pivot
2 1 3 8 7 5 6 4
Quicksort: Partition
i=3
j=6 5>4 – increase j
j Pivot
2 1 3 8 7 5 6 4
Quicksort: Partition
i=3
j=7 6>4 – increase j
j Pivot
2 1 3 8 7 5 6 4
Quicksort: Partition
i=3
j=7 6>4 – increase j
j Pivot
2 1 3 8 7 5 6 4
Quicksort: Partition
i=3
j=7 Pivot=i+1
j Pivot
2 1 3 4 7 5 6 8
Quicksort: Partition
Pivot Pivot
2 1 3 4 7 5 6 8
Sorting Algorithms with linear time
Counting Sort
orti Counting sort: No comparisons between elements.
Input: A[1 . . n], where A[j] ∈ {1, 2, . . . , k}.
Output: B[1 . . n], sorted.
Auxiliary storage: C [1 . . k].
1 for i ← 1 to k
2 do C [i ] ←
3 for j 0
← 1 to n
4 do C [A[j]] ← C [A[j]] d C [i ] = |{key
5 for i +
← 12 to k = i }|
6 do C [i ] ← C [i ] + C
7 [i −
for j ← 1]
n downto 1 d C [i ] = |{key
8 do B[C [A[j]]] ← ≤ i }|
9 A[j]C [A[j]] ← C [A[j]]
−1
12 /
Counting sort example
Counting sort example Loop 1
for i ←1 to k
do C [i ] ←0
Counting sort example Loop 2
for j ←1 to n
do C [A[j ]] ←C [A[j ]] + 1 C [i ] = |{key = i}|
Counting sort example Loop 2
for j ←1 to n
do C [A[j ]] ←C [A[j ]] + 1 C [i ] = |{key = i}|
Counting sort example Loop 2
for j ←1 to n
do C [A[j ]] ←C [A[j ]] + 1 C [i ] = |{key = i}|
Counting sort example Loop 2
for j ←1 to n
do C [A[j ]] ←C [A[j ]] + 1 C [i ] = |{key = i}|
Counting sort example Loop 2
for j ←1 to n
do C [A[j ]] ←C [A[j ]] + 1 C [i ] = |{key = i}|
Counting sort example Loop 3
for i ←2 to k
do C [i ] ←C [i ] + C [i −1] C [i ] = |{key ≤i}|
Counting sort example Loop 3
for i ←2 to k
do C [i ] ←C [i ] + C [i −1] C [i ] = |{key ≤i}|
Counting sort example Loop 3
for i ←2 to k
do C [i ] ←C [i ] + C [i −1] C [i ] = |{key ≤i}|
Counting sort example loop 4
for j ←n downto 1
do B[C [A[j ]]] ←A[j ]
C [A[j ]] ←C [A[j ]] −1
Counting sort example Loop 4
for j ←n downto 1
do B[C [A[j ]]] ←A[j ]
C [A[j ]] ←C [A[j ]] −1
Counting sort example Loop 4
for j ←n downto 1
do B[C [A[j ]]] ←A[j ]
C [A[j ]] ←C [A[j ]] −1
Counting sort example Loop 4
for j ←n downto 1
do B[C [A[j ]]] ←A[j ]
C [A[j ]] ←C [A[j ]] −1
Counting sort example Loop 4
for j ←n downto 1
do B[C [A[j ]]] ←A[j ]
C [A[j ]] ←C [A[j ]] −1
Counting sort Complexity
O(k)
O(n)
O(k)
O(n)
O(n + k)
The worst-case running time of Counting sort is O(n + k).
If k = O(n), then the worst case running time is O(n).
Books
Fundamental of Computer Algorithms, Ellis Horowitz, Sartaj Sahni, Sanguthevar Rajasekaran (HSR)
References
https://www.google.com/search?q=bubble+sort+
step+by+step&sxsrf=ALeKk01uxzgfT3Oy6k1Q3WxVnSpiIN8_4g:1587999728942
&tbm=isch&source=iu&ictx=1&fir=vRwFsGwVfJ6pJM%253A%252CSzhhze6MPQr4c
M%252C_&vet=1&usg=AI4_-kSrEEXqwRL-PkHhVUtn7jNfF9dB6g&sa=X&ved=2ahUK
Ewje0Pz974jpAhXRAnIKHWhMD2UQ_h0wAXoECAcQBg#imgrc=EN4Sdu7veOWVo
M&imgdii=eOqvCu85p9-eBM
https://www.interviewcake.com/concept/java/counting-sort
https://www.geeksforgeeks.org/counting-sort/
https://www.hackerearth.com/practice/algorithms/sorting/quick-sort/tutorial/