Sorting and Searching – Detailed Notes
Selection Sort
Definition:
Selection Sort is a simple comparison-based sorting technique. It divides the array into two
parts: sorted and unsorted. It repeatedly selects the minimum element from the unsorted
part and moves it to the end of the sorted part.
Example:
Input: [29, 10, 14, 37, 14]
Step 1: [10, 29, 14, 37, 14]
Step 2: [10, 14, 29, 37, 14] ...
Insertion Sort
Definition:
Insertion Sort builds the sorted array one element at a time. It takes an element and inserts
it at the correct position in the already sorted part.
Example:
Input: [5, 2, 4, 6, 1, 3]
Step 1: [2, 5, 4, 6, 1, 3]
Step 2: [2, 4, 5, 6, 1, 3] ...
Bubble Sort
Definition:
Bubble Sort repeatedly steps through the list, compares adjacent elements, and swaps them
if they are in the wrong order. This continues until no swaps are needed.
Example:
Input: [5, 1, 4, 2, 8]
Step 1: [1, 4, 2, 5, 8] ...
Quick Sort
Definition:
Quick Sort is a divide-and-conquer algorithm. It picks a 'pivot' element, partitions the array
around the pivot (elements less than pivot to the left, greater to the right), and then
recursively sorts the partitions.
Example:
Input: [10, 7, 8, 9, 1, 5]
Pivot = 5, Partitions: [1], [5], [10, 7, 8, 9] ...
Merge Sort
Definition:
Merge Sort is a divide-and-conquer sorting algorithm. It divides the array into halves,
recursively sorts them, and merges the sorted halves.
Example:
Input: [38, 27, 43, 3, 9, 82, 10]
Divide: [38, 27, 43] and [3, 9, 82, 10] ...
Radix Sort
Definition:
Radix Sort is a non-comparative sorting algorithm. It sorts the elements based on digits
(from least significant digit to most significant). It is useful for integers and fixed-length
strings.
Example:
Input: [170, 45, 75, 90, 802, 24, 2, 66]
Sort by units → tens → hundreds.
Linear Search
Definition:
Linear Search checks each element one by one from the start to the end until the desired
element is found or the list ends.
Example:
Search 4 in [1, 3, 5, 4, 2]: Found at index 3
Binary Search
Definition:
Binary Search works on a **sorted** array. It repeatedly divides the search interval in half
and compares the middle element with the target.
Example:
Search 6 in [1, 3, 5, 6, 9, 11]: Mid=5, Go left, Mid=6 → Found