Sorting Flashcards
Bubble Sort
Iterates over every element, comparing each pair and swapping if necessary. No more iterations are needed once no swaps take place. It has high time complexity O(n^2) best case but can be improved to O(n) if given sorted list.
Selection Sort
Iterates over each element and swaps it with the smallest element int the remaining list. Works well for small sets. Time complexity is O(n^2) best case.
Insertion Sort
Every repetition removes an element from the input, and inputs into the correct position. Best when the input data is nearly sorted or the input size is small. Time complexity is O(n^2) worst case and O(n) best case.
Shell Sort
It is an improvement of insertion sort and is faster than bubble, selection, and insertion sorting. Time complexity is O(nlogn) worst case and O(n) best case.
Merge Sort
The list is divided into two parts and these are solved recursively, then they are merged.Used for sorting a linked list. It is insensitive to the initial ordering of the list. Time complexity is O(nlogn) best and worst case.
Heap Sort
Used to sort priority queues. Time complexity is O(nlogn) best and worst case.
Quicksort
It is an example of a divide-and-conquer algorithm. It is also called partition exchange sort. The array is divided into two, and the two sub-arrays are sorted by recursive calls. In the recursion, if there are 1 or no items in the array, return. Else, an element is picked at random to use as the “pivot”. This array is in turn split into 2. Time complexity is O(n^2) worst case and O(nlogn) best case.
Tree Sort
Each element is scanned and placed into the proper position of the binary search tree. Time complexity is O(n^2) worst case and O(nlogn) best case.
Counting Sort
Assumes that each element is an integer in the range of 1 to K. Time complexity of O(n) in best and worst cases.
Bucket/Bin Sort
Time complexity of O(n) in best and worst cases.
Radix Sort
Time complexity of O(n) in best and worst cases.
Topological Sort
Used to sort graphs.
External Sorting
Uses another sort algorithm but allows to sort under memory constraints.