Sort by Selection sort: simple selection + heap sorting

Source: Internet
Author: User

A simple choice of sorting

1, thought: Each time the traversal remembers the current smallest (large) elements of the position, and finally only need a single exchange operation can be placed in the appropriate location. Compared with bubble sort, the number of mobile data is few, save time, performance is better than bubble sort.

2. Complexity of Time:

Best: O (N2), positive sequence

Worst: O (N2), reverse order

Average: O (N2)

3. Auxiliary space: O (1)

4, Stability: Unstable, the exchange process may upset the sequence

5, Application occasions: N Small situation

 Public Static voidSelectsort (int[] a) {inti,j,min,t;  for(j = 0;j < A.length; J + +) {min= A[j];//set the first element to be sorted to the minimum valuet = j;//set the location to swap!              for(i = J+1;i < a.length; i++) {                if(Min >A[i]) {min=A[i]; T=i; }            }            if(t! = j) Swap (a,j,t);//swap if not for current position        }    }

Second, heap sequencing

1, thought: Heap sorting is a sort of choice, the whole mainly by the construction of the initial heap + Exchange heap top element and the end element and rebuild the heap two parts. The choice of sorting is mainly the number of comparisons, so from reducing the number of comparisons to improve the selection of sorting, heap sorting takes advantage of the nature of the complete binary tree, the current node I sub-node is (2i,2i+1), such as the large root heap, the current node must be greater than or equal to any of its child nodes.

2. Time complexity: initial build heap O (n), swap heap top and end elements and rebuild (LOGN)

Best: O (NLOGN)

Worst: O (Nlogn)

Average: O (NLOGN)

3. Auxiliary space: O (1)

4, Stability: unstable

1 ImportJava.util.Scanner;2 3  Public classHeapsort {4     //heap Subscript starting from 15     //starts from the current given point s and adjusts downwards6     //large Top heap, get incrementing array7      Public Static voidAdjustheap (int[] A,intSintLen) {8         intrc,i;9rc =A[s];Ten          for(i = s*2; i<= len; i = i*2 ) { One             if(I < len && a[i]<a[i+1]) i++;//take the larger one of the two sub-nodes of S A             if(RC > A[i]) Break;//Compare RC to the current larger sub-node, if large, no further adjustment is required -A[s] = A[i];//take a larger child node to move to S -s = i;//s changes to position after move the         } -A[s] = RC;//Finally, the RC is placed in the corresponding position -     } -      +      Public Static voidHeapsort (int[] a) { -         inti,tmp; +          for(i = (a.length-1)/2;i > 0; i--)  AAdjustheap (A, I, a.length-1); at          for(i = a.length-1; i > 1;i--) { -TMP = A[1]; -A[1] =A[i]; -A[i] =tmp; -Adjustheap (A, 1, i-1); -             } in     } -      to      Public Static voidInsertheap (int[] A,intx) { +         intc = a.length-1, temp,f; -A[C] =x; thetemp = A[c];//node to be inserted at the current point *f = C/2;//parent Node $          while(F>=1 && C! = 1) {Panax Notoginseng             if(Temp < a[f]) Break; -A[C] = a[f];//If the current node is greater than the parent, adjust the parent node down thec =F; +f = C/2; A         } theA[C] =temp; +                  -     } $      Public Static voidMain (string[] args) { $         int[] A =New int[10]; -Scanner cin =NewScanner (system.in); -         intI=0; the          while(i<a.length) { -A[i] =cin.nextint ();Wuyii++; the         } - Heapsort (a);  Wu         //Insertheap (A, 886); -          for(i = 1;i < a.length;i++) { AboutSystem.out.print (a[i]+ ""); $         }  -     } -}

Iii. Summary

1, heap sorting is an improvement of simple selection of sorting, the focus of improvement is: How to reduce the number of key code comparisons .

2, simple selection of sorting in a trip to select only the smallest key code, not a trip to save the results, and thus recorded more than the number of comparisons.

Heap sorting in the selection of the minimum key code , but also find the smaller key code , reduce The number of comparisons in the subsequent choice, thus improving the efficiency of the entire sequencing.

3. Heap sorting is not sensitive to the arrangement state of original Records , which is the most important advantage of heap sorting.

Sort by Selection sort: simple selection + heap sorting

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.