Java programmers must know eight major sorting orders, and java programmers must know

Source: Internet
Author: User

Java programmers must know eight major sorting orders, and java programmers must know

 

1. Insert directly to sort

 

(1) Basic Ideas:In the number of groups to be sorted, assume that the number of the preceding (n-1) [n> = 2] is already

Now we need to insert the nth number into the previous ordered number so that the n number

It is also sorted. This repeats until all the rows are sorted.

 

(2) Instances

 



3) implemented in java

 

 

2. Hill sorting (minimum incremental sorting)

 

(1) Basic Ideas:The number of groups to be sorted by the algorithm is divided into several groups according to a certain incremental d (n/2, n is the number of The number to be sorted), and the subscript difference of each group is 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.

 

(2) instance:

 



(3) implemented in java

 

 

3. Simple selection and sorting

 

(1) Basic Ideas:In the number of groups to be sorted, the minimum number is selected to exchange with the number at the first position;

 

Then find the smallest number in the remaining number and exchange it with the number in the second position, so that the loop ends until the last number is compared with the last number.

 

(2) instance:

 



(3) implemented in java

 

 

4. Heap sorting

 

(1) Basic Ideas:Heap sorting is a kind of tree-based sorting that effectively improves the Direct selection and sorting.

The heap is defined as follows: a sequence 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.

 

(2) instance:

 

Initial sequence:

 

Heap creation:

 



Swap, maximum number of kicks out from the heap

 



 

And so on:The last two remaining nodes in the heap are swapped, kicked out, and sorted.

 

(3) implemented in java

 


 

5. Bubble Sorting

 

(1) Basic Ideas:In the number of groups 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.

 

(2) instance:

 



(3) implemented in java

 

 

6. Quick sorting

 

(1) Basic Ideas:Select a benchmark element. Generally, the first or last element is selected. The column to be sorted is divided into two parts, one of which is smaller than the benchmark element and the other is greater than or equal to the benchmark element, at this time, the reference element is in the correct position after sorting, and then uses the same method to recursively sort the divided two parts.

 

(2) instance:

 



(3) implemented in java

 

 

7. Merge and sort

 

(1) 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 of which is ordered. Then combine the ordered subsequences into the overall ordered sequence.

 

(2) instance:



(3) implemented in java

 


 

8. Base sorting

 

(1) Basic Ideas: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.

 

(2) instance:

 



(3) implemented in java

 


 

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.