## DEV Community

Madhav Ganesan

Posted on • Updated on • Originally published at madhavganesan.hashnode.dev

# Bubble Sort

## Time Complexity:

Best: O(n2) (when the array is already sorted)
Average: O(n2)
Worst: O(n2)
Space Complexity: O(1)

``````#include <iostream>
#include <vector>
using namespace std;

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

int main() {
vector<int> arr = {64, 34, 25, 12, 22, 11, 90};
bubbleSort(arr);
for (int num : arr) {
cout << num << " ";
}
return 0;
}
``````

# Selection Sort

## Time Complexity:

Best: O(n2)
Average: O(n2)
Worst: O(n2)
Space Complexity: O(1)

``````#include <iostream>
#include <vector>
using namespace std;

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

int main() {
vector<int> arr = {64, 25, 12, 22, 11};
selectionSort(arr);
for (int num : arr) {
cout << num << " ";
}
return 0;
}
``````

# Insertion Sort

## Time Complexity:

Best: O(n) (when the array is already sorted)
Average: O(n2)
Worst: O(n2)
Space Complexity: O(1)

``````#include <iostream>
#include <vector>
using namespace std;

void insertionSort(vector<int>& arr) {
int n = arr.size();
for (int i = 1; i < n; ++i) {
int key = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
--j;
}
arr[j + 1] = key;
}
}
``````

# Merge Sort

## Time Complexity:

Best: O(nlogn)
Average: O(nlogn)
Worst: O(nlogn)
Space Complexity: O(n)

``````#include <iostream>
#include <vector>
using namespace std;

void merge(vector<int>& arr, int l, int m, int r) {
int n1 = m - l + 1;
int n2 = r - m;
vector<int> L(n1);
vector<int> R(n2);

for (int i = 0; i < n1; ++i)
L[i] = arr[l + i];
for (int j = 0; j < n2; ++j)
R[j] = arr[m + 1 + j];

int i = 0, j = 0, k = l;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k++] = L[i++];
} else {
arr[k++] = R[j++];
}
}

while (i < n1) {
arr[k++] = L[i++];
}

while (j < n2) {
arr[k++] = R[j++];
}
}

void mergeSort(vector<int>& arr, int l, int r) {
if (l < r) {
int m = l + (r - l) / 2;
mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);
merge(arr, l, m, r);
}
}
``````

# Quick Sort

## Time Complexity:

Best: O(nlogn)
Average: O(nlogn)
Worst: O(n2) (when the pivot is the smallest or largest element)
Space Complexity: O(logn) (due to recursion stack)

``````#include <iostream>
#include <vector>
using namespace std;

int partition(vector<int>& arr, int low, int high) {
int pivot = arr[high];
int i = low - 1;
for (int j = low; j < high; ++j) {
if (arr[j] <= pivot) {
++i;
swap(arr[i], arr[j]);
}
}
swap(arr[i + 1], arr[high]);
return i + 1;
}

void quickSort(vector<int>& arr, int low, int high) {
if (low < high) {
int pi = partition(arr, low, high);
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}
``````

# Heap Sort

## Time Complexity:

Best: O(nlogn)
Average: O(nlogn)
Worst: O(nlogn)
Space Complexity: O(1)

``````#include <iostream>
#include <vector>
using namespace std;

void heapify(vector<int>& arr, int n, int i) {
int largest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;

if (left < n && arr[left] > arr[largest])
largest = left;

if (right < n && arr[right] > arr[largest])
largest = right;

if (largest != i) {
swap(arr[i], arr[largest]);
heapify(arr, n, largest);
}
}

void heapSort(vector<int>& arr) {
int n = arr.size();

for (int i = n / 2 - 1; i >= 0; --i)
heapify(arr, n, i);

for (int i = n - 1; i >= 0; --i) {
swap(arr[0], arr[i]);
heapify(arr, i, 0);
}
}
``````