Sorting Algorithms

My first python class was actually very helpful. Link to various sorting algorithms implemented in Python.

AlgorithmBest Case(Ω)Average Case(Θ)Worst Case(O)
Selection Sort
Insertion Sort
Bubble Sort
Merge Sort
Quick Sort
Heap Sort
Radix Sort
Counting Sort
Bucket Sort

https://www.bigocheatsheet.com/

Merge Sort Vs. Quick Sort

  • Quicksort is likely faster in practice, but merge sort is faster on paper.
  • Merge Sort divides the set into the smallest possible groups immediately then reconstructs the incrementally as it sorts the groupings.
  • Quicksort continually partitions the data set by a pivot, until the set is recursively sorted

Custom Sorting in C++

You can do something like the following

sort(begin(arr), end(arr), [](string &s1, string &s2){ return s1+s2>s2+s1; });

For a C++ Vector,

sort(v.begin(), v.end());

Using the greater operator

sort(arr, arr + n, greater<int>());