Computer Science · College · BCA, BTech & MCA
Searching & Sorting Algorithms: Binary Search, QuickSort & MergeSort
Comprehensive guide to sorting and searching: Linear vs Binary Search, Bubble, Insertion, QuickSort (Divide and Conquer) and MergeSort, with Big-O space and time complexity comparisons.
1Searching Algorithms: Linear vs Binary Search
Searching & Sorting Algorithms: Binary Search, QuickSort & MergeSort
Comprehensive guide to sorting and searching: Linear vs Binary Search, Bubble, Insertion, QuickSort (Divide and Conquer) and MergeSort, with Big-O space and time complexity comparisons.
- 1Subject — Computer Science
- 2Target Audience — BCA, BTech & MCA Students
- 3Topic Core — Divide & Conquer, Big-O Matrix & Stability
Linear Searchsequentially tests every element in an array until a target match is found, executing in time on unsorted collections.Binary Searchoperates onsorted arraysby repeatedly halving the search search space, achieving logarithmic runtime.
Binary Search Recurrence Relation
- n = Number of elements in the sorted array
- T(n) = Time complexity to search an array of size n
def binary_search(arr, target):
low = 0
high = len(arr) - 1
while low <= high:
# Avoid integer overflow: low + (high - low) // 2
mid = low + (high - low) // 2
if arr[mid] == target:
return mid # Target found at index mid
elif arr[mid] < target:
low = mid + 1 # Search right half
else:
high = mid - 1 # Search left half
return -1 # Not found
# Example
data = [2, 5, 8, 12, 16, 23, 38, 56, 72, 91]
print("Index of 23:", binary_search(data, 23))Index of 23: 5
2Divide & Conquer: Merge Sort vs Quick Sort
Merge Sortdivides an array into two halves, recursively sorts both, and then merges the sorted halves in time. It guarantees time in all casesand isstable, but requires auxiliary memory.
Quick Sortselects apivot elementand partitions the array such that all smaller elements reside on the left and larger on the right. It operatesin-place( auxiliary stack memory) and runs in average , but degrades to when the pivot is poorly chosen.
Merge Sort Execution Pipeline
- 1
Divide
Compute mid = low + (high - low) / 2 and split array into left and right sub-arrays
- 2
Conquer
Recursively invoke mergeSort(left) and mergeSort(right) until base size 1 is reached
- 3
Combine / Merge
Merge the two sorted arrays into a single sorted array using two pointer comparisons in O(n)
3Algorithmic Complexity & Stability Master Table
Sorting Algorithms Master Benchmark Matrix
| Algorithm | Best Time | Average Time | Worst Time | Worst Space | Stable? |
|---|---|---|---|---|---|
| Bubble Sort | O(n) (optimized) | O(n²) | O(n²) | O(1) | Yes |
| Selection Sort | O(n²) | O(n²) | O(n²) | O(1) | No |
| Insertion Sort | O(n) | O(n²) | O(n²) | O(1) | Yes |
| Merge Sort | O(n log n) | O(n log n) | O(n log n) | O(n) | Yes |
| Quick Sort | O(n log n) | O(n log n) | O(n²) | O(log n) | No |
| Heap Sort | O(n log n) | O(n log n) | O(n log n) | O(1) | No |
Bubble Sort
- Best Time
- O(n) (optimized)
- Average Time
- O(n²)
- Worst Time
- O(n²)
- Worst Space
- O(1)
- Stable?
- Yes
Selection Sort
- Best Time
- O(n²)
- Average Time
- O(n²)
- Worst Time
- O(n²)
- Worst Space
- O(1)
- Stable?
- No
Insertion Sort
- Best Time
- O(n)
- Average Time
- O(n²)
- Worst Time
- O(n²)
- Worst Space
- O(1)
- Stable?
- Yes
Merge Sort
- Best Time
- O(n log n)
- Average Time
- O(n log n)
- Worst Time
- O(n log n)
- Worst Space
- O(n)
- Stable?
- Yes
Quick Sort
- Best Time
- O(n log n)
- Average Time
- O(n log n)
- Worst Time
- O(n²)
- Worst Space
- O(log n)
- Stable?
- No
Heap Sort
- Best Time
- O(n log n)
- Average Time
- O(n log n)
- Worst Time
- O(n log n)
- Worst Space
- O(1)
- Stable?
- No
Definition
Algorithm Stability — A sorting algorithm is stable if two elements with equal keys appear in the sorted output in the exact same relative order as they were in the unsorted input.
Remember
- Binary Search requires a sorted array and executes in O(log n) time by halving search intervals.
- Merge Sort guarantees O(n log n) in best, average, and worst cases, and is stable, but requires O(n) auxiliary space.
- Quick Sort sorts in-place with O(n log n) average time, making it the fastest general-purpose internal sort in practice.
- Insertion Sort is exceptionally fast O(n) for small or nearly sorted collections and forms the basis of TimSort.
Check yourself
Question 1
Which sorting algorithm has a worst-case time complexity of O(n log n) and requires O(1) auxiliary space?Question 2
Why is preferred over in Binary Search?