Skills in High Demand
by Toptal Clients
React.js Developer JobsNode.js Developer JobsRuby on Rails Developer JobsAzure Developer JobsReact Native Developer JobsQA Engineer JobsGo Engineer JobsJavaScript Developer JobsPython Developer JobsDjango Developer JobsPHP Developer JobsC# Developer JobsiOS Developer JobsBlockchain Developer JobsSwift Developer JobsAWS Developer JobsVue.js Developer JobsJava Developer Jobs.NET Developer JobsAngular Developer JobsAndroid Developer JobsMagento Developer JobsShopify Developer JobsWordPress Developer JobsLaravel Developer JobsElixir Developer JobsDocker Developer JobsFlutter Developer JobsSoftware Architect JobsUnity or Unity3D Developer JobsCloud Engineer JobsASP.NET Developer JobsKubernetes Expert JobsSystem Security Developer JobsKotlin Developer JobsCSS Developer JobsComputer Vision Developer JobsAI Engineer JobsDrupal Developer JobsSQL Developer JobsSymfony Developer JobsRuby Developer JobsData Scientist JobsBusiness Intelligence Developer JobsC++ Developer JobsIonic Developer JobsGraphQL Developer JobsMachine Learning Engineer JobsXamarin Developer JobsFirebase Developer JobsReact.js Developer JobsNode.js Developer JobsRuby on Rails Developer JobsAzure Developer JobsReact Native Developer JobsQA Engineer JobsGo Engineer JobsJavaScript Developer JobsPython Developer JobsDjango Developer JobsPHP Developer JobsC# Developer JobsiOS Developer JobsBlockchain Developer JobsSwift Developer JobsAWS Developer JobsVue.js Developer JobsJava Developer Jobs.NET Developer JobsAngular Developer JobsAndroid Developer JobsMagento Developer JobsShopify Developer JobsWordPress Developer JobsLaravel Developer JobsElixir Developer JobsDocker Developer JobsFlutter Developer JobsSoftware Architect JobsUnity or Unity3D Developer JobsCloud Engineer JobsASP.NET Developer JobsKubernetes Expert JobsSystem Security Developer JobsKotlin Developer JobsCSS Developer JobsComputer Vision Developer JobsAI Engineer JobsDrupal Developer JobsSQL Developer JobsSymfony Developer JobsRuby Developer JobsData Scientist JobsBusiness Intelligence Developer JobsC++ Developer JobsIonic Developer JobsGraphQL Developer JobsMachine Learning Engineer JobsXamarin Developer JobsFirebase Developer Jobs

Toptal connects the top 3% of freelance developers all over the world.

Quick Sort

Animation, code, analysis, and discussion of quick sort on 4 initial conditions.

How to use: Press "Play all", or choose the    button.

Play All
Play animation
Random
Play animation
Nearly Sorted
Play animation
Reversed
Play animation
Few Unique
ALGORITHM
_# choose pivot_
swap a[1,rand(1,n)]

_# 2-way partition_
k = 1
for i = 2:n, if a[i] < a[1], swap a[++k,i]
swap a[1,k]
_→ invariant: a[1..k-1] < a[k] <= a[k+1..n]_

_# recursive sorts_
sort a[1..k-1]
sort a[k+1,n]
DISCUSSION

When carefully implemented, quick sort is robust and has low overhead. When a stable sort is not needed, quick sort is an excellent general-purpose sort – although the 3-way partitioning version should always be used instead.

The 2-way partitioning code shown above is written for clarity rather than optimal performance; it exhibits poor locality, and, critically, exhibits O(n2) time when there are few unique keys. A more efficient and robust 2-way partitioning method is given in Quicksort is Optimal by Robert Sedgewick and Jon Bentley. The robust partitioning produces balanced recursion when there are many values equal to the pivot, yielding probabilistic guarantees of O(n·lg(n)) time and O(lg(n)) space for all inputs.

With both sub-sorts performed recursively, quick sort requires O(n) extra space for the recursion stack in the worst case when recursion is not balanced. This is exceedingly unlikely to occur, but it can be avoided by sorting the smaller sub-array recursively first; the second sub-array sort is a tail recursive call, which may be done with iteration instead. With this optimization, the algorithm uses O(lg(n)) extra space in the worst case.

KEY
  • Black values are sorted.
  • Gray values are unsorted.
  • Dark gray values denote the current interval.
  • A pair of red triangles mark k and i (see the code).
PROPERTIES
  • Not stable
  • O(lg(n)) extra space (see discussion)
  • O(n2) time, but typically O(n·lg(n)) time
  • Not adaptive
Preparing for a technical interview? Check out our interview guides.