0% found this document useful (0 votes)
10 views

Sorting Algorithms

The document discusses the time complexities of various sorting algorithms including bubble sort, selection sort, counting sort, heap sort, insertion sort, merge sort, quick sort, and radix sort. It provides figures and pseudocode examples for each algorithm.

Uploaded by

samiamahbub2580
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Sorting Algorithms

The document discusses the time complexities of various sorting algorithms including bubble sort, selection sort, counting sort, heap sort, insertion sort, merge sort, quick sort, and radix sort. It provides figures and pseudocode examples for each algorithm.

Uploaded by

samiamahbub2580
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Sorting Algorithms Time Complexity

Graphs

January 7, 2024

Author: Sahib Abbas Bahar Chowdhury


ID: 21701022
Department: Computer Science & Engineering
Course: CSE 412
TextBook: Introduction to Algorithm
Submitted to: Professor Muhammad Sanaullah
Chowdhury
University of Chittagong

1
Apologies
Errors might be present in the provided graphs or pseudocodes. If you notice
any mistakes or have suggestions for improvements, I sincerely apologize and
appreciate your understanding. Please feel free to provide feedback so that I
can enhance the quality of the content.

1 Sorting Algorithm Time Complexities

Figure 1: Bubble Sort

1
Figure 2: Selection Sort

2
Figure 3: Counting Sort

3
Figure 4: Heap Sort

4
Figure 5: Insertion Sort

5
Figure 6: Merge Sort

6
Figure 7: Quick Sort

7
Figure 8: Radix Sort

8
2 Psuedocodes
Bubble Sort

Algorithm 1 Bubble Sort


1: procedure BubbleSort(arr)
2: n ← length of arr
3: for i ← 0 to n − 1 do
4: for j ← 0 to n − i − 1 do
5: if arr[j] > arr[j + 1] then
6: swap(arr[j], arr[j + 1])
7: end if
8: end for
9: end for
10: end procedure

9
Selection Sort

Algorithm 2 Selection Sort


1: procedure SelectionSort(arr)
2: n ← length of arr
3: for i ← 0 to n − 1 do
4: minIndex ← i
5: for j ← i + 1 to n do
6: if arr[j] < arr[minIndex] then
7: minIndex ← j
8: end if
9: end for
10: swap(arr[i], arr[minIndex])
11: end for
12: end procedure

10
Counting Sort

Algorithm 3 COUNTING-SORT(A, B, k)
1: for i ← 0 to k do
2: C[i] ← 0
3: end for
4: for j ← 1 to A.length do
5: C[A[j]] ← C[A[j]] + 1
6: end for
7: for i ← 1 to k do
8: C[i] ← C[i] + C[i − 1]
9: end for
10: for j ← A.length downto 1 do
11: B[C[A[j]]] ← A[j]
12: C[A[j]] ← C[A[j]] − 1
13: end for

11
Heap Sort

Algorithm 4 MAX-HEAPIFY(A, i)
1: l ← LEFT(i)
2: r ← RIGHT(i)
3: if l ≤ A.heap-size and A[l] > A[i] then
4: largest ← l
5: else
6: largest ← i
7: end if
8: if r ≤ A.heap-size and A[r] > A[largest] then
9: largest ← r
10: end if
11: if largest ̸= i then
12: exchange A[i] with A[largest]
13: MAX-HEAPIFY(A, largest)
14: end if

Algorithm 5 BUILD-MAX-HEAP(A)
1: A.heap-size ← A.length
2: for i ← ⌊(A.length/2)⌋ downto 1 do
3: MAX-HEAPIFY(A, i)
4: end for

Algorithm 6 HEAPSORT(A)
1: BUILD-MAX-HEAP(A)
2: for i ← A.length downto 2 do
3: exchange A[1] with A[i]
4: A.heap-size ← A.heap-size − 1
5: MAX-HEAPIFY(A, 1)
6: end for

12
Insertion Sort

Algorithm 7 INSERTION-SORT(A)
1: for j ← 2 to A.length do
2: key ← A[j]
3: i←j−1
4: while i > 0 and A[i] > key do
5: A[i + 1] ← A[i]
6: i←i−1
7: end while
8: A[i + 1] ← key
9: end for

13
Merge Sort

Algorithm 8 MERGE-SORT(A, p, r)
1: if p < r then
2: q ← ⌊((p + r)/2)⌋
3: MERGE-SORT(A, p, q)
4: MERGE-SORT(A, q + 1, r)
5: MERGE(A, p, q, r)
6: end if

Algorithm 9 MERGE(A, p, q, r)
1: n1 ← q − p + 1
2: n2 ← r − q
3: let L[1..n1 + 1] and R[1..n2 + 1] be new arrays
4: for i ← 1 to n1 do
5: L[i] ← A[p + i − 1]
6: end for
7: for j ← 1 to n2 do
8: R[j] ← A[q + j]
9: end for
10: L[n1 + 1] ← ∞
11: R[n2 + 1] ← ∞
12: i ← 1
13: j ← 1
14: for k ← p to r do
15: if L[i] ≤ R[j] then
16: A[k] ← L[i]
17: i←i+1
18: else
19: A[k] ← R[j]
20: j ←j+1
21: end if
22: end for

14
Quick Sort

Algorithm 10 PARTITION(A, p, r)
1: x ← A[r]
2: i ← p − 1
3: for j ← p to r − 1 do
4: if A[j] ≤ x then
5: i←i+1
6: exchange A[i] with A[j]
7: end if
8: end for
9: exchange A[i + 1] with A[r]
10: return i + 1

Algorithm 11 QUICKSORT(A, p, r)
1: if p < r then
2: q ← PARTITION(A, p, r)
3: QUICKSORT(A, p, q − 1)
4: QUICKSORT(A, q + 1, r)
5: end if

15
Radix Sort

Algorithm 12 RADIX-SORT(A, d)
1: for i ← 1 to d do
2: use counting sort to sort array A on digit i
3: end for

Algorithm 13 COUNTING-SORT(A, B, k)
1: for i ← 0 to k do
2: C[i] ← 0
3: end for
4: for j ← 1 to A.length do
5: C[A[j]] ← C[A[j]] + 1
6: end for
7: for i ← 1 to k do
8: C[i] ← C[i] + C[i − 1]
9: end for
10: for j ← A.length downto 1 do
11: B[C[A[j]]] ← A[j]
12: C[A[j]] ← C[A[j]] − 1
13: end for

16

You might also like