Let's talk about how to sort the eight major parts of javascript and about the eight major parts of javascript.

Source: Internet
Author: User

Let's talk about how to sort the eight major parts of javascript and about the eight major parts of javascript.

One month after I started school, I have dreamed of a Data Structure algorithm problem in the written examination. I have more fear of the data structure than any "demons and ghosts. It seems necessary to review frequently-used data structures to avoid the "Nightmare" coming true.

The importance of the data organization and other programming basics is needless to say.

Sorting algorithms are classified into internal sorting and external sorting. Memory is used for internal sorting. Here we only discuss internal sorting.

1. Insert sorting: insert sorting directly and sort by hill.

2. Select sorting: Simple selection of sorting and heap sorting

3. Exchange sorting: Bubble sorting and quick sorting

4. Merge and sort

5. Base sorting

Insert sort directly

Basic Idea: In the number of groups to be sorted, assume that the number (n-1) [n> = 2] is already sorted in order, first, we need to insert the nth number to the first ordered number, so that the n number is also sorted in order. We know that the Order is in full order.

Hill sorting

Basic Idea: the number of groups to be sorted by an algorithm is divided into several groups according to a certain increment d (n/2, n is the number to be sorted), and the subscript recorded in each group is different from d. Insert and sort all elements in each group, group them with a small increment (d/2), and insert and sort them directly in each group. When the increment value is reduced to 1, the sorting is completed after direct insertion and sorting.

Simple selection and sorting

Basic Idea: In the number of a group to be sorted, the minimum one is selected to exchange the number at the first position, and then the smallest one is found in the remaining number to exchange the number at the second position, in this way, find the number of the last and last numbers.

Heap sorting

Basic Idea: heap sorting is a kind of tree-based sorting, which effectively improves the Direct selection and sorting.

Sequences with n elements (h1, h2 ,..., hn), if and only if (hi> = h2i, hi> = 2i + 1) or (hi <= h2i, hi <= 2i + 1) (I = 1, 2 ,..., n/2) is called heap. Here we only discuss the heap that meets the conditions of the former. From the definition of heap, we can see that the heap top element (that is, the first element) must be a heap top element (large top heap ). A full binary tree can intuitively represent the heap structure. The heap top is the root, and the others are the left and right subtree. Initially, the sequence of the numbers to be sorted is regarded as a binary tree for sequential storage, and their storage order is adjusted to make it a heap. At this time, the root node of the heap has the largest number. Then, the root node is exchanged with the last node in the heap. Then adjust the preceding (n-1) number to make it heap. Wait until there are only two nodes in the heap and exchange them. Finally, an ordered sequence of n nodes is obtained. According to the algorithm description, heap sorting requires two processes: creating a heap and switching the last element of the heap. Therefore, heap sorting consists of two functions. One is the heap build penetration function, and the other is the function that calls the penetration function repeatedly to implement sorting.

Bubble Sorting

Basic Idea: In the number of a group to be sorted, compare and adjust the two adjacent numbers from top to bottom based on the total number in the range not sorted yet, let a large number sink, a small number rises. That is, when the numbers of two adjacent parties are compared and their sorting and sorting requirements are opposite, they are exchanged.

Quick sorting

Basic Idea: select a benchmark element. Generally, select the first or last element and divide the columns to be sorted into two parts, one of which is smaller than the benchmark element, A part is greater than or equal to the reference element. At this time, the reference element is in the correct position after sorting, and then uses the same method to recursively sort and divide the two parts.

Merge Sorting

Basic sorting: the Merge Sorting method combines two (or more) ordered tables into a new ordered table, that is, the sequence to be sorted is divided into several subsequences, each sub-sequence is ordered. Then combine the ordered subsequences into the overall ordered sequence.

Base sort

Basic Idea: unify all the values to be compared (positive integers) into the same length of digits, and add zero before the number of shorter digits. Then, sort the data by bit. In this way, the sequence is changed to an ordered sequence after the ranking is completed until the sorting is completed by the highest bit.

Code demo address: http://lovermap.sinaapp.com/test/sort.html

Now let's analyze the stability of the eight sorting algorithms.

(Please use the preceding basic sorting ideas to understand the stability of sorting. (The basic eight sorting ideas have been mentioned earlier. I will not repeat them here.) otherwise, they may be vague)

(1) Insert directly to sort: Insert sorting. The comparison starts from the last element of the ordered sequence. If it is larger than it, it is directly inserted after it; otherwise, it is always forward. If you find an element that is equal to the inserted element, insert it to the end of the element that is equal to the inserted element. Insert sorting is stable.

(2) Hill sorting: Hill sorting sorts the elements according to different step sizes. One insert sorting is stable and does not change the relative sequence of the same elements. However, during different insert sorting processes, the same elements may move in their respective insert sorting, and the stability will be damaged, so the hill sorting is unstable.

(3) Simple selection and sorting: If the current element is smaller than an element, and the small element appears after an element equal to the current element, the stability will be damaged after switching. It may be a bit vague. Let's look at a small instance: 858410. During the first scan, 1st elements 8 will be exchanged with 4, the relative sequence of the two 8 in the original sequence is inconsistent with that of the original sequence, so the selected sequence is unstable.

(4) Heap sorting: The heap sorting process starts from n/2 and selects the maximum value (large top heap) or the minimum value (small top heap) for its subnodes ), of course, the choice between these three elements will not undermine stability. But when it is n/2-1, n/2-2 ,... when selecting elements for these parent nodes, it is possible that the nth or second parent node switches the next element, the n/2-1 parent node does not swap the same elements behind it, so the heap sorting is unstable.

(5) Bubble Sorting: As we can see from the previous content, the Bubble Sorting is the comparison between two adjacent elements, and the Exchange also occurs between these two elements. If the two elements are equal, there is no need to exchange them. So the Bubble Sorting is stable.

(6) Fast sorting: When an element in the central element and sequence is exchanged, it is very likely that the stability of the previous element is disrupted. Let's look at a small example: 6 4 4 5 4 7 8 9. In the first sorting, the original sequence of element 4 is destroyed when element 6 is exchanged with element 3 4, therefore, the quick sorting is unstable.

(7) Merge and sort: When one or two elements exist in the Child column, one element is not exchanged, and two elements are not exchanged if the sizes are equal. In the process of merging sequences, if the two current elements are equal, we store the elements in the preceding sequence before the result sequence. Therefore, the merging sorting is also stable.

(8) Base sorting: First sort by low position, then collect; then sort by high level, and then collect; and so on until the highest bit. Sometimes some attributes have a priority order. They are first sorted by low priority and then by high priority. The final order is the highest priority, and the highest priority is the highest priority. Base sorting is based on separate sorting and collected separately, so it is stable.

8 types of sorting, including classification, stability, time complexity, and space complexity:

The above is all the content of this article. I hope you will like it.

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.