Sorting Algorithms I šŸ“š

Agenda

  1. Bubble Sort
  2. Selection Sort
  3. Insertion Sort
  4. Count Sort
  5. Radix Sort

1. Bubble Sort šŸŒ€

Time Complexity: O(n²)

void bubbleSort(vector<int>& arr) {
    for (int i = 0; i < arr.size(); i++) {
        for (int j = 0; j < arr.size() - 1 - i; j++) {
            if (arr[j] > arr[j + 1]) {
                swap(arr[j], arr[j + 1]);
            }
        }
    }
}

2. Selection Sort 🧳

Time Complexity: O(n²)

void selectionSort(vector<int>& arr) {
    for (int i = 0; i < arr.size(); i++) {
        int minIndex = i;
        for (int j = i + 1; j < arr.size(); j++) {
            if (arr[j] < arr[minIndex]) {
                minIndex = j;
            }
        }
        swap(arr[i], arr[minIndex]);
    }
}