0% found this document useful (0 votes)
30 views10 pages

Overview of Sorting Algorithms

The document provides an overview of sorting algorithms, including their purpose, types, and specific examples such as Bubble Sort, Selection Sort, Insertion Sort, Merge Sort, and Quick Sort. It outlines the steps and algorithms for each sorting method, along with their time complexity in best, average, and worst cases. Additionally, it briefly explains the concept of recursion as a programming technique used in some sorting algorithms.

Uploaded by

Rabbi Noor
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views10 pages

Overview of Sorting Algorithms

The document provides an overview of sorting algorithms, including their purpose, types, and specific examples such as Bubble Sort, Selection Sort, Insertion Sort, Merge Sort, and Quick Sort. It outlines the steps and algorithms for each sorting method, along with their time complexity in best, average, and worst cases. Additionally, it briefly explains the concept of recursion as a programming technique used in some sorting algorithms.

Uploaded by

Rabbi Noor
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

A sorting algorithm is a procedure used to arrange elements of a list or array in a

specific order, usually ascending or descending.


Purpose:
●​ Make data easier to search
●​ Improve efficiency in data processing
●​ Used in databases, applications, and algorithms

Types of Sorting Algorithms

Type Examples Key Idea

Simple / Selection Sort, Insertion Compare elements and swap


Comparison-based Sort, Bubble Sort as needed

Efficient / Divide & Merge Sort, Quick Sort, Divide array into smaller
Conquer Heap Sort subarrays, sort, then merge

Non-Comparison / Counting Sort, Radix Use element properties like


Linear-time Sort, Bucket Sort counting frequency

Comparison Table

Algorithm Technique Best Average Worst

Bubble Sort O(n) O(n²) O(n²)

Selection Sort Comparison O(n²) O(n²) O(n²)

Insertion Sort O(n) O(n²) O(n²)

Merge Sort O(n log n) O(n log n) O(n log n)


Divide & Conquer
Quick Sort O(n log n) O(n log n) O(n²)
Bubble Sort

Steps
1.​ Start from the first element of the array.
2.​ Compare the current element with the next element.
3.​ If the current element is greater than the next element, swap them.
4.​ Move to the next pair and repeat step 3 for the full pass.
5.​ After each pass, the largest element “bubbles up” to its correct position at
the end.
6.​ Repeat passes until no swap is needed.

Algorithm
BubbleSort(A, n)
for i ← 0 to n-2
for j ← 0 to n-i-2
if A[ j] > A[ j+1]
swap(A[ j], A[ j+1])

Example: Array [12, 11, 13, 5, 6]


Comparison
Pass Swap Array
Positions Elements
Initial - - - [12, 11, 13, 5, 6]
(0,1) 12 & 11 Yes [11, 12, 13, 5, 6]
(1,2) 12 & 13 No [11, 12, 13, 5, 6]
Pass 1
(2,3) 13 & 5 Yes [11, 12, 5, 13, 6]
(3,4) 13 & 6 Yes [11, 12, 5, 6, 13]
(0,1) 11 & 12 No [11, 12, 5, 6, 13]
Pass 2 (1,2) 12 & 5 Yes [11, 5, 12, 6, 13]
(2,3) 12 & 6 Yes [11, 5, 6, 12, 13]
(0,1) 11 & 5 Yes [5, 11, 6, 12, 13]
Pass 3
(1,2) 11 & 6 Yes [5, 6, 11, 12, 13]
Pass 4 (0,1) 5&6 No [5, 6, 11, 12, 13]
Done - No swaps, Sorted - [5, 6, 11, 12, 13]
Selection Sort

Steps
1.​ Start with the first element.
2.​ Find the minimum element in the unsorted part.
3.​ Swap it with the first unsorted element.
4.​ Repeat until all elements are sorted.​

Algorithm
for i ← 0 to n-2
minIndex ← i
for j ← i+1 to n-1
if A[ j] < A[minIndex]
minIndex ← j
swap A[i] with A[minIndex]

Example: Array: [64, 25, 12, 22, 11]

Step Operation Array after step Explanation

1 Find min (11) & swap with 64 [11, 25, 12, 22, 64] Minimum 11 placed at
index 0

2 Find min (12) & swap with 25 [11, 12, 25, 22, 64] Minimum 12 placed at
index 1

3 Find min (22) & swap with 25 [11, 12, 22, 25, 64] Minimum 22 placed at
index 2

4 Find min (25) & swap with 25 [11, 12, 22, 25, 64] Element already in
place

5 Only one element left [11, 12, 22, 25, 64] Array fully sorted
Insertion Sort

Steps
1.​ Start from the second element.
2.​ Compare it with elements in the sorted portion.
3.​ Insert it at the correct position.
4.​ Repeat for all elements.​

Algorithm
for i ← 1 to n-1
key ← A[i]
j ← i-1
while j ≥ 0 and A[ j] > key
A[ j+1] ← A[ j]
j ← j-1
A[ j+1] ← key

Example: Array: [12, 11, 13, 5, 6]

i Key j← j ≥ 0 and A[ j] > A[ j+1] ← j← A[ j+1] ← Array


i-1 key A[ j] j-1 key

Initial [12, 11, 13, 5, 6]

1 11 0 0 ≥ 0 and 12 > 11 A[1] ← 12 -1 A[0] ← 11 [11, 12, 13, 5, 6]

2 13 1 1 ≥ 0 and 12 > 13 - - - [11, 12, 13, 5, 6]

3 5 2 2 ≥ 0 and 13 > 5 A [3]← 13 1 - [11, 12, 13, 13, 6]

1 ≥ 0 and 12 > 5 A[2] ← 12 0 - [11, 12, 12, 13, 6]

0 ≥ 0 and 11 > 5 A[1] ← 11 -1 A [0] ← 5 [5, 11, 12, 13, 6]

4 6 3 3 ≥ 0 and 13 > 6 A[4] ← 13 2 - [5, 11, 12, 13, 13]

2 ≥ 0 and 12 > 6 A[3] ← 12 1 - [5, 11, 12, 12, 13]

1 ≥ 0 and 11 > 6 A[2] ← 11 0 - [5, 11, 11, 12, 13]

0 ≥ 0 and 5 > 6 - - A[1] ← 6 [5, 6, 11, 12, 13]


Merge Sort

Steps
1.​ Divide the array into two halves.
2.​ Recursively sort both halves.
3.​ Merge the two sorted halves.​

Algorithm
MergeSort(A, low, high)
if low < high
mid = (low + high) / 2
MergeSort(A, low, mid)
MergeSort(A, mid+1, high)
Merge(A, low, mid, high)

Merge(A, low, mid, high)


i = low
j = mid + 1
k = low
create Temp[] array

while i ≤ mid and j ≤ high


if A[i] ≤ A[ j]
Temp[k] = A[i]
i=i+1
else
Temp[k] = A[ j]
j=j+1
k=k+1

while i ≤ mid
Temp[k] = A[i]
i=i+1
k=k+1

while j ≤ high
Temp[k] = A[ j]
j=j+1
k=k+1

for x from low to high


A[x] = Temp[x]
Example: Example: Array: [38, 27, 43, 3, 9, 82, 10]

Divide Phase

Step Action
1 Divide [38 27 43 3 9 82 10] into [38 27 43 3] & [9 82 10]
2 Divide [38 27 43 3] → [38 27] & [43 3]
3 Divide [38 27] → [38] & [27]
4 Divide [43 3] → [43] & [3]
5 Divide [9 82 10] → [9] & [82 10]
6 Divide [82 10] → [82] & [10]

Merge Phase

Merging Result
Merge [38] & [27] [27 38]
Merge [43] & [3] [3 43]
Merge [27 38] & [3 43] [3 27 38 43]
Merge [82] & [10] [10 82]
Merge [9] & [10 82] [9 10 82]
Final Merge [3 9 10 27 38 43 82]
Quick Sort

Steps
1.​ Choose a pivot element.
2.​ Partition array: elements less than pivot left, greater right.
3.​ Recursively apply Quick Sort on left and right subarrays.​

Algorithm
Quick_Sort(A, low, high)
if low < high
pi ← Partition(A, low, high)
Quick_Sort(A, low, pi-1)
Quick_Sort(A, pi+1, high)

Partition(A, low, high)


pivot ← A[low]
i ← low
j ← high

while i < j
while A[i] <= pivot AND i <= high-1
i←i+1

while A[ j] > pivot AND j >= low+1


j←j-1

if i < j
swap A[i] and A[ j]

swap A[low] and A[ j]


return j

Example: A = [50, 23, 9, 18, 61, 32]

Iter i j i moves j moves i after j after Action Array


(value) (value)

1 0 5 i: 0→1 j: 5 4 5 i<j → [50, 23, 9,


(50→23), (A[5]=32) swap A[4] 18, 32, 61]
→2 not >50 → & A[5] (61
(23→9), j stays 5 ↔ 32)
Iter i j i moves j moves i after j after Action Array
(value) (value)

→3
(9→18),
→4 (18→61
stops)

2 4 5 i: A[4]=32 j: A[5]=61 5 4 now i<j? (no


≤50 and >50 → (5<4 false) change)
i≤4 → j:5→4 → exit
i:4→5 while

End — — — — — — swap [32, 23, 9,


A[low] & 18, 50, 61]
A[ j] →
swap A[0]
& A[4] (50
↔ 32)

Iter i j i moves j moves i after j after Action Array


(value) (value)

1 0 3 i: 0→1 j: A[3]=18 3 3 i<j? (3<3 (no


(32→23), >32? no → false) → change)
→2 j stays3 exit while
(23→9),
→3 (9→18
but i≤2
allowed →
i
becomes3)

End — — — — — — swap [18, 23, 9,


A[low] & 32, 50, 61]
A[ j] →
swap A[0]
Iter i j i moves j moves i after j after Action Array
(value) (value)

& A[3] (32


↔ 18)

Iter i j i moves j moves i after j after Action Array


(value) (value)

1 0 2 i: 0→1 j: A[2]=9 1 2 i<j (1<2) [18, 9, 23,


(18→23) >18? no → j → swap 32, 50, 61]
stops stays2 A[1] &
because A[2] (23
A[1]=23 ≤? ↔ 9)
no

2 1 2 i: A[1]=9 j: A[2]=23 2 1 i<j? (2<1 (no change)


≤18 and i≤1 >18 → false) →
→ i:1→2 j:2→1 exit
while

End — — — — — — swap [9, 18, 23,


A[low] & 32, 50, 61]
A[ j] →
swap
A[0] &
A[1] (18 ↔
9)
Recursion: Recursion is a programming technique where a function calls itself
repeatedly until a base condition or stopping condition is met.

A recursive function has:


●​ Base Case: condition that stops further recursive calls.
●​ Recursive Case: function calling itself with a reduced/smaller input.

General Structure
function recursion(n)
if (base condition)
return value
else
return recursion(n-1)

Recursive Algorithm Example

#include <stdio.h> Factorial(5)


int Factorial(int n) { 5 * Factorial(4)
if (n == 0) 4 * Factorial(3)
return 1; 3 * Factorial(2)
else 2 * Factorial(1)
return n * Factorial(n - 1); 1 * Factorial(0)
} return 1
int main(void) {
int n;
printf("Enter a number: ");
scanf("%d", &n);
printf("Factorial of %d = %d\n", n, Factorial(n));
return 0;
}

You might also like