8 major sorting tasks required by programmers (2) --- simple sorting by choice and heap sorting (implemented in Java)

Source: Internet
Author: User

Next article: the programmer must know the 8 largest sorting (1) ------- Insert the sorting directly, and the hill sorting (implemented in Java ).

 

3. Simple selection and sorting

(1) Basic Idea: In the number of groups to be sorted, select the minimum number and 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

publicclass selectSort {    public selectSort(){       int a[]={1,54,6,3,78,34,12,45};       int position=0;       for(int i=0;i<a.length;i++){                      int j=i+1;           position=i;           int temp=a[i];           for(;j<a.length;j++){           if(a[j]<temp){              temp=a[j];              position=j;           }           }           a[position]=a[i];           a[i]=temp;       }       for(int i=0;i<a.length;i++)           System.out.println(a[i]);    }}

 

 

4. Heap sorting

(1) Basic Idea: heap sorting is a kind of tree-based selection and sorting, which effectively improves 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

Create a heap at the remaining node, and then swap out the maximum number.

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

(3) implemented in Java

Import Java. util. arrays; publicclass detail {INTA [] = {49,38, 65,97 }; public heapsort () {heapsort (a);} public void heapsort (INT [] A) {system. out. println ("start sorting"); int arraylength =. length; // cyclically build heap for (INT I = 0; I <arrayLength-1; I ++) {// build heap buildmaxheap (A, arrayLength-1-i ); // swap heap top and last element swap (A, 0, arrayLength-1-i); system. out. println (arrays. tostring (A) ;}} private void swap (INT [] data, int I, Int J) {// todo auto-generated method stub int TMP = data [I]; data [I] = data [J]; data [J] = TMP;} // create a large top heap privatevoid buildmaxheap for the data array from 0 to lastindex (INT [] data, int lastindex) {// todo auto-generated method stub // start from the parent node of the node (last node) at lastindex for (INT I = (lastIndex-1)/2; i> = 0; I --) {// K Save the node int K = I; // If the subnode of the current K node has a while (K * 2 + 1 <= lastindex) {// int biggerindex = 2 * k + 1 index of the Left subnode of the k node; // If biggerindex is smaller than lastindex, that is, if (biggerindex <lastindex) {// if the value of the right subnode is large, if (data [biggerindex] <data [biggerindex + 1]) {// biggerindex always records the index of a large subnode biggerindex ++ ;}} // if the value of a K node is smaller than the value of a large sub-node, if (data [k] <data [biggerindex]) {// exchange their swap (data, K, biggerindex); // assign biggerindex to K to start the next loop of the while loop, and re-ensure that the value of K nodes is greater than the value of its left and right subnodes K = biggerindex ;} else {break ;}}}}}

 

 

Next article: The 8-level sorting that programmers must know (3) ------- Bubble sorting and quick sorting (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.