Unit 4: Searching and Sorting

Table of Contents

1. Searching

Searching is the process of finding a particular element (the "key") within a collection of elements (like an array or list).

Linear Search

This is the simplest search algorithm. It sequentially checks each element in the list until the target key is found or the end of the list is reached.

Binary Search

This is a much more efficient search algorithm, but it has one major prerequisite: the list must be sorted.

It works on the "Divide and Conquer" principle.

Common Mistake: A common bug is integer overflow when calculating the midpoint. mid = low + (high - low) / 2 is a safer way to write mid = (low + high) / 2 for very large arrays.

Comparison of Linear and Binary Search

Feature Linear Search Binary Search
Prerequisite None. Works on sorted or unsorted data. Data must be sorted.
Time Complexity O(n) (linear) O(log n) (logarithmic)
Speed Slow for large datasets. Very fast, even for large datasets.
Data Structure Works on arrays and linked lists. Works efficiently only on arrays (for O(1) access to the mid element).

2. Sorting

Sorting is the process of arranging elements in a collection in a specific order (e.g., ascending or descending).

Key Sorting Concepts (Stable vs. Unstable)

Bubble Sort

The simplest sorting algorithm. It repeatedly steps through the list, compares adjacent elements, and swaps them if they are in the wrong order. This "bubbles" the largest elements to the end.

Selection Sort

This algorithm divides the list into a sorted sublist (at the beginning) and an unsorted sublist.

Selection Sort is useful when memory writes are "expensive," as it performs the minimum possible number of swaps (O(n) swaps).

Insertion Sort

Builds the final sorted array one item at a time. It's like sorting a hand of playing cards. It takes an element from the unsorted part and "inserts" it into its correct position in the sorted part.

Insertion sort is very efficient for small datasets and for datasets that are almost sorted.

Merge Sort

A "Divide and Conquer" algorithm. It's one of the most efficient sorting algorithms.

Quick Sort

Another "Divide and Conquer" algorithm. It is generally the fastest sorting algorithm in practice.

The worst case (O(n²)) is a major drawback. This can be mitigated by choosing a good pivot, such as a random element or the "median-of-three."

Comparison of Sorting Techniques

Algorithm Time (Best) Time (Average) Time (Worst) Space Stable
Bubble Sort O(n) 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
Key Takeaway: