Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

The Sound of Sorting Algorithm Cheat Sheet, Cheat Sheet of Data Structures and Algorithms

Quick and useful cheat sheet on Sorting Algorithms in data structure

Typology: Cheat Sheet

2019/2020

Uploaded on 10/09/2020

rakshan
rakshan 🇺🇸

4.6

(16)

6 documents

Partial preview of the text

Download The Sound of Sorting Algorithm Cheat Sheet and more Cheat Sheet Data Structures and Algorithms in PDF only on Docsity! The Sound of Sorting Algorithm Cheat Sheet Function selectionSort(A : Array of Element; n : N) for i := 1 to n do min := i for j := i + 1 to n do // find smallest element if A[j] < A[min] then min := j endfor swap(A[i], A[min]) // swap element to the beginning invariant A[1] ≤ · · · ≤ A[i] endfor Function insertionSort(A : Array of Element; n : N) for i := 2 to n do // {A[1]} is sorted j := i while (j > 0) & (A[j − 1] > A[j]) // find right position j swap(A[j − 1], A[j]) // move larger elements to the back j := j − 1 endwhile invariant A[1] ≤ · · · ≤ A[i] endfor Function mergeSort(A : Array of Element; lo, hi : N) if hi− lo ≤ 1 then return // base case mid := (lo + hi)/2 // middle element mergeSort(lo, mid), mergeSort(mid, hi) // sort halves B := allocate (Array of Element size hi− lo) i := lo, j := mid, k := 1 // running indexes while (i < mid) & (j < hi) if A[i] < A[j] B[k++] := A[i++] // merge! else B[k++] := A[j++] endwhile while i < mid do B[k++] := A[i++] // copy remainder while j < hi do B[k++] := A[j++] A[lo, . . . , hi− 1] := B[1, . . . , (hi− lo)] // copy back dispose (B) Procedure bubbleSort(A : Array [1 . . . n] of Element) for i := 1 to n do for j := 1 to n− i do if A[j] > A[j + 1] then // If right is smaller, swap(A[j], A[j + 1]) // move to the right Procedure heapSort(A : Array [1 . . . n] of Element) buildHeap(A) // construct a max-heap in the array while n > 1 swap(A[1], A[n]) // take maximum n := n− 1 // shrink heap area in array siftDown(A, 1) // correctly order A[1] in heap Procedure buildHeap(A : Array [1 . . . n] of Element) for i := bn/2c downto 1 do siftDown(i) // reestablish max-heap invariants Procedure siftDown(A : Array [1 . . . n] of Element; i : N) if 2i > n then return k := 2i // select right or left child if (2i + 1 ≤ n) & (A[2i] ≤ A[2i + 1]) then // find smaller child k := k + 1 if A[i] < A[k] then // if child is larger than A[i], swap(A[i], A[k]) // switch with child siftDown(A, k) // and move element further down Procedure cocktailShakerSort(A : Array [1 . . . n] of Element) lo := 1, hi := n, mov := lo while lo < hi do for i := hi downto lo + 1 do if A[i− 1] > A[i] then // move smallest element in swap(A[i− 1], A[i]), mov := i // A[hi..lo] to A[lo] endfor lo := mov for i := lo to hi− 1 do if A[i] > A[i + 1] then // move largest element in swap(A[i], A[i + 1]), mov := i // A[lo..hi] to A[hi] endfor hi := mov Procedure gnomeSort(A : Array [1 . . . n] of Element) i := 2 while i ≤ n do if A[i] ≥ A[i− 1] then // move to right while i++ // elements grow larger else swap(A[i], A[i− 1]) // swap backwards while if i > 2 then i−− // element grow smaller endwhile 1 http://panthema.net/2013/sound-of-sorting
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved