'%3E%0A%3Cpath fill-rule='evenodd' d='M0 0H1466.6V825H0V0Z' class='g0'/%3E%0A%3Cpath fill-rule='evenodd' d='M0 0H1466.7V825H0V0Z' class='g0'/%3E%0A%3C/g%3E%0A%3Cpath d='M468.9 378.7H997.6m-528.3-.4V584.4M574.9 378.3V584.4M680.5 378.3V584.4M786.1 378.3V584.4M891.6 378.3V584.4M997.2 378.3V584.4M468.9 406.2H997.6M468.9 453.9H997.6M468.9 481.4H997.6M468.9 529H997.6M468.9 556.5H997.6M468.9 584H997.6' class='g1'/%3E%0A%3Cpath fill-rule='evenodd' d='M781 433.6H469.4v-55h623.3v55H781Z' class='g0'/%3E%0A%3C/svg%3E)
Algorithm Time Space Stable In-place
Selection
Sort
O(n^2) O(1) No Yes
Bubble Sort O(n^2) O(1) Yes Yes
Insertion
Sort
O(n^2) O(1) Yes Yes
Merge Sort O(n log n) O(n) Yes No
Quick Sort O(n log n) O(log n) No Yes
Quick Sort.
Selecon Sort
Selection sort repeatedly finds the smallest element from the unsorted part of the
array and places it at the beginning.
Steps: start at the first position, find the minimum in the unsorted part, swap it with
the current position, then move forward.
Example: 64, 25, 12, 22, 11 -> 11, 25, 12, 22, 64 -> 11, 12, 25, 22, 64 -> 11, 12, 22,
25, 64 -> 11, 12, 22, 25, 64.
Time complexity: best, average, worst = O(n^2). Space: O(1). Stable: usually no.
In-place: yes.
Bubble Sort
Bubble sort compares adjacent elements and swaps them if they are in the wrong
order.
After each pass, the largest unsorted element moves to the end.
Steps: compare neighbors, swap if needed, repeat passes until no swaps occur.
Time complexity: best = O(n) with optimization, average and worst = O(n^2).
Space: O(1). Stable: yes. In-place: yes.
Inseron Sort
Insertion sort builds a sorted portion of the array one element at a time.
Steps: start from the second element, store it as key, shift larger elements right,
and insert key in the correct position.
Example: 12, 11, 13, 5, 6 -> 11, 12, 13, 5, 6 -> 11, 12, 13, 5, 6 -> 5, 11, 12, 13, 6 ->
5, 6, 11, 12, 13.
Time complexity: best = O(n), average and worst = O(n^2). Space: O(1). Stable:
yes. In-place: yes.
Merge Sort
Merge sort uses divide and conquer.
It splits the array into halves, recursively sorts each half, and then merges the
sorted halves.
Example: 38, 27, 43, 10 -> split into halves -> sort each half -> merge into 10, 27,
38, 43.
Time complexity: best, average, worst = O(n log n). Space: O(n). Stable: yes. In-
place: no, usually.
Quick Sort
Quick sort uses divide and conquer and a pivot element.
It partitions the array so smaller elements go left of the pivot and larger elements
go right, then sorts both parts recursively.
Example: 10, 7, 8, 9, 1, 5 with pivot 5 is partitioned, then left and right parts are
sorted recursively.
Time complexity: best and average = O(n log n), worst = O(n^2). Space: O(log n)