SlideShare a Scribd company logo
1 of 2
Download to read offline
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
Procedure quickSort(A : Array of Element; `, r : N)
if ` ≥ r then return
q := pickPivotPos(A, `, r)
m := partition(A, `, r, q)
quickSort(A, `, m − 1), quickSort(A, m + 1, r)
Function partition(A : Array of Element; `, r : N, q : N)
p := A[q] // pivot element
swap(A[q], A[r]) // swap to the end
i := `
invariant ≤ p > p ? p
` i j r
for j := ` to r − 1 do
if A[j] ≤ p then
swap(A[i], A[j]), i++ // move smaller to the front
assert ≤ p > p p
` i r
swap(A[i], A[r]) // move pivot into the middle
assert ≤ p p > p
` i r
return i
Procedure quickSortTernary(A : Array of Element; `, r : N)
if ` ≥ r then return
q := pickPivotPos(A, `, r)
(m, m0
) := partitionTernary(A, `, r, q)
quickSortTernary(A, `, m − 1), quickSortTernary(A, m0
+ 1, r)
Function partitionTernary(A : Array of Element; `, r : N; q : N)
p := A[q] // pivot element
i := `, j := `, k := r
invariant < p > p ? = p
` i j k r
while (j ≤ k) // three-way comparison
if A[j] = p then swap(A[j], A[k]), k−− ;
else if A[j] < p then swap(A[j], A[i]), i++ , j++ ;
else j++ ;
assert < p > p = p
` i k r
i0
:= i + r − k + 1
swap(A[i . . . i0
], A[k + 1 . . . r]) // move = p area to the middle
assert < p = p > p
` i i0 r
return (i, i0
)
Procedure LSDRadixSort(A : Array [1 . . . n] of Element)
K := 4 // number of buckets per round
D := dlogK(max{A[i] + 1 | i = 1, . . . , n})e // calculate number of rounds
B := allocate (Array of Element size n) // temporary array B
for d := 0 to D − 1 do // sort by the d-th K-digit.
redefine key(x) := (x div Kd
) mod K
KSortCopy(A, B, n), swap(A, B) // sort from A to B, and swap back
invariant A ist nach den K-Ziffern d..0 sortiert.
dispose (B)
Procedure KSortCopy(A, B : Array [1 . . . n] of Element; K : N)
c = h0, . . . , 0i : Array [0 . . . K − 1] of N
for i := 1 to n do c[key(A[i])]++ // count occurrences
sum := 1
for k := 0 to K − 1 do // exclusive prefix sum
next := sum + c[k], c[k] := sum, sum := next
for i := 1 to n do
B

c[key(A[i])]++

:= A[i] // move element A[i] into bucket of B
Procedure MSDRadixSort(A : Array [1 . . . n] of Element)
K := 4 // number of buckets per round
D := dlogK(max{A[i] + 1 | i = 1, . . . , n})e // count number of round
MSDRadixSortRec(A, D − 1, K)
Procedure MSDRadixSortRec(A : Array [1 . . . n] of Element; d, K : N)
c = h0, . . . , 0i : Array [0 . . . K − 1] of N // KSort with in-place permuting
redefine key(x) := (x div Kd
) mod K
for i := 1 to n do c[key(A[i])]++ // count occurrences
b = h0, . . . , 0i : Array [0 . . . K] of N
sum := 1
for k := 0 to K do // inclusive prefix sum into b
sum := sum + c[k], b[k] := sum
assert b[K] = n
for i := 1 to n do
while j := −− b[key(A[i])]

 i // walk on cycles until i
swap(A[i], A[j]) // move A[i] into right bucket
i := i + c[key(A[i])] // bucket of A[i] is finished
invariant A ist nach den K-Ziffern d..(D − 1) sortiert
if d = 0 return // done?
for k := 0 to K − 1 do // recursion into each of the K buckets if
if c[k]  1 // it contains two or more elements
MSDRadixSortRec(A

b[k] . . . b[k + 1] − 1

, d − 1, K)
dispose (b), dispose (c)
2 http://panthema.net/2013/sound-of-sorting

More Related Content

Similar to SoS-archive-file-of-information-technology.pdf

Advanced Search Techniques
Advanced Search TechniquesAdvanced Search Techniques
Advanced Search TechniquesShakil Ahmed
 
Ch01 basic concepts_nosoluiton
Ch01 basic concepts_nosoluitonCh01 basic concepts_nosoluiton
Ch01 basic concepts_nosoluitonshin
 
Implement the following sorting algorithms Bubble Sort Insertion S.pdf
Implement the following sorting algorithms  Bubble Sort  Insertion S.pdfImplement the following sorting algorithms  Bubble Sort  Insertion S.pdf
Implement the following sorting algorithms Bubble Sort Insertion S.pdfkesav24
 
Fundamental computing algorithms
Fundamental computing algorithmsFundamental computing algorithms
Fundamental computing algorithmsGanesh Solanke
 
USING JAVAThere are at least two types of nearly sorted array.pdf
USING JAVAThere are at least two types of nearly sorted array.pdfUSING JAVAThere are at least two types of nearly sorted array.pdf
USING JAVAThere are at least two types of nearly sorted array.pdflohithkart
 
Advanced data structure
Advanced data structureAdvanced data structure
Advanced data structureShakil Ahmed
 
Monadologie
MonadologieMonadologie
Monadologieleague
 
Unit6 jwfiles
Unit6 jwfilesUnit6 jwfiles
Unit6 jwfilesmrecedu
 
Graph for Coulomb damped oscillation
Graph for Coulomb damped oscillationGraph for Coulomb damped oscillation
Graph for Coulomb damped oscillationphanhung20
 
하스켈 프로그래밍 입문 3
하스켈 프로그래밍 입문 3하스켈 프로그래밍 입문 3
하스켈 프로그래밍 입문 3Kwang Yul Seo
 
CSE680-07QuickSort.pptx
CSE680-07QuickSort.pptxCSE680-07QuickSort.pptx
CSE680-07QuickSort.pptxDeepakM509554
 
R + Hadoop = Big Data Analytics. How Revolution Analytics' RHadoop Project Al...
R + Hadoop = Big Data Analytics. How Revolution Analytics' RHadoop Project Al...R + Hadoop = Big Data Analytics. How Revolution Analytics' RHadoop Project Al...
R + Hadoop = Big Data Analytics. How Revolution Analytics' RHadoop Project Al...Revolution Analytics
 

Similar to SoS-archive-file-of-information-technology.pdf (20)

Advanced Search Techniques
Advanced Search TechniquesAdvanced Search Techniques
Advanced Search Techniques
 
Ch01 basic concepts_nosoluiton
Ch01 basic concepts_nosoluitonCh01 basic concepts_nosoluiton
Ch01 basic concepts_nosoluiton
 
Divide and Conquer
Divide and ConquerDivide and Conquer
Divide and Conquer
 
Implement the following sorting algorithms Bubble Sort Insertion S.pdf
Implement the following sorting algorithms  Bubble Sort  Insertion S.pdfImplement the following sorting algorithms  Bubble Sort  Insertion S.pdf
Implement the following sorting algorithms Bubble Sort Insertion S.pdf
 
Fundamental computing algorithms
Fundamental computing algorithmsFundamental computing algorithms
Fundamental computing algorithms
 
USING JAVAThere are at least two types of nearly sorted array.pdf
USING JAVAThere are at least two types of nearly sorted array.pdfUSING JAVAThere are at least two types of nearly sorted array.pdf
USING JAVAThere are at least two types of nearly sorted array.pdf
 
Daa chapter5
Daa chapter5Daa chapter5
Daa chapter5
 
Unit6 C
Unit6 C Unit6 C
Unit6 C
 
Tower of Hanoi.ppt
Tower of Hanoi.pptTower of Hanoi.ppt
Tower of Hanoi.ppt
 
Advanced data structure
Advanced data structureAdvanced data structure
Advanced data structure
 
Monadologie
MonadologieMonadologie
Monadologie
 
Unit6 jwfiles
Unit6 jwfilesUnit6 jwfiles
Unit6 jwfiles
 
Disjoint sets
Disjoint setsDisjoint sets
Disjoint sets
 
Graph for Coulomb damped oscillation
Graph for Coulomb damped oscillationGraph for Coulomb damped oscillation
Graph for Coulomb damped oscillation
 
하스켈 프로그래밍 입문 3
하스켈 프로그래밍 입문 3하스켈 프로그래밍 입문 3
하스켈 프로그래밍 입문 3
 
C Language Unit-6
C Language Unit-6C Language Unit-6
C Language Unit-6
 
CSE680-07QuickSort.pptx
CSE680-07QuickSort.pptxCSE680-07QuickSort.pptx
CSE680-07QuickSort.pptx
 
R + Hadoop = Big Data Analytics. How Revolution Analytics' RHadoop Project Al...
R + Hadoop = Big Data Analytics. How Revolution Analytics' RHadoop Project Al...R + Hadoop = Big Data Analytics. How Revolution Analytics' RHadoop Project Al...
R + Hadoop = Big Data Analytics. How Revolution Analytics' RHadoop Project Al...
 
sorting1.pptx
sorting1.pptxsorting1.pptx
sorting1.pptx
 
Sorting2
Sorting2Sorting2
Sorting2
 

Recently uploaded

AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Wonjun Hwang
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxnull - The Open Security Community
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024The Digital Insurer
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Unlocking the Potential of the Cloud for IBM Power Systems
Unlocking the Potential of the Cloud for IBM Power SystemsUnlocking the Potential of the Cloud for IBM Power Systems
Unlocking the Potential of the Cloud for IBM Power SystemsPrecisely
 
APIForce Zurich 5 April Automation LPDG
APIForce Zurich 5 April  Automation LPDGAPIForce Zurich 5 April  Automation LPDG
APIForce Zurich 5 April Automation LPDGMarianaLemus7
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
Bluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdfBluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdfngoud9212
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024BookNet Canada
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions
 

Recently uploaded (20)

AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Unlocking the Potential of the Cloud for IBM Power Systems
Unlocking the Potential of the Cloud for IBM Power SystemsUnlocking the Potential of the Cloud for IBM Power Systems
Unlocking the Potential of the Cloud for IBM Power Systems
 
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptxVulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
 
APIForce Zurich 5 April Automation LPDG
APIForce Zurich 5 April  Automation LPDGAPIForce Zurich 5 April  Automation LPDG
APIForce Zurich 5 April Automation LPDG
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
Bluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdfBluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdf
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
 
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort ServiceHot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping Elbows
 

SoS-archive-file-of-information-technology.pdf

  • 1. 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
  • 2. Procedure quickSort(A : Array of Element; `, r : N) if ` ≥ r then return q := pickPivotPos(A, `, r) m := partition(A, `, r, q) quickSort(A, `, m − 1), quickSort(A, m + 1, r) Function partition(A : Array of Element; `, r : N, q : N) p := A[q] // pivot element swap(A[q], A[r]) // swap to the end i := ` invariant ≤ p > p ? p ` i j r for j := ` to r − 1 do if A[j] ≤ p then swap(A[i], A[j]), i++ // move smaller to the front assert ≤ p > p p ` i r swap(A[i], A[r]) // move pivot into the middle assert ≤ p p > p ` i r return i Procedure quickSortTernary(A : Array of Element; `, r : N) if ` ≥ r then return q := pickPivotPos(A, `, r) (m, m0 ) := partitionTernary(A, `, r, q) quickSortTernary(A, `, m − 1), quickSortTernary(A, m0 + 1, r) Function partitionTernary(A : Array of Element; `, r : N; q : N) p := A[q] // pivot element i := `, j := `, k := r invariant < p > p ? = p ` i j k r while (j ≤ k) // three-way comparison if A[j] = p then swap(A[j], A[k]), k−− ; else if A[j] < p then swap(A[j], A[i]), i++ , j++ ; else j++ ; assert < p > p = p ` i k r i0 := i + r − k + 1 swap(A[i . . . i0 ], A[k + 1 . . . r]) // move = p area to the middle assert < p = p > p ` i i0 r return (i, i0 ) Procedure LSDRadixSort(A : Array [1 . . . n] of Element) K := 4 // number of buckets per round D := dlogK(max{A[i] + 1 | i = 1, . . . , n})e // calculate number of rounds B := allocate (Array of Element size n) // temporary array B for d := 0 to D − 1 do // sort by the d-th K-digit. redefine key(x) := (x div Kd ) mod K KSortCopy(A, B, n), swap(A, B) // sort from A to B, and swap back invariant A ist nach den K-Ziffern d..0 sortiert. dispose (B) Procedure KSortCopy(A, B : Array [1 . . . n] of Element; K : N) c = h0, . . . , 0i : Array [0 . . . K − 1] of N for i := 1 to n do c[key(A[i])]++ // count occurrences sum := 1 for k := 0 to K − 1 do // exclusive prefix sum next := sum + c[k], c[k] := sum, sum := next for i := 1 to n do B c[key(A[i])]++ := A[i] // move element A[i] into bucket of B Procedure MSDRadixSort(A : Array [1 . . . n] of Element) K := 4 // number of buckets per round D := dlogK(max{A[i] + 1 | i = 1, . . . , n})e // count number of round MSDRadixSortRec(A, D − 1, K) Procedure MSDRadixSortRec(A : Array [1 . . . n] of Element; d, K : N) c = h0, . . . , 0i : Array [0 . . . K − 1] of N // KSort with in-place permuting redefine key(x) := (x div Kd ) mod K for i := 1 to n do c[key(A[i])]++ // count occurrences b = h0, . . . , 0i : Array [0 . . . K] of N sum := 1 for k := 0 to K do // inclusive prefix sum into b sum := sum + c[k], b[k] := sum assert b[K] = n for i := 1 to n do while j := −− b[key(A[i])] i // walk on cycles until i swap(A[i], A[j]) // move A[i] into right bucket i := i + c[key(A[i])] // bucket of A[i] is finished invariant A ist nach den K-Ziffern d..(D − 1) sortiert if d = 0 return // done? for k := 0 to K − 1 do // recursion into each of the K buckets if if c[k] 1 // it contains two or more elements MSDRadixSortRec(A b[k] . . . b[k + 1] − 1 , d − 1, K) dispose (b), dispose (c) 2 http://panthema.net/2013/sound-of-sorting