The Classic sorting algorithm

Source: Internet
Author: User
the classic sorting algorithm

These days to review the sequencing of this module, sorting algorithm in the daily work of programmers is essential, sometimes we unknowingly used to sort, this is because the high-level language system has been relatively perfect package and optimize the sorting algorithm, and in the written test, interview and so on we can see it figure. The following combination of the three-year textbook: Strict version of the "Data structure", to say that these several classic sorting algorithm, if there is no right to welcome correct.

First of all, we should first say the basic concept (according to the book), change inseparable from the concept, no concept no rules, that can not.

First, bubble sort

Second, direct selection of the sort

Third, direct insertion sort

Iv. sort of Hill

Five, heap sorting

Vi. Merge Sort

Seven, quick sort

Eight, various algorithms efficiency competition

1: Internal sorting and external sorting (we focus on internal sorting (as we most commonly use to)) within the sort: the time data objects in the sort are all stored in the sort of memory. Out of order: the number of time objects in the sorting is too many, not at the same time stored in memory, according to the ordering requirements, constantly moving between, external memory.

2: The sorting method varies by arrangement (move interchange), which can be summed up in five categories: Insert Sort, select sort, swap sort, merge sort, and assign sort (some book directly say cardinality). The insertion sort mainly includes the direct insertion sort and the hill sort two; the selection sort mainly includes the direct selection sorting and the heap sorting, the Exchange sort mainly includes the bubble sort and the quick sort; The merge sort mainly includes two-way merging (common merge sort). The allocation sort mainly includes the box sort and the cardinal sort.

3: Stable stability Ordering: Suppose that in the file to be sorted, there are two or more than two records with the same keyword, after sorting by some sort, if the relative order of the elements of these same keywords remains unchanged, then this sort method is stable. bubbling, inserting, cardinality, merging belong to stable sort; selection, fast, hill, heap belong to the unstable sort.

4: Chart helps everyone to remember:

Various sorting algorithms
Category Sorting methods Complexity of Time Stability
Average situation Best case Worst case scenario
Insert Sort Insert directly
O (N2)
O (N) O (N2) Stability
Hill sort
O (n1.3) O (N) O (N2) Not stable
Select sort
Direct selection
O (N2) O (N2) O (N2) Not stable
Heap Sort
O (NLOG2N) O (NLOG2N) O (NLOG2N) Not stable
Exchange sort
Bubble sort
O (N2) O (N) O (N2) Stability
Quick Sort
O (NLOG2N) O (NLOG2N) O (N2) Not stable
Merge sort
O (NLOG2N) O (NLOG2N) O (NLOG2N) Stability
Base sort
O (d (r+n))
O (d (N+RD)) O (d (r+n)) Stability

One, bubble sort bubble sort is also called bubble sort, everybody knows, learn C language that will the loop structure of that this do example, bubble sort process is very simple, but efficiency is too low. 1: Brief process: First compare a record's keyword to the second record's keyword if you exchange two records in reverse order, and then compare the second record and the third record's keywords ... until n-1 a record. The first n record of the keyword is compared, the above is called a bubbling sort, and then a second trip, Working with N-1 Records ... (Can be disturbed by this method, the actual application can be reversed or positive order) 2: Sorting efficiency: If the beginning of the order of the elements need to be ordered, then just a trip, need to n-1 the keyword comparison, if the reverse order to do N (n-1)/2 comparisons, so the time complexity is O (N2), So the efficiency of super low, generally not recommended to use 3: Sort process diagram: 4: Concrete algorithm implementation (can have many forms):

Related Article

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.