Log in Sign up
Back to Discover
💻

Sorting algorithm

technology Maturity 11-13

Computers can put things in order.

Bubblesort-edited-color.svg
Bubblesort-edited-color.svg
They can sort numbers or words. This helps us find things fast. It makes work easy for us. It is like cleaning your room. Do you like to sort your toys?

38 words

Computers use special rules to sort things.

Bubblesort-edited-color.svg
Bubblesort-edited-color.svg
They can put numbers in order. They can also put words in order. This helps the computer work fast.
Merge sort animation.gif
Merge sort animation.gif
Sorting is like putting your toys in a row. It makes it easy to find what you need. Some rules sort by swapping items. Other rules use different ways to group things. Sorting helps us read data easily. It is a very useful tool for computers.

76 words

Computers use special rules to sort lists. These rules are called sorting algorithms.

Bubblesort-edited-color.svg
Bubblesort-edited-color.svg
A sorting algorithm puts items in a specific order. Most often, it puts numbers in order from low to high. It can also put words in alphabetical order.
Merge sort animation.gif
Merge sort animation.gif
Sorting is very important. It helps other computer tasks work much faster.

A good sort must follow two rules. First, the items must stay in order. Second, the list must have all the original items. It just changes their places.

Some sorts are "stable." This means they keep the original order for items that are the same. Imagine sorting playing cards by their rank.

Sorting stability playing cards.svg
Sorting stability playing cards.svg
If you have two 5s, a stable sort keeps them in their original order. This is helpful when you sort by more than one thing.

There are many ways to sort. Some use "exchange" methods. These swap items to find the right spot. Others use "selection" methods. They pick the best item and move it. Scientists still study how to make the fastest sorts for small groups of items.

182 words

Computers often need to organize information into a specific order. This task is done using a sorting algorithm. An algorithm is just a set of rules to follow. Most often, computers sort numbers from smallest to largest. They can also sort words into alphabetical order.

Merge sort animation.gif
Merge sort animation.gif
Sorting is very important for making computers work well. It helps other tasks, like searching for data, run much faster. A good sort must follow two main rules. First, the list must be in a steady order. Second, the list must contain all the original items. It only changes their positions, not the items themselves.

There are many different ways to sort a list. Some methods use an exchange style. This means the computer swaps items to find the right spot. Bubble sort is a famous example of this.

Bubblesort-edited-color.svg
Bubblesort-edited-color.svg
Other methods use selection. This is when the computer picks the best item and moves it. Merge sort is another way that uses a method called merging.
Merge sort animation.gif
Merge sort animation.gif
Some algorithms are also stable. A stable sort keeps items that are the same in their original order. Imagine sorting playing cards by their rank.
Sorting stability playing cards.svg
Sorting stability playing cards.svg
If you have two 5s, a stable sort keeps them in the same relative order. This is helpful if you want to sort by rank and then by suit.

People have been studying how to sort for a long time. This problem is famous because it seems simple but is actually quite hard to solve perfectly. Researchers have worked on this since the very beginning of computing. Around 1951, a person named Betty Holberton worked on early sorting. She worked on famous computers like the ENIAC and the UNIVAC. Later, in 1956, scientists analyzed the bubble sort method. Many fast ways to sort have been known since the middle of the 20th century. New ways are still being invented today. For example, Timsort was made in 2002, and library sort was published in 2006.

Scientists use math to see how well a sort works. They look at the best, worst, and average cases. This helps them understand how much time or memory a sort uses. A common way to measure this is using something called big O notation.

Shell sorting algorithm color bars.svg
Shell sorting algorithm color bars.svg
Some sorts are called "in-place" because they do not need much extra memory. They only need a tiny amount of extra space to do their job. Other sorts are called "adaptive." This means they work faster if the list is already mostly sorted. Some sorts are even "online," which means they can sort items as they arrive in a steady stream.

Sorting algorithms are a big part of learning about computers. They are often the first thing students learn in science classes. These algorithms help students understand how computers think and solve problems. They introduce ideas like how to divide a big job into smaller parts. This is called a divide-and-conquer approach.

Merge sort animation.gif
Merge sort animation.gif
Even though we have many ways to sort, some questions are still not answered. Scientists are still trying to find the best way to sort very small groups of items. They are also looking for the best ways to sort on machines that do many things at once. The world of sorting is still full of new discoveries.

549 words

In computer science, a sorting algorithm is a specific set of rules used to organize elements of a list. Most commonly, these algorithms arrange data in numerical or lexicographical order. Lexicographical order refers to the way words are organized alphabetically. Sorting can be done in ascending order, from smallest to largest, or descending order, from largest to smallest. Efficient sorting is vital because it optimizes other processes. For instance, search and merge algorithms run much faster when their input data is already sorted. Sorting also helps in canonicalizing data, which means putting it into a standard, consistent format.

To be considered successful, a sorting algorithm must satisfy two strict mathematical conditions. First, the output must be in monotonic order. This means every element is either greater than or equal to the previous one, or less than or equal to it. Second, the output must be a permutation of the input. A permutation is a reordering that retains every single original element without adding or losing any. While some algorithms are designed for sequential access, the most powerful ones assume the data is in a structure that allows random access. Random access means the computer can jump directly to any specific item in the list.

Merge sort animation.gif
Merge sort animation.gif

Algorithms are often classified by how they move data. Comparison sorts are a major category where the computer examines data only by comparing two elements at a time. These algorithms have a fundamental mathematical limit. They require a minimum of n log n - 1.4427n + O(log n) comparisons to work. However, non-comparison sorts, such as counting sort, can sometimes perform even better. Sorting can also be categorized by its method of movement. Exchange sorts, like bubble sort or quicksort, work by swapping items. Selection sorts, including heapsort, work by picking specific items to move. Other methods include insertion, merging, and even specialized types like the library sort.

Bubblesort-edited-color.svg
Bubblesort-edited-color.svg

Another important concept is stability. A stable sorting algorithm maintains the relative order of records that have equal keys. A key is the specific part of the data used for the sort. For example, if you sort playing cards by rank, the two 5s in a deck have equal keys. A stable sort ensures that if one 5 was originally above the other, it stays above it in the final list.

Sorting stability playing cards.svg
Sorting stability playing cards.svg
This is very useful when sorting by multiple criteria. You might sort a list of students by name first, and then sort them by class section. If the second sort is stable, the alphabetical name order will remain intact within each section.

Sorting playing cards using stable sort.svg
Sorting playing cards using stable sort.svg

Researchers have studied sorting since the very beginning of computing. The problem is famous because it is easy to describe but difficult to solve perfectly. In 1951, Betty Holberton worked on early sorting for the ENIAC and UNIVAC computers. By 1956, scientists were already analyzing the bubble sort method. While many optimal algorithms were discovered in the mid-20th century, new ones are still being created. Timsort was introduced in 2002, and the library sort was published in 2006. Even today, finding the best way to sort very small arrays of fewer than 20 elements remains an open research problem.

Shell sorting algorithm color bars.svg
Shell sorting algorithm color bars.svg

Computer scientists use Big O notation to measure the complexity of these algorithms. Complexity describes how the running time or memory usage changes as the list grows larger. For typical serial algorithms, good behavior is O(n log n), while bad behavior can reach O(n^2). Some algorithms are also described as "in-place." A strictly in-place sort requires only O(1) additional memory beyond the items being sorted. Others are called "adaptive," meaning they run faster if the input list is already partially sorted. Some are "online," meaning they can process a constant stream of data as it arrives.

Sorting is a fundamental topic in introductory computer science classes. It provides a gateway to many core concepts, such as divide-and-conquer strategies and various data structures. Students learn about heaps, binary trees, and randomized algorithms through sorting. It also introduces the idea of time-space tradeoffs, where a programmer must choose between using more memory or more time. As computing moves toward parallel machines, finding the most optimal ways to sort across many processors remains a major area of study for scientists around the world.

719 words
🖼️ Images & Media (5)
File:Merge sort animation.gif
Merge sort animation.gif
File:Sorting stability playing cards.svg
Sorting stability playing cards.svg
File:Sorting playing cards using stable sort.svg
Sorting playing cards using stable sort.svg
File:Shell_sorting_algorithm_color_bars.svg
Shell_sorting_algorithm_color_bars.svg
File:Bubblesort-edited-color.svg
Bubblesort-edited-color.svg
Up Next
💻
Merge sort
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.