Sorting and Searching

Sorting

Bubble Sort

  • How it works: Bubble Sort repeatedly compares adjacent elements and swaps them if they are in the wrong order. It continues this process until no more swaps are needed.

  • Time Complexity: O(n^2) in the worst and average cases, where 'n' is the number of elements.

  • Space Complexity: O(1) - Bubble Sort is an in-place sorting algorithm, which means it doesn't require additional memory for sorting.

def bubble_sort(arr):
    n = len(arr)
    for i in range(n):
        for j in range(n-i-1):
            if arr[j] > arr[j+1]:
                arr[j], arr[j+1] = arr[j+1], arr[j]

Insertion Sort

  • How it works: Insertion Sort builds the final sorted array one item at a time by taking an element from the unsorted part and inserting it into its correct position within the sorted part.

  • Time Complexity: O(n^2) in the worst and average cases.

  • Space Complexity: O(1) - Like the previous two, Insertion Sort is an in-place sorting

def insertion_sort(arr):
    n = len(arr)
    for i in range(1, n):
        j = i-1
        item = arr[i]
        while j >= 0 and arr[j] > arr[j+1]:
            arr[j], arr[j+1] = arr[j+1], arr[j]
            j -= 1
        arr[j+1] = item

Selection Sort

  • How it works: Selection Sort divides the input into two parts: a sorted part and an unsorted part. It repeatedly selects the minimum element from the unsorted part and appends it to the sorted part.

  • Time Complexity: O(n^2) in the worst and average cases.

  • Space Complexity: O(1) - Selection Sort is also an in-place sorting algorithm.

Quick Sort

  • How it works: Quick Sort also uses a divide-and-conquer approach. It selects a pivot element and partitions the array into two subarrays: one with elements less than the pivot and one with elements greater than the pivot. It then recursively sorts the subarrays.

  • Time Complexity: O(n^2) in the worst case (rare), but O(n log n) on average and in the best case.

  • Space Complexity: O(log n) - Quick Sort is often in-place, and the space complexity is determined by the recursion stack.

Merge Sort

  • How it works: Merge Sort is a divide-and-conquer algorithm. It divides the unsorted list into smaller sublists, sorts them, and then merges the sorted sublists until the entire list is sorted.

  • Time Complexity: O(n log n) in the worst, average, and best cases.

  • Space Complexity: O(n) - Merge Sort typically requires additional space for the merging step, making it not entirely in-place.

Heap Sort

  • How it works: Heap Sort uses a binary heap data structure to repeatedly extract the maximum element (for ascending order) and place it at the end of the sorted portion of the array.

  • Time Complexity: O(n log n) in all cases.

  • Space Complexity: O(1) - Heap Sort is an in-place sorting algorithm.

Searching

Last updated