Log in Sign up
Back to Discover
💻

Quicksort

technology Maturity 7-9

Computers use a fast way to sort.

Quicksort-example.gif
Quicksort-example.gif
It puts things in order. It picks one thing to help. It puts small things on one side. It puts big things on the other. This helps us find things fast. Do you like to sort your toys?

46 words

Computers use a fast way to sort things.

Quicksort-example.gif
Quicksort-example.gif
This helps them put items in order.

A man named Tony Hoare made this idea. He wanted to sort words in a list.

Quicksort-diagram.svg
Quicksort-diagram.svg

First, the computer picks one item to help. This item is called a pivot. The computer looks at all the other items.

It puts small items on one side. It puts big items on the other side. This makes two new, smaller lists.

Then, it does the same thing to those lists. It keeps going until everything is in order. It is a very smart way to work!

101 words

Computers need fast ways to put items in order. One great way is called Quicksort. A scientist named Tony Hoare made it in 1959.

Quicksort-example.gif
Quicksort-example.gif
He needed to sort Russian words to use a dictionary.

Quicksort uses a way called divide-and-conquer. This means it breaks a big job into smaller parts. First, the computer picks one item from the list. We call this item a pivot.

Quicksort-diagram.svg
Quicksort-diagram.svg

Next, the computer does a set of steps called partitioning. It moves items around the pivot. It puts items smaller than the pivot on one side. It puts items larger than the pivot on the other side. Now the list is split into two smaller groups.

The computer then repeats these steps on the new groups. It keeps doing this until every item is in its right place. This is called recursion, which means a process calls itself.

Quicksort is very fast for most data. It is often faster than other ways like merge sort. Some versions use a special way to pick the pivot to stay fast. Even though it is old, many computers still use it today.

186 words

Computers often need to organize large lists of information very quickly. One of the best ways to do this is through an algorithm called Quicksort. An algorithm is just a set of steps a computer follows to finish a task. Quicksort is a special kind of divide-and-conquer method. This means it takes a huge, difficult job and breaks it into smaller, easier pieces.

Quicksort-diagram.svg
Quicksort-diagram.svg
It is famous because it works very fast on most types of data. It can even be faster than other methods like merge sort or heapsort.

To understand how it works, you first have to look at a step called partitioning. The computer picks one item from the list to be the pivot.

Quicksort-example.gif
Quicksort-example.gif
Next, it moves all the other items around that pivot. Items that are smaller than the pivot move to one side. Items that are larger than the pivot move to the other side. This creates two new, smaller groups of items. The computer then uses recursion to sort those smaller groups. Recursion is when a process repeats itself on smaller and smaller parts until the whole list is in order.

A scientist named Tony Hoare invented Quicksort in 1959. At that time, he was a visiting student at Moscow State University. He was working on a project to help machines translate languages. He needed to sort Russian words so he could look them up in a dictionary.

Quicksort-example.gif
Quicksort-example.gif
He realized his first idea was too slow for the job. He eventually published his findings in a journal called The Computer Journal in 1962. Later, he used a tool called ALGOL to make the code even better.

Many researchers have helped make Quicksort even better over the years. In 1975, Robert Sedgewick studied how to pick the best pivot to keep the speed high. In 1993, Jon Bentley and Doug McIlroy added new ways to handle lists with equal items. They used a clever trick called the pseudomedian of nine to pick a pivot. This involves looking at nine items and finding a middle value.

Quicksort-diagram.svg
Quicksort-diagram.svg
There is also a version called the Lomuto partition scheme. This version is simple to learn but can be slower than Hoare's original way.

Quicksort is used in many places you might not notice. It is a default tool used in the Unix operating system. It is also part of the standard libraries for the C and Java programming languages. You can think of it like a master organizer for digital information. Just as you might sort a deck of cards by splitting it into piles, Quicksort splits data to find order. It turns a messy pile of information into a perfectly straight line.

Quicksort-diagram.svg
Quicksort-diagram.svg

448 words

Quicksort is a highly efficient, general-purpose sorting algorithm used to organize data. It belongs to a category of algorithms known as divide-and-conquer. This means it solves a large problem by breaking it into smaller, more manageable sub-problems. Quicksort is a comparison sort. It works by comparing elements to one another to determine their relative order. It can sort any type of data as long as a "less-than" relation, or a total order, is defined. Because it is so effective, it is often faster than other methods like merge sort or heapsort when dealing with randomized data.

Quicksort-diagram.svg
Quicksort-diagram.svg

The core mechanism of Quicksort is a process called partitioning. To begin, the algorithm selects a single element from the array to serve as a pivot. The goal of partitioning is to reorder the array so that all elements smaller than the pivot move to one side. Meanwhile, all elements larger than the pivot move to the opposite side. This creates two distinct sub-arrays. Once the partition is complete, the pivot is in its final, correct position. The algorithm then uses recursion to sort the remaining sub-arrays. Recursion is a process where a function calls itself to repeat the same steps on smaller parts of the data.

Quicksort-example.gif
Quicksort-example.gif

There are different ways to perform this partitioning, which makes Quicksort a family of related algorithms. One famous method is the Lomuto partition scheme, named after Nico Lomuto. This scheme is popular in textbooks because it is compact and easy to understand. It usually chooses the last element of the array as the pivot. However, it is less efficient than other methods. On average, it performs three times more swaps than the original method. It also performs poorly if all the elements in the array are equal.

Quicksort-diagram.svg
Quicksort-diagram.svg

The original method is the Hoare partition scheme, created by Tony Hoare. This scheme uses two pointers that start at opposite ends of the array. These pointers move toward each other until they find an "inversion." An inversion occurs when a pointer finds an element that is greater than the pivot on the left side, while the other pointer finds an element smaller than the pivot on the right. When this happens, the two elements are swapped. This continues until the pointers cross, signaling that a valid partition has been found. This method is generally more efficient than the Lomuto scheme.

The history of Quicksort began in 1959. Tony Hoare was a visiting student at Moscow State University at the time. He was working on a machine translation project for the National Physical Laboratory. He needed to sort Russian words to look them up in a Russian-English dictionary. His initial idea, an insertion sort, was too slow for the task. Hoare eventually published his algorithm in 1962 in The Computer Journal. Later, he improved the algorithm using ALGOL, a programming language that allowed for recursion. This improved version was published in the journal Communications of the Association for Computing Machinery in 1961.

Quicksort-example.gif
Quicksort-example.gif

Many scientists have since refined the algorithm. In 1975, Robert Sedgewick published a milestone PhD thesis. He studied different ways to select a pivot to improve performance. In 1993, Jon Bentley and Doug McIlroy introduced further improvements for programming libraries. They developed a technique to handle equal elements more effectively. They also used a pivot scheme called the "pseudomedian of nine." This involves taking a sample of nine elements and finding the median of their medians. This helps ensure the algorithm stays fast even with difficult data.

Quicksort-diagram.svg
Quicksort-diagram.svg

Despite its speed, Quicksort has specific mathematical limits. On average, the algorithm is very fast, but its performance can change based on the input. In the worst-case scenario, the number of comparisons can reach O(n squared). This often happens if the pivot selection is poor, such as always choosing the last element in an already sorted array. To combat this, many modern versions pick the middle element or use randomness. In 1998, Doug McIlroy even created an "AntiQuicksort" function. This function creates adversarial data specifically designed to force the algorithm into its slowest behavior.

Quicksort-diagram.svg
Quicksort-diagram.svg

Quicksort remains a fundamental tool in modern computing. It is used as the default library sort subroutine in Unix. It also provides the basis for the C standard library subroutine and the reference implementation in Java. The algorithm's ability to sort "in-place" is also important. This means it can sort the data using only a small amount of extra memory. By dividing complex datasets into simple, ordered segments, Quicksort helps manage the massive amounts of information processed by computers every day.

760 words
🖼️ Images & Media (2)
File:Quicksort-diagram.svg
Quicksort-diagram.svg
File:Quicksort-example.gif
Quicksort-example.gif
Up Next
💻
Tony Hoare
Technology
More to explore

What is Nepedia?

A free, ad-free encyclopedia for children. Every article is written at five reading levels, so the same page works for a five-year-old and a fifteen-year-old — use the level switcher above to see this one change. No account needed to read.