Sorting Flashcards
(6 cards)
Bubble sort
iterates through array and swaps adjacent numbers if they’re not in order
keeps iterating until we have an iteration with no swaps
O(n^2)
Insertion sort
build sorted array, shifting elements into the right place as we iterate through once
O(n^2)
Selection sort
split into sorted and unsorted parts
find smallest element in array and add to end of the already sorted part
O(n^2)
Merge sort
recursively sort left and right parts of array, merge them together
O(n log n) – space: O(n)
Heap Sort
convert unsorted array into max heap
(parent - A[i/2], left - A[2i], right - A[2i + 1])
swap first element with last element and remove from heap
heapify swapped element and repeat
O(n log n)
Quick Sort
choose pivot and swap to end of list
elements smaller than pivot move to the left, elements larger than pivot stay to the right
swap pivot point with counter
repeat of left and right parts
worst O(n^2) – average O(n log n)