On the techniques of realizing eight sort _javascript by JavaScript

Source: Internet
Author: User
Tags sorts

School one months, has many dreams of the written test data structure algorithm, I have more fear of data structure than any "ghosts and goblins." Oh, it seems really necessary to review the commonly used data structure, lest "nightmare" come true.

The importance of programming basics, such as data organizations, is not to say, just get to the point.

Sorting algorithm, which is divided into internal sort and external sort. Internal sorting to use memory, here only discusses the internal sort.

1, insert sort: direct insertion sort and hill sort

2, select sort: Simple select sort and heap sort

3, swap sort: bubble sort and quick sort

4, Merge sort

5, Cardinal Order

Direct Insert Sort

The basic idea: in order to sort the number of a group, assuming that the front (n-1) [n>=2] number is already in order, first to the number of n inserted into the front of the ordered number, so that the number of N is ranked in order. Repeat the loop so that you know the order is all sorted out.

Hill sort

Basic idea: The algorithm first will be sorted by a group of numbers by an increment D (n/2,n to order the number of numbers) into groups, each group recorded in the subscript difference D. A direct insert sort of all elements in each group is then grouped with a smaller increment (D/2), followed by a direct insert sort in each group. When the increment is reduced to 1 o'clock, the sort completes after a direct insert sort.

Simple selection Sort

The basic idea: in the set of numbers to be sorted, select the smallest number and the number of the first position, and then find the smallest interchange with the second position in the remaining number, so that the UN goes to the penultimate number and the last number.

Heap Sort

Basic idea: Heap sequencing is a kind of tree-shape selection, which is an effective improvement to the direct selection sort.

A sequence with n elements (h1,h2,..., HN) that is called a heap if and only if it satisfies (hi>=h2i,hi>=2i+1) or (hi<=h2i,hi<=2i+1) (i=1,2,..., N/2). Only the heap that satisfies the former condition is discussed here. As you can see from the definition of the heap, the top element of the heap (that is, the first element) must be the maximum item (the large top heap). A complete binary tree can visually represent the structure of a heap. The top of the heap is the root and the other is Zuozi and right subtree. Initially, the sequence of numbers to be sorted is considered to be a sequential two-tree, which adjusts their storage order to make it a heap, at which point the number of root nodes of the heap is the largest. The root node is then exchanged with the last node of the heap. Then readjust the number of front (n-1) to make it a heap. And so on, until there are only two nodes in the heap, and exchange them, and finally get an ordered sequence of n nodes. From the description of the algorithm, heap sequencing requires two processes, one is to build a heap, and the other is to exchange positions between the top of the heap and the last element of the heap. So the heap sort has two function components. The first is to build the seepage function of the heap, and the second is to call the function of the infiltration function to realize the sort.

Bubble sort

The basic idea: in order to sort of a group of numbers, the current has not yet lined up in the range of the whole number, from top to bottom of the adjacent two numbers in turn to compare and adjust, so that the larger number to sink, smaller upward. That is, whenever two adjacent numbers are compared and found to be in reverse order, they are interchanged.

Quick Sort

Basic idea: Select a datum element, usually select the first element or the last element, through a scan, the backlog sequence is divided into two parts, one part is smaller than the datum element, the other is greater than or equal to the datum element, when the datum element is in the correct position after it is sorted, and then recursively sorts the two parts divided by the same method.

Merge sort

Basic Sort: Merge (merge) sorting method is to combine two (or more than two) ordered tables into a new ordered table, that is, to divide the sequence into several subsequence, each subsequence is ordered. Then the ordered Subsequence is merged into the whole ordered sequence.

Cardinality sort

Basic idea: Unify all the values to be compared (positive integers) to the same number of digits, preceded by a short number of 0. Then, start at the lowest bit, and then sort it one at a time. This is done from the lowest bit to the highest order, and the sequence becomes an ordered series.

Code Demo Address: http://lovermap.sinaapp.com/test/sort.html

Now we analyze the stability of the 8 sorting algorithms.

(Please use the basic idea of sorting to understand the stability of the order (8 sorts of basic ideas have been said before, here is no longer to repeat) may be a little fuzzy.

(1) Direct insertion Sort : General insertion Sort, the comparison is from the last element of ordered sequence, if larger than it is directly inserted behind it, otherwise it is always forward. If one is found to be equal to the insertion element, it is inserted behind the equality element. The insertion sort is stable.

(2) Hill sort : Hill sort is to insert the elements according to different step size, once the insertion sort is stable, will not change the relative order of the same elements, but in different insert sort process, the same elements may move in their respective insertion sort, stability will be destroyed, So hill sort is unstable.

(3) Simple selection sort : In a trip, if the current element is smaller than an element, and the small element appears behind an element that is equal to the current element, then the stability of the exchange is corrupted. Light said may be a little fuzzy, to see a small example: 858410, the first scan, the 1th element 8 and 4 Exchange, then the original sequence 2 8 of the relative order and the original sequence inconsistent, so the selection of the order is not stable.

(4) Heap sort : The process of heap sequencing is to select the maximum (large top heap) or the smallest (small top heap) with a total of 3 values from the beginning of N/2 and its child nodes, and the choice between these 3 elements certainly does not break the stability. But when for n/2-1, N/2-2, ... When these parent nodes select elements, it is possible that the N/2 parent exchange swaps the subsequent element, while the n/2-1 parent node does not exchange the same element after it, so the heap sort is not stable.

(5) Bubble sort : It is known from the previous content that the bubble sort is the adjacent two element comparison, the exchange also occurs between these two elements, if two elements are equal, do not exchange. So the bubble sort stabilizes.

(6) Quick sort : in the central element and in the sequence of the exchange of elements, it is very likely to the previous elements of the stability of the upset. Or look at a small example: 6 4 4 5 4 7 8 9, the first order, the central element 6 and the third 4 exchange will destroy the original sequence of element 4, so the fast ordering is unstable.

(7) Merge sort : In the Decomposed child column, there are 1 or 2 elements, 1 elements will not exchange, 2 elements if the size is equal will not exchange. In the process of sequence merging, if two current elements are equal, we keep the elements in the preceding sequence in front of the result sequence, so the merge sort is also stable.

(8) Cardinal Order : It is sorted by low order, then collected, sorted by high order, then collected, and so on until the highest bit. Sometimes some attributes are in order of precedence, first by low priority, and then by high priority, the final order is high priority in front, high priority of the same low priority high in the front. Cardinality sorting is based on separate sorting, collected separately, so it is stable.

8 kinds of sorting, stability, time complexity and space complexity summary:

The above mentioned is the entire content of this article, I hope you can enjoy.

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.