Eight sorts of sorting algorithms summarizing __ algorithm

Source: Internet
Author: User
Tags sorts

Learned so many sorting algorithms, has not done a summary, hehe

Bubble Sort

Bubble sort is the slowest sort algorithm. In practical use it is the least efficient algorithm. It compares each element of the array by a Bandi and makes the larger data sink and the smaller data rise. It is an O (n^2) algorithm.

Quick Sort

The quick sort is an in-place sequencing, divide-and-conquer, large-scale recursive algorithm. Essentially, it is the in-place version of the merge sort. The quick sort can consist of four steps below.

(1) If not more than 1 data, direct return.
(2) The leftmost value of the general selection sequence is used as fulcrum data.
(3) The sequence is divided into 2 parts, part of which is greater than fulcrum data, and the other part is less than Fulcrum data.
(4) A recursive sort sequence is used on both sides.

Fast sorting is faster than most sorting algorithms. Although we can write algorithms that are faster than fast sorting in some special cases, there is no faster than it usually does. The quick sort is recursive and is not a good choice for a machine with very limited memory.

Merge Sort

The merge sort breaks down the sequence to be sorted first, from 1 to 2, 2 to 4, and then decomposed, which, when decomposed into only 1 groups, can be sorted and then merged back into the original sequence so that all the data can be sorted. The merge sort is a little faster than the heap sort, but it needs one more memory space than the heap sort, because it requires an extra array.

   Heap Sort

Heap sorting is suitable for situations where data is very large (millions of data).

Heap sorting does not require a large number of recursive or multidimensional staging arrays. This is appropriate for a very large number of data sequences. For example, more than millions of records, because the fast sort, merge sort all use the recursive design algorithm, when the data volume is very large, the stack overflow error may occur.

Heap sorting builds all the data into a heap, the largest data is on top of the heap, and then the heap top data is exchanged with the last data of the sequence. Then rebuild the heap again, swap the data, and sequentially, you can sort all the data.

HillSort

The shell sort reduces the number of data exchanges and movements by dividing the data into groups, sorting each group first, and then inserting all the elements one at a time. The average efficiency is O (NLOGN). The rationality of the grouping will have an important influence on the algorithm. Now use the D.e.knuth grouping method more.

The shell sort is 5 times times faster than the bubble sort, roughly twice times faster than the insertion sort. Shell sorting is much slower than quicksort,mergesort,heapsort. But it is relatively simple, it is suitable for the data volume below 5000 and speed is not particularly important occasions. It is very good for the number of small data series to repeat the order.

Insert Sort

Insert sort by inserting the values in a sequence into a sorted sequence until the end of the sequence. The insert sort is an improvement to the bubbling sort. It is twice times faster than bubble sort. It is generally not necessary to use insert sorting in cases where the data is greater than 1000, or to repeat the sequence of more than 200 data items.

Exchange Sortand select Sort

Both of these sorting methods are the sorting algorithm of the Exchange method, and the efficiency is all O (N2). In the actual application in and bubble sort basically the same status. They are only the initial stage of the sorting algorithm, which is less used in practice.

Cardinality Sort

Cardinality ordering and the usual sort algorithm do not follow the same route. It is a relatively novel algorithm, but it can only be used for the ordering of integers, if we're going to apply the same approach to floating-point numbers, we have to understand the storage format of floating-point numbers and map floating-point numbers to integers in a special way, and then map back again, which is a very cumbersome thing to do, so it's not much to use. And, most importantly, this algorithm also requires more storage space.

Summary

The following is a general table summarizing the characteristics of all the sorting algorithms that are common to us. <?xml:namespace prefix = o/>

Sorting method

Average Time

Worst case scenario

Degree of stability

Extra Space

Note

Bubble

O (N2)

O (N2)

Stability

O (1)

N Hours is better

Exchange

O (N2)

O (N2)

Unstable

O (1)

N Hours is better

Choose

O (N2)

O (N2)

Unstable

O (1)

N Hours is better

Insert

O (N2)

O (N2)

Stability

O (1)

Most of the sorted is better

Base

O (LOGRB)

O (LOGRB)

Stability

O (N)

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.