Algorithms: Quicksort

Algorithms and Data Structures From Zero to Hero

Divide & conquer

Divide and conquer is a problem-solving approach that involves breaking a large problem down into smaller, more manageable sub-problems, solving each sub-problem independently, and then combining the solutions to the sub-problems to solve the larger problem.

Quicksort

Quicksort is a sorting algorithm. It’s much faster than selection sort and is frequently used in real life.

function quickSort(arr) {
if (arr.length <= 1) {
return arr
}
  • A sub-array of all the numbers less than the pivot
  • The pivot
  • A sub-array of all the numbers greater than the pivot
quickSort([7, 1]) + [13] + quickSort([])
> [1, 7, 13] <------ A sorted array

Inductive proof

You just got sneak peak into inductive proofs! Inductive proofs are one way to prove that your algorithm works. Each inductive proof has two steps: the base case and the inductive case. Sound familiar? For example, suppose I want to prove that I can climb to the top of a ladder. In the inductive case, if my legs are on a rung, I can put my legs on the next rung. So if I’m on rung 2, I can climb to rung 3. That’s the inductive case. For the base case, I’ll say that my legs are on rung 1. Therefore, I can climb the entire ladder, going up one rung at a time.

Time Complexity

  • Best / average case: O ( n . log ( n ) ) in most balanced scenarios, when the generated partitions have nearly equal elements. Thus for a balanced case, the depth of the recursion tree is log2 ( n ) and the reordering at each recursion level takes O ( n ) time.
    Thus giving the time complexity O ( n . log ( n ) )
  • Worst case: O ( n2 ) when the array is already sorted, the pivot element will require n comparisons to deduce that it remains in its position at the end. Furthermore, the first partition would be empty, but the second partition will have (n-1) elements. Similarly, the pivot in the second partition will need (n-1) comparisons to deduce that it remains in its position at the end and so on. Consequently there will be a total of F(n) = n + (n-1) + (n-2) + .. + 2 + 1 comparisons. Thus giving a worst-case time complexity of O ( n2 ).

Example code listing

function quickSort(arr) {
// base case: arrays with 0 or 1 element are already "sorted"
if (arr.length <= 1) {
return arr
}

const pivot = arr[0]

// partition the list into two sub-lists
let left = []
let right = []
for (let i = 1; i < arr.length; i++) {
if (arr[i] < pivot) {
left.push(arr[i])
} else {
right.push(arr[i])
}
}

// recursively sort the two sub-lists
left = quickSort(left)
right = quickSort(right)

// combine the sorted sub-lists back into a single, sorted list
return left.concat(pivot, right)
}

Summary

In conclusion, the quicksort algorithm is a popular and effective way to sort large lists of data. It is a divide and conquer algorithm that works by dividing the unsorted list into smaller sub-lists and then sorting each sub-list independently before combining them back into a single, sorted list.

--

--

Software Engineer passionate about learning, creating new things and sharing my knowledge in the process.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Kemil Beltre

Software Engineer passionate about learning, creating new things and sharing my knowledge in the process.