Sorting Algorithms Cheat Sheet by Pryl Via Cheatography.Com/66402/Cs/16808
Total Page:16
File Type:pdf, Size:1020Kb
Sorting algorithms Cheat Sheet by pryl via cheatography.com/66402/cs/16808/ Sorting algorithms and Methods Merge sort Sorting algorit hms Metho ds function merge_sort(list m) Bubble sort Exchanging // Base case. A list of zero or one elements is sorted, by definit ion. Heapsort Selection if length of m ≤ 1 then Insertion sort Insertion r eturn m Introsort Partiti oning & Selection // Recursive case. First, divide the list into Merge sort Merging equal-s ized sublists Patience sorting Insertion & Selection // consisting of the first half and second half of Quicksort Partiti oning the list. Selection sort Selection // This assumes lists start at index 0. Timsort Insertion & Merging var left := empty list var right := empty list Unshuffle sort Distrib ution and Merge for each x with index i in m do if i < (length of m)/2 then Best and Worst Case add x to left Algor ithms Best Case Worst Case else Bogosort n ∞ add x to right Bubble sort n n2 // Recursi vely sort both sublists. Bucket sort (uniform keys) - n2k left := merge_s ort (left) r ight := merge_s ort (ri ght) Heap sort n log n n log n // Then merge the now-sorted sublists. Insertion sort n n2 r eturn merge(l eft, right) Merge sort n log n n log n 2 Quick sort n log n n Bogosort Selection sort n2 n2 while not isInOrder(deck): Shell sort n log n n4/3 s huf fle (deck) Spreadsort n n(k/s+d) Timsort n n log n Bucket sort Unshuffle sort n kn function bucketSort(array, n) is b uckets ← new array of n empty lists Insertion sort for i = 0 to (length (ar ray )-1) do function insertionSortR(array A, int n) i nsert array[i] into buckets [ms bit s(a rra y[i], k)] if n>0 for i = 0 to n - 1 do i nse rti onS ort R(A ,n-1) n ext Sor t(b uck ets [i]); x ← A[n] r eturn the concate nation of buckets [0], ...., j ← n-1 buckets [n-1] w hile j >= 0 and A[j] > x A [j+1] ← A[j] Resources j ← j-1 https:/ /en .wi kip edi a.or g/ wik i/S ort ing _al gor ith m#C omp ari son _of _al gor ithms end while http:// big och eat she et.com A [j+1] ← x end if end function By pryl Published 27th August, 2018. Sponsored by ApolloPad.com cheatography.com/pryl/ Last updated 27th August, 2018. Everyone has a novel in them. Finish Yours! Page 1 of 2. https://apollopad.com Sorting algorithms Cheat Sheet by pryl via cheatography.com/66402/cs/16808/ Sorting algorithm complex ities Bubble sort (cont) Algor ithms Average Case Memory end procedure complex ity Bitonic sorter log2 n n log2 n Quicksort Bogosort n × n! 1 algorithm quicksort(A, lo, hi) is Bubble sort n2 1 if lo < hi then p := partiti on(A, lo, hi) Bucket sort (uniform n+k nk keys) q uic kso rt(A, lo, p - 1 ) q uic kso rt(A, p + 1, hi) Burstsort n(k/d) n(k/d) algorithm partiti on(A, lo, hi) is Counting sort n+r n+r p ivot := A[hi] Heap sort n log n 1 i := lo Insertion sort n2 1 for j := lo to hi - 1 do Introsort n log n log n if A[j] < pivot then LSD Radix Sort n(k/d) n+2d swap A[i] with A[j] Merge sort n log n n i := i + 1 swap A[i] with A[hi] MSD Radix Sort (in- n(k/d) 2d place) return i Patience sort - n Selection sort Pigeonhole sort n+2k 2k procedure selection sort Quicksort n log n log n list : array of items Selection sort n2 1 n : size of list Shell sort Depends on gap 1 for i = 1 to n - 1 sequence / set current element as minimum / Spaghetti sort n n2 min = i Spreadsort n(k/d) (k/d)2d Stooge sort n(log 3/log1.5) n / check the element to be minimum / for j = i+1 to n Timsort n log n n if list[j] < list[min] then Bubble sort min = j; end if procedure bubbleSort( A : list of sortable items ) end for n = length(A) / swap the minimum element with the current r epeat element/ s wapped = false if indexMin != i then for i = 1 to n-1 inclusive do swap list[min] and list[i] if A[i-1] > A[i] then end if s wap (A[ i-1], A[i]) end for s wapped = true end if end procedure end for n = n - 1 u ntil not swapped By pryl Published 27th August, 2018. Sponsored by ApolloPad.com cheatography.com/pryl/ Last updated 27th August, 2018. Everyone has a novel in them. Finish Yours! Page 2 of 2. https://apollopad.com.