You get a bonus - 1 coin for daily activity. Now you have 1 coin

Sorting Algorithms: Complexity, Definition, Properties, Types, and Online Comparison

Lecture



Sorting algorithm — is an algorithm for arranging the elements in a list in order. When an element of a list has several fields, the field that serves as the ordering criterion is called the sort key. In practice, a number often serves as the key, while the other fields store various data that has no effect on the operation of the algorithm.

History

Sorting Algorithms: Complexity, Definition, Properties, Types, and Online Comparison

The Hollerith tabulator with a "sorting box"

The first prototypes of modern sorting methods already appeared in the 19th century. By 1890, to speed up the processing of US census data, the American Herman Hollerith created the first statistical tabulator — an electromechanical machine designed for the automatic processing of information recorded on punched cards. Hollerith's machine had a special "sorting box" with 26 internal compartments. Operating the machine required the operator to insert a punched card and pull down a lever. Thanks to the holes punched in the card, a certain electrical circuit was closed, and the reading on the associated dial increased by one. At the same time, one of the 26 lids of the sorting box would open, and the punched card would move into the corresponding compartment, after which the lid would close. This machine made it possible to process about 50 cards per minute, which sped up data processing threefold. By the 1900 census, Hollerith had improved the machine, automating the feeding of cards. The operation of Hollerith's sorting machine was based on radix sorting methods. The patent for the machine specifies sorting "separately for each column," but does not define the order. Another similar machine, patented in 1894 by John Gore, mentions sorting starting from the tens column. The method of sorting starting from the units column first appears in the literature in the late 1930s. By that time, sorting machines already made it possible to process up to 400 cards per minute.

Sorting Algorithms: Complexity, Definition, Properties, Types, and Online Comparison

EDVAC

Later, the history of algorithms turned out to be linked to the development of electronic computers. According to some sources, it was a sorting program that became the very first program for computing machines. Some computer designers, in particular the developers of EDVAC, called the task of sorting data the most characteristic non-numerical task for computing machines. In 1945, John von Neumann, in order to test a set of instructions for EDVAC, developed merge-sort programs. That same year, the German engineer Konrad Zuse developed a program for sorting by simple insertion. By that time, fast specialized sorting machines had already appeared, and it was against these that the efficiency of the computers being developed was measured. The first published discussion of sorting by means of computing machines was a lecture given by John Mauchly in 1946. Mauchly showed that sorting could also be useful for numerical calculations, and described the methods of simple insertion sort and binary insertion sort, as well as radix sort with partial passes. Later, the company "Eckert–Mauchly Computer Corporation," which he organized together with the engineer John Eckert, released some of the earliest electronic computing machines, BINAC and UNIVAC. Alongside the noted internal sorting algorithms, external sorting algorithms also began to appear, and their development was driven by the limited memory capacity of the first computing machines. In particular, methods of balanced two-way radix sorting and balanced two-way merging were proposed.

By 1952, many internal sorting methods were already being used in practice, but the theory was relatively poorly developed. In October 1952, Daniel Goldenberg presented five sorting methods with an analysis of the best and worst cases for each. In 1954, Harold Seward developed Goldenberg's ideas further and also analyzed methods of external sorting. In 1956, Howard Demuth examined three abstract models of the sorting problem: using cyclic memory, linear memory, and random-access memory. For each of these problems, the author proposed optimal or near-optimal sorting methods, which helped connect theory with practice. Because so few people were involved with computing technology at the time, these reports did not appear in the "open literature." The first major survey article on sorting to appear in print, in 1955, was the work of J. Hosken, in which he described all the special-purpose equipment and computer sorting methods available at the time, drawing on manufacturers' brochures. In 1956, E. Friend analyzed, in his work, the mathematical properties of a large number of internal and external sorting algorithms, proposing several new methods.

After that, many different sorting algorithms were proposed: for example, address calculation sorting in 1956; merge insertion, radix exchange sort, cascade merge sort, and Shell sort in 1959, polyphase merge sort and tree insertion sort in 1960, oscillating sort and Hoare's quicksort in 1962, Williams's heapsort and Batcher's merge exchange sort in 1964. The late 1960s also saw intensive development of sorting theory. Algorithms that appeared later were largely variations of already known methods. Adaptive sorting methods, aimed at faster execution when the input sequence satisfies predetermined criteria, became widespread.

Comparison of online sorting algorithms

Открыть на весь экран

Problem statement

Suppose it is required to order N elements: Sorting Algorithms: Complexity, Definition, Properties, Types, and Online Comparison. Each element is a record Sorting Algorithms: Complexity, Definition, Properties, Types, and Online Comparison, containing certain information and a key Sorting Algorithms: Complexity, Definition, Properties, Types, and Online Comparison that controls the sorting process. On the set of keys an order relation «<» is defined such that for any three key values Sorting Algorithms: Complexity, Definition, Properties, Types, and Online Comparison the following conditions hold[10]:

  • the trichotomy law[en]: either Sorting Algorithms: Complexity, Definition, Properties, Types, and Online Comparison, or Sorting Algorithms: Complexity, Definition, Properties, Types, and Online Comparison, or Sorting Algorithms: Complexity, Definition, Properties, Types, and Online Comparison;
  • the transitivity law: if Sorting Algorithms: Complexity, Definition, Properties, Types, and Online Comparison and Sorting Algorithms: Complexity, Definition, Properties, Types, and Online Comparison, then Sorting Algorithms: Complexity, Definition, Properties, Types, and Online Comparison.

These conditions define the mathematical notion of a linear, or total, ordering, and sets satisfying them can be sorted by most methods[10].

The task of sorting is to find a permutation of the records Sorting Algorithms: Complexity, Definition, Properties, Types, and Online Comparison with indices Sorting Algorithms: Complexity, Definition, Properties, Types, and Online Comparison, after which the keys would be arranged in non-decreasing order[10]:

Sorting Algorithms: Complexity, Definition, Properties, Types, and Online Comparison

A sort is called stable if it does not change the relative order of elements with equal keys[10]:

Sorting Algorithms: Complexity, Definition, Properties, Types, and Online Comparison for any Sorting Algorithms: Complexity, Definition, Properties, Types, and Online Comparison and }Sorting Algorithms: Complexity, Definition, Properties, Types, and Online Comparison.

Sorting methods can be divided into internal and external. Internal sorting is used for data that fits into main memory, which makes it more flexible in terms of data structures. External sorting is applied when the data does not fit into main memory, and it is oriented toward achieving a result under conditions of limited resources[11].

Evaluation of a sorting algorithm

Sorting algorithms are evaluated by their execution speed and their efficiency in the use of memory:

  • Time — the main parameter characterizing the speed of an algorithm. Also called computational complexity. For sorting, the worst, average and best behavior of the algorithm in terms of the cardinality of the input set A are important. If the set A is fed to the algorithm's input, we denote n = |A|. For a typical sorting algorithm, good behavior is O(n log n) and bad behavior is O(n2). Ideal behavior for sorting is O(n). Sorting algorithms that use only the abstract key-comparison operation always need at least comparisons. Nevertheless, there exists Han's sorting algorithm (Yijie Han) with computational complexity O(n log log n log log log n), which exploits the fact that the key space is bounded (it is extremely complex, and hidden behind the O-notation is a rather large coefficient, which makes it impossible to apply in everyday practice). There is also the notion of sorting networks. Assuming that several comparisons can be performed simultaneously (for example, during parallel computation), one can sort n numbers in O(log2 n) operations. In this case the number n must be known in advance;
  • Memory — a number of algorithms require the allocation of additional memory for temporary data storage. As a rule, these algorithms require O(log n) memory. The estimate does not take into account the space occupied by the original array and costs independent of the input sequence, for example, for storing the program code (since all of this consumes O(1)). Sorting algorithms that do not consume additional memory are referred to as in-place sorts.

Optimality Sorting Algorithms: Complexity, Definition, Properties, Types, and Online Comparison in the general case

In the general case, the sorting problem assumes that the only operation guaranteed to be available on elements is comparison. The answer to comparing elements Sorting Algorithms: Complexity, Definition, Properties, Types, and Online Comparison and Sorting Algorithms: Complexity, Definition, Properties, Types, and Online Comparison can be one of two options: Sorting Algorithms: Complexity, Definition, Properties, Types, and Online Comparison or Sorting Algorithms: Complexity, Definition, Properties, Types, and Online Comparison. Therefore, if in the course of its work the algorithm makes Sorting Algorithms: Complexity, Definition, Properties, Types, and Online Comparison comparisons, then a total of }Sorting Algorithms: Complexity, Definition, Properties, Types, and Online Comparison combinations of answers to them are possible.

The number of permutations of Sorting Algorithms: Complexity, Definition, Properties, Types, and Online Comparison elements equals Sorting Algorithms: Complexity, Definition, Properties, Types, and Online Comparison. In order for it to be possible to construct a surjection from the set of combinations of answers onto the set of all permutations, the number of comparisons must be no less than Sorting Algorithms: Complexity, Definition, Properties, Types, and Online Comparison (since comparison is the only permitted operation).

By taking the logarithm of Stirling's formula, one can discover that Sorting Algorithms: Complexity, Definition, Properties, Types, and Online Comparison[12]

Properties and types

  • Stability — a stable sort does not change the relative order of elements with equal keys[13].
  • Natural behavior — the efficiency of the method when processing already ordered or partially ordered data. An algorithm behaves naturally if it takes this characteristic of the input sequence into account and works better.
  • Use of the comparison operation. Algorithms that use comparison of elements with each other for sorting are called comparison-based. The minimum worst-case complexity for these algorithms is Sorting Algorithms: Complexity, Definition, Properties, Types, and Online Comparison( Sorting Algorithms: Complexity, Definition, Properties, Types, and Online Comparison), but they differ in flexibility of application. For special cases (data types) there are more efficient algorithms.

Another important property of an algorithm is its scope of application. There are two main types of ordering here:

  • Internal sorting operates on arrays that fit entirely in random-access memory with random access to any cell. Data is usually ordered in place, without additional costs.
    • In modern personal computer architectures, memory paging and caching are widely used. The sorting algorithm must combine well with the caching and paging algorithms in use.
  • External sorting operates on large-volume storage devices, but not with random access, rather with sequential access (ordering of files), that is, at any given moment only one element is «visible», and the cost of seeking back is unreasonably high compared to memory. This imposes certain additional constraints on the algorithm and leads to special ordering methods, usually using additional disk space. Moreover, access to data in external memory is performed much more slowly than operations with random-access memory.
    • Access to the medium is performed sequentially: at each moment in time one can read or write only the element following the current one.
    • The volume of data does not allow it to fit in RAM.

Algorithms are also classified by:

  • the need for additional memory or the absence thereof
  • the need for knowledge of the data structure beyond the comparison operation, or the absence thereof

List of sorting algorithms

Classification of sorting algorithms

Sorting Algorithms: Complexity, Definition, Properties, Types, and Online Comparison

In this table Sorting Algorithms: Complexity, Definition, Properties, Types, and Online Comparison is the number of records to be ordered, and Sorting Algorithms: Complexity, Definition, Properties, Types, and Online Comparison is the number of unique keys.

Stable sorting algorithms

  • Bubble sort — for each pair of indices, a swap is performed if the elements are out of order. Algorithm complexity: Sorting Algorithms: Complexity, Definition, Properties, Types, and Online Comparison.
  • Cocktail sort. Algorithm complexity: Sorting Algorithms: Complexity, Definition, Properties, Types, and Online Comparison.
  • Insertion sort — determine where the current element should be located in the ordered list, and insert it there. Algorithm complexity: Sorting Algorithms: Complexity, Definition, Properties, Types, and Online Comparison.
  • Gnome sort (originally published under the name «stupid sort» for the simplicity of its implementation) — similar to insertion sort. Algorithm complexity — Sorting Algorithms: Complexity, Definition, Properties, Types, and Online Comparison; the recursive version additionally requires Sorting Algorithms: Complexity, Definition, Properties, Types, and Online Comparison of memory.
  • Merge sort — arrange the first and second half of the list separately, then merge the ordered lists. Algorithm complexity: Sorting Algorithms: Complexity, Definition, Properties, Types, and Online Comparison. Requires Sorting Algorithms: Complexity, Definition, Properties, Types, and Online Comparison of additional memory.
  • Tree sort. Algorithm complexity: Sorting Algorithms: Complexity, Definition, Properties, Types, and Online Comparison in the best case, and Sorting Algorithms: Complexity, Definition, Properties, Types, and Online Comparison in the worst. Requires Sorting Algorithms: Complexity, Definition, Properties, Types, and Online Comparison of additional memory.
  • Timsort — a hybrid algorithm (uses insertion sort and merge sort). Algorithm complexity: Sorting Algorithms: Complexity, Definition, Properties, Types, and Online Comparison. Requires Sorting Algorithms: Complexity, Definition, Properties, Types, and Online Comparison of additional memory. Developed for use in the Python language[14].

Unstable sorting algorithms

  • Selection sort — searches for the smallest or largest element and places it at the beginning or end of the ordered list. Algorithm complexity: Sorting Algorithms: Complexity, Definition, Properties, Types, and Online Comparison.
  • Comb sort — algorithm complexity: Sorting Algorithms: Complexity, Definition, Properties, Types, and Online Comparison; an improvement on bubble sort.
  • Shell sort — an improvement on insertion sort. The algorithm's complexity varies depending on the choice of gap-length sequence; with a certain choice (see article), it is possible to achieve a complexity of Sorting Algorithms: Complexity, Definition, Properties, Types, and Online Comparison or Sorting Algorithms: Complexity, Definition, Properties, Types, and Online Comparison.
  • Heapsort (heap sort) — algorithm complexity: Sorting Algorithms: Complexity, Definition, Properties, Types, and Online Comparison; turns the list into a heap, takes the largest element and adds it to the end of the list.
  • Smoothsort — algorithm complexity Sorting Algorithms: Complexity, Definition, Properties, Types, and Online Comparison.
  • Quicksort, in the variant with minimal memory costs — algorithm complexity: Sorting Algorithms: Complexity, Definition, Properties, Types, and Online Comparison — average time, Sorting Algorithms: Complexity, Definition, Properties, Types, and Online Comparison — worst case; widely known as the fastest of the known algorithms for ordering large random lists; the original data set is split into two halves so that any element of the first half is ordered relative to any element of the second half; the algorithm is then applied recursively to each half. Using Sorting Algorithms: Complexity, Definition, Properties, Types, and Online Comparison of additional memory, the sort can be made stable.
  • Introsort — algorithm complexity: Sorting Algorithms: Complexity, Definition, Properties, Types, and Online Comparison, a combination of quicksort and heapsort. Heapsort is used when the recursion depth exceeds Sorting Algorithms: Complexity, Definition, Properties, Types, and Online Comparison.
  • Patience sorting — algorithm complexity: Sorting Algorithms: Complexity, Definition, Properties, Types, and Online Comparison — worst case, additionally requires Sorting Algorithms: Complexity, Definition, Properties, Types, and Online Comparison of memory, and also finds the longest increasing subsequence.
  • Stooge sort — a recursive sorting algorithm with time complexity Sorting Algorithms: Complexity, Definition, Properties, Types, and Online Comparison.

Impractical sorting algorithms

  • Bogosort (also stupid sort) — Sorting Algorithms: Complexity, Definition, Properties, Types, and Online Comparison on average. Randomly shuffle the array, check the order.
  • Permutation sort — Sorting Algorithms: Complexity, Definition, Properties, Types, and Online Comparison — worst-case time. For each pair, the correct order is checked, and every possible permutation of the original array is generated.
  • Bead sort — Sorting Algorithms: Complexity, Definition, Properties, Types, and Online Comparison or Sorting Algorithms: Complexity, Definition, Properties, Types, and Online Comparison, requires specialized hardware.
  • Pancake sorting — Sorting Algorithms: Complexity, Definition, Properties, Types, and Online Comparison, requires specialized hardware.

Algorithms not based on comparisons

  • Bucket sort — requires Sorting Algorithms: Complexity, Definition, Properties, Types, and Online Comparison of additional memory and knowledge of the nature of the data being sorted, going beyond the «rearrange» and «compare» functions. Algorithm complexity: {\displaystyle O(n)}Sorting Algorithms: Complexity, Definition, Properties, Types, and Online Comparison.
  • Radix sort (also digit sort) — algorithm complexity: Sorting Algorithms: Complexity, Definition, Properties, Types, and Online Comparison; requires Sorting Algorithms: Complexity, Definition, Properties, Types, and Online Comparison of additional memory.
  • Counting sort. Algorithm complexity: Sorting Algorithms: Complexity, Definition, Properties, Types, and Online Comparison. Requires Sorting Algorithms: Complexity, Definition, Properties, Types, and Online Comparison of additional memory.

Other sorting algorithms

  • Topological sorting
  • External sorting

Comparison of insertion, exchange, and selection sorting algorithms

Sorting Algorithms: Complexity, Definition, Properties, Types, and Online Comparison

Sorting Algorithms: Complexity, Definition, Properties, Types, and Online Comparison

Sorting Algorithms: Complexity, Definition, Properties, Types, and Online Comparison Sorting Algorithms: Complexity, Definition, Properties, Types, and Online Comparison

Sorting Algorithms: Complexity, Definition, Properties, Types, and Online Comparison

Sorting Algorithms: Complexity, Definition, Properties, Types, and Online Comparison

Sorting Algorithms: Complexity, Definition, Properties, Types, and Online Comparison

Sorting Algorithms: Complexity, Definition, Properties, Types, and Online Comparison

Efficiency of sorting algorithms

Sorting Algorithms: Complexity, Definition, Properties, Types, and Online Comparison

Sorting strings

One of the most frequent applications of sorting algorithms is the sorting of strings. This is usually done as follows: first the set of strings is sorted by the first character of each string, then each subset of strings having the same first character is sorted by the second character, and so on until all the strings are ordered. In this case, a missing character (when comparing a string of length N with a string of length N+1) is considered smaller than any character.

Applying this method to strings that represent numbers in their natural written form produces counterintuitive results: for example, «9» turns out to be greater than «11», since the first character of the first string has a greater value than the first character of the second. To fix this problem, a sorting algorithm can convert the strings being sorted into numbers and sort them as numbers. Such an algorithm is called «numeric sorting», while the one described earlier is called «string sorting». Likewise, in practice an effective way to solve the problem of sorting strings containing numbers is to add a certain number of zeros before the number, so that «011» would be considered greater than «009» because of the zeros.

See also

  • Big O
  • Time complexity of an algorithm
  • Bubble sort
  • Bubble sort
  • Cocktail sort
  • Cocktail sort
  • Insertion sort
  • Insertion sort
  • Gnome sort
  • Gnome sort
  • Merge sort
  • Merge sort
  • Tree sort
  • Tree sort
  • Timsort
  • Timsort
  • Selection sort
  • Selection sort
  • Comb sort
  • Comb sort
  • Shell sort
  • Shell sort
  • Heapsort
  • heap sort
  • Heapsort
  • Smoothsort
  • Smoothsort .
  • Quicksort
  • Quicksort

Comments

Юрий Вагин 22-05-2023
Все приведенные алгоритмы - настоящая ерунда.Наберите NewBlock sort в yandex и увидите супер.O(n*log(n/2))
Админ 22-05-2023
что вы имеете ввиду NewBlock sort - это новый алгоритм сортировки? чтото яша ничего нормального не выдает

To leave a comment

If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

Lectures and tutorial on "Algorithms"

Terms: Algorithms