Search_Sorting_Algorithms_Study_Notes
Search_Sorting_Algorithms_Study_Notes
Binary Search
Binary Search is a more efficient searching algorithm that requires a sorted list as
input. It works by repeatedly dividing the search space in half, comparing the
middle element with the target value, and eliminating half of the remaining
elements based on the comparison. This divide-and-conquer approach gives
Binary Search a time complexity of O(log n), making it much faster than Linear
Search for large datasets.
Bubble Sort
Bubble Sort is a simple sorting algorithm that repeatedly steps through the list,
compares adjacent elements, and swaps them if they are in the wrong order.
While easy to understand and implement, it has a time complexity of O(n²),
making it inefficient for large datasets. The algorithm gets its
name from the way smaller elements "bubble" to the top of the list with each
iteration.
Selection Sort
Selection Sort is a sorting algorithm that works by repeatedly finding the minimum
element from the unsorted portion of the list and placing it at the beginning. The
algorithm maintains two subarrays: one that is sorted and one that is unsorted.
Like Bubble Sort, Selection Sort has a time complexity of O(n²), but it generally
performs better than Bubble Sort as it makes fewer swaps.
Time Complexity: O(n ² ) Quadratic
Insertion Sort
Insertion Sort is a sorting algorithm that builds the final sorted array one element
at a time. It works by taking elements from the unsorted part and inserting them
into their correct position in the sorted part of the array. While it also has a time
complexity of O(n²), Insertion Sort performs well on small datasets and is
particularly efficient when dealing with arrays that are already partially sorted.
Time Complexity: O(n ² ) Quadratic
Watch this 2-min video: https://youtu.be/JU767SDMDvA?si=i_MQ_hXZdxvJ2R7t
Merge Sort
Merge Sort is a highly efficient divide-and-conquer sorting algorithm that works
by dividing the array into smaller subarrays, sorting them, and then merging them
back together. It consistently performs well with a time complexity of O(n log n),
making it much faster than the simpler sorting algorithms for large datasets.
Although it requires additional memory space, Merge Sort is stable and
predictable, making it a popular choice for sorting linked lists and in situations
where stable sorting is required.
Time Complexity: O(n log n) Linearithmic
Bucket Sort
Bucket Sort is a distribution-based sorting algorithm that works by distributing
elements into a number of buckets, then sorting these buckets individually. It
assumes uniform distribution of the input data across buckets and performs well
when this assumption holds true. The algorithm has an average time complexity of
O(n + k), where k is the number of buckets.
Heap Sort
Heap Sort is a comparison-based sorting algorithm that uses a binary heap data
structure to sort elements. It works by first building a max-heap from the input
array, then repeatedly extracting the maximum element and rebuilding the heap
until all elements are sorted. While it has a consistent time complexity of O(n log
n) and sorts in-place, it typically performs slower than Quick Sort in practice due
to poor cache performance.
Time Complexity: O(n log n) Linearithmic