Ten Classic sorting algorithms

Source: Internet
Author: User

Common Classic Sorting algorithms

Common Classic Sort

Nonlinear time comparison class ordering : By comparison, the relative order between elements is determined, because its time complexity cannot break through O (Nlogn), so it is called nonlinear time comparison class ordering.

linear time Non-comparative class ordering : The relative order between elements is not determined by comparison, it can break through the lower bound of time based on the comparison sort and run in linear time, so it is called linear time non-comparison class ordering. Time Complexity & spatial complexity & stability

Time Complexity & spatial complexity & stability

Stable : If A is originally in front of B, and a=b, after sort a is still in front of B.

Instability: If A is originally in front of B, and A=b, then a may appear behind B.

time Complexity : The total number of operations on the sorted data. Reflects the regularity of the number of operations when n changes.

spatial Complexity : a measure of how much storage space is temporarily occupied by an algorithm while it is running, as S (n) =o (f (n)). Bubble sort (Bubble sort)

Bubble sort (Bubble sort) is a simpler sort algorithm in the field of computer science.

It repeatedly visited the sequence to sort, comparing two elements at a time, and swapping them out if they were wrong in the order. The work of the sequence of visits is repeated until no more need to be exchanged, that is, the sequence is sorted.

The name of this algorithm is because the larger the element will slowly "float" to the top of the sequence of the series, so the name "bubble sort".

Bubble sort (Bubble sort) Select sort (Selection sort)

Compare the previous element in the array with the size of the next element, if the following element is smaller than the previous element with a variable K to remember his position, and then the second comparison, the front "next element" has now become "the previous element", continue with his "latter element" To compare if the next element is smaller than he would use the variable K to remember its position in the array (subscript), and when the loop ends, we should find the lowest number of the subscript, and then to judge, if the index of the element is not the first element subscript, let the first element to exchange values with him, This will find the smallest number in the entire array. Then find the second small number in the array, let him swap the value with the second element in the array, and so on.

Select sort (Selection sort) Insert sort (insertion sort)

There is an ordered sequence of data that requires inserting a number in the already-sorted data sequence, but the data sequence is still ordered after insertion, and a new sorting method is used-insert sort, the basic operation of inserting a sort is to insert a data into the ordered data that is already sorted, In order to obtain a new, number plus one ordered data, the algorithm is suitable for the ordering of a small amount of data, the time complexity of O (n^2). is a stable sorting method. The insertion algorithm divides the array to be sorted into two parts: the first part contains all the elements of the array, except for the last element (where an array has more space to insert), and the second part contains only that element (that is, the element to be inserted). After the first part is sorted, the last element is inserted into the first part of the sequence.

The basic idea of inserting a sort is that each step inserts a record to be sorted in the appropriate position in the previously sorted file by the size of its key value until all is inserted.

Insert sort (Insertion sort) Hill sort (Shell sort)

The Hill sort (Shell's sort) is a more efficient and improved version of the direct insertion sorting algorithm, which is also known as "narrowing incremental sorting" (diminishing Increment sort). Hill Sort is a non-stable sorting algorithm. The method was named after D.l.shell was introduced in 1959.

Hill sort is to group records by a certain increment of the subscript, sorting each group using the direct insertion sorting algorithm; As the increments gradually decrease, each group contains more and more keywords, when the increment is reduced to 1 o'clock, the entire file is divided into a group, the algorithm terminates.

Hill sort (Shell sort) Merge sort (merge sort)

Merge sort (Merge-sort) is an efficient sorting algorithm based on merging operation, which is a very typical application of divide-and-conquer method (Divide and Conquer). The ordered Subsequence is merged to obtain a fully ordered sequence, i.e., the order of each subsequence is ordered, and then the sequence of sub-sequences is ordered. If two ordered tables are combined into an ordered table, they are called two-way merging.

Merge sort fast sorting (quick sort)

Quick Sort (Quicksort) is an improvement to the bubbling sort.

Quick Sort by C. A. R. Hoare was introduced in 1962. Its basic idea is: by a trip to sort the data to be sorted into two separate parts, one part of all the data is smaller than the other part of all the data, and then the two parts of the data are quickly sorted by this method, the entire sorting process can be recursive, so as to achieve the entire data into an ordered sequence.

Fast sorting (Quick sort) heap sort (heap sort)

Heap Ordering (heapsort) is a sort of sorting algorithm designed by using the data structure of the pile-up tree (heap), which is a sort of selection. The elements of the specified index can be quickly positioned using the features of the array. The heap is divided into Dagen and small Gan, which are completely binary trees. The Dagen requirement is that the value of each node is not more than the value of its parent node, i.e. A[parent[i]] >= a[i]. In the non-descending order of the array, it is necessary to use the large root heap, because according to the requirements of Dagen, the maximum value must be at the top of the heap.

Heap sort (heap sort) count sort (counting sort)

The Count sort is a non-comparison based sorting algorithm, which was proposed by Harold H. Seward in 1954. Its advantage is that when sorting integers within a certain range, its complexity is 0 (n+k) (where K is the range of integers), faster than any comparison sorting algorithm.

This is, of course, a sacrifice of space for time, and when O (k) >o (N*log (n)) it is less efficient than comparison-based sorting (the time complexity of the comparison-based ordering is O (N*log (n)), such as merge sort, heap sort)

Count sort (counting sort) bucket sort (bucket sort)

Bucket sort (bucket sort) or so-called box sorting is a sort algorithm that works by splitting an array into a finite number of buckets. Each bucket is sorted separately (it is possible to use a different sorting algorithm or recursively continue to sort by using the bucket sort). Bucket sequencing is an inductive result of pigeon nest sorting. When the values in the array to be sorted are evenly distributed, the bucket is sorted using linear time (Θ (n)). But the bucket sort is not a comparison sort, and he is not affected by the O (n log n) lower bound.

Bucket sort (bucket sort) Cardinal sort (Radix sort)

The radix sort (radix sort) belongs to the "distributive sort" (distribution sort), also known as the "bucket method" (bucket sort) or bin sort, as the name implies, it is a part of the information through the key value, the elements to be sorted into some "barrels", In order to achieve the role of sequencing, the cardinal ranking method is a sort of stability, its time complexity is O (Nlog (r) m), where R is the base taken, and M is the number of heaps, at some point, the cardinality of the sorting method is more efficient than other stability ranking method.

Base sort (Radix sort)

Ten Classic sorting algorithms

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.