SORTING AND SEARCHING ALGORITHMS
Sorting and Searching Algorithms
1. Basic Sorting Algorithms
• Bubble Sort
o Concept: Repeatedly steps through the
list, compares adjacent elements, and
swaps them if they are in the wrong
order.
o Time Complexity:
▪ Best Case: O(n) (when the array is
already sorted)
▪ Worst and Average Case: O(n²)
o Characteristics: Simple but inefficient for
large datasets.
• Selection Sort
o Concept: Divides the list into two parts:
the sorted part and the unsorted part. It
repeatedly selects the smallest (or
largest) element from the unsorted part
and moves it to the end of the sorted
part.
o Time Complexity: O(n²) for all cases.
o Characteristics: Simple and performs well
on small lists, but inefficient for large
datasets.
• Insertion Sort
o Concept: Builds the sorted array one
element at a time by repeatedly taking
the next element from the unsorted part
and inserting it into the correct position
within the sorted part.
o Time Complexity:
▪ Best Case: O(n) (when the array is
already sorted)
▪ Worst and Average Case: O(n²)
o Characteristics: Efficient for small or
nearly sorted datasets.
2. Divide and Conquer Algorithms
• Merge Sort
o Concept: Recursively splits the array in
half, sorts each half, and then merges the
sorted halves.
o Time Complexity: O(n log n) for all cases.
o Characteristics: Stable sorting algorithm
with good performance on large datasets.
Requires additional space for the
temporary arrays.
• Quick Sort
o Concept: Picks a "pivot" element and
partitions the array into two subarrays:
elements less than the pivot and
elements greater than the pivot. It then
recursively sorts the subarrays.
o Time Complexity:
▪ Best and Average Case: O(n log n)
▪ Worst Case: O(n²) (when the pivot is
the smallest or largest element)
o Characteristics: In-place sorting
algorithm with high performance in
practice. The choice of pivot can greatly
affect performance.
• Binary Search
o Concept: Searches for a target value
within a sorted array by repeatedly
dividing the search interval in half.
o Time Complexity: O(log n)
o Characteristics: Efficient for searching in
sorted arrays but requires the array to be
sorted beforehand.
3. Linear Time Sorting
• Counting Sort
o Concept: Counts the number of
occurrences of each unique element in
the input array. Uses this count to
determine the position of each element
in the output array.
o Time Complexity: O(n + k) where n is the
number of elements and k is the range of
input values.
o Characteristics: Non-comparative sorting
algorithm, efficient when the range of
input values (k) is not significantly larger
than the number of elements.
• Radix Sort
o Concept: Sorts numbers by processing
individual digits. It typically uses counting
sort as a subroutine to sort the digits.
o Time Complexity: O(d * (n + k)), where d
is the number of digits in the largest
number.
o Characteristics: Non-comparative and
works well for sorting integers with a
large number of digits.
• Bucket Sort
o Concept: Divides the input array into
several "buckets" and then sorts each
bucket individually (often using another
sorting algorithm like insertion sort).
o Time Complexity: O(n + k) in the average
case, where n is the number of elements
and k is the number of buckets.
o Characteristics: Efficient when the input
is uniformly distributed over a range.