ArticleslgStudy

computer science

Sorting

Sorting is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Sorting rather than just read about it. In short: Sorting refers to ordering data in an increasing or decreasing manner according to some linear relationship among the data items. ordering: arranging items in a sequence ordered by some criterion; categorizing: grouping items with similar properties. Ordering items is the combination of categorizing them based on equivalent order, and ordering the categories themselves.

Sorting — main illustration
Sorting — illustration

Key takeaways

  • Sorting belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Sorting to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Sorting from memory before moving on to harder problems.

Reference excerpt

Sorting refers to ordering data in an increasing or decreasing manner according to some linear relationship among the data items.

ordering: arranging items in a sequence ordered by some criterion; categorizing: grouping items with similar properties. Ordering items is the combination of categorizing them based on equivalent order, and ordering the categories themselves.

By type

Information or data In computer science, arranging in an ordered sequence is called "sorting". Sorting is a common operation in many applications, and efficient algorithms have been developed to perform it. The most common uses of sorted sequences are:

making lookup or search efficient; making merging of sequences efficient; enabling processing of data in a defined order. The opposite of sorting, rearranging a sequence of items in a random or meaningless order, is called shuffling. For sorting, either a weak order, "should not come after", can be specified, or a strict weak order, "should come before" (specifying one defines also the other, the two are the complement of the inverse of each other, see operations on binary relations). For the sorting to be unique, these two are restricted to a total order and a strict total order, respectively. Sorting n-tuples (depending on context also called e.g. records consisting of fields) can be done based on one or more of its components. More generally objects can be sorted based on a property. Such a component or property is called a sort key. For example, the items are books, the sort key is the title, subject or author, and the order is alphabetical. A new sort key can be created from two or more sort keys by lexicographical order. The first is then called the primary sort key, the second the secondary sort key, etc. For example, addresses could be sorted using the city as primary sort key, and the street as secondary sort key. If the sort key values are totally ordered, the sort key defines a weak order of the items: items with the same sort key are equivalent with respect to sorting. See also stable sorting. If different items have different sort key values then this defines a unique order of the items.

A standard order is often called ascending (corresponding to the fact that the standard order of numbers is ascending, i.e. A to Z, 0 to 9), the reverse order descending (Z to A, 9 to 0). For dates and times, ascending means that earlier values precede later ones e.g. 1/1/2000 will sort ahead of 1/1/2001.

Common algorithms

Bubble/Shell sort: Exchange two adjacent elements if they are out of order. Repeat until array is sorted. Insertion sort: Scan successive elements for an out-of-order item, then insert the item in the proper place. Selection sort: Find the smallest (or biggest) element in the array, and put it in the proper place. Swap it with the value in the first position. Repeat until array is sorted. Quick sort: Partition the array into two segments. In the first segment, all elements are less than or equal to the pivot value. In the second segment, all elements are greater than or equal to the pivot value. Finally, sort the two segments recursively. Merge sort: Divide the list of elements in two parts, sort the two parts individually and then merge it.

Physical

Various sorting tasks are essential in industrial processes, such as mineral processing. For example, during the extraction of gold from ore, a device called a shaker table uses gravity, vibration, and flow to separate gold from lighter materials in the ore (sorting by size and weight). Sorting is also a naturally occurring process that results in the concentration of ore or sediment. Sorting results from the application of some criterion or differential stressors to a mass to separate it into its components based on some variable quality. Materials that are different, but only slightly so, such as the isotopes of uranium, are very difficult to separate. Optical sorting is an automated process of sorting solid products using cameras and/or lasers and has widespread use in the food industry. Sensor-based sorting is used in mineral processing.

See also Help:Sorting in Wikipedia tables. For sorting of categories, see Wikipedia:Categorization#Sort keys and for sorting of article sections, see WP:ORDER Collation Data processing IBM mainframe sort/merge Unicode collation algorithm Knolling 5S (methodology)

References

External links

Demonstration of Sorting Algorithms (includes bubble and quicksort) Animated video explaining bubble sort and quick sort and compares their performance.

Illustrations

Sorting: A railroad classification yard, used for sorting freight cars
A railroad classification yard, used for sorting freight cars

Worked examples

Example 1 — a first encounter with Sorting

Start with the simplest possible case. Write down what Sorting claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Sorting before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Sorting ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Sorting

In research
Sorting appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Sorting in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Sorting is common in secondary-school and first-year university syllabi. It links to neighbouring topics Data processing, Sorting algorithms, so understanding it makes those chapters shorter.
In everyday life
Look for Sorting outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Sorting” →

Affiliate

Preply — study more efficiently by working with a personal tutor. 50% off.

How to study Sorting in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Sorting means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Sorting out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Sorting in simple terms?

Sorting refers to ordering data in an increasing or decreasing manner according to some linear relationship among the data items. ordering: arranging items in a sequence ordered by some criterion; categorizing: grouping items with similar properties. Ordering items is the combination of categorizin…

Why does Sorting matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Sorting?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Sorting.

Tags

  • Data processing
  • Sorting algorithms

Keep exploring