The example of this article tells the sorting of the Java sort algorithm summary. Share it for everyone for your reference. The specific analysis is as follows:
In 1991, Robert W. Flayd, a professor at the Department of Computer Sciences at the Department of Computer Sciences at Stanford University, and J. Williams in 1964 jointly invented the famous Heap SORT (Heap Sort) Essence This article mainly introduces Java to the heap sorting.
HEAPSORT refers to a sorting algorithm designed by the pile (heap) data structure, which can use the characteristics of the array to quickly locate the element of specified indexes. The stacking order is an unstable sorting method. The auxiliary space is O (1), and the worst time complexity is O (NLOG2N). The average performance of the heap sorting order is closer to the worst performance.
The stacking order uses the characteristics of the maximum (or minimum) of the keywords of a large pile (or small root pile) pile top record, which makes the record of the maximum (or minimum) keyword in the current disorderly area simple.
(1) Basic ideas sorted with a large pile
① First build the initial file R [1..N] into a large root pile. R [n] exchange, thereby obtaining a new disorderly area R [1.. N-1] and an orderly area R [n], and satisfy R [1.. N-1] .keys≤R [n] .key
③ Since the new root R [1] may violate the nature of the heap after the exchange, the current non-orderly region R [1.. N-1] should be adjusted into a heap. Then the keywords in the keywords in R [1..N-1] again are exchanged with the last record R [N-1] in the interval, which obtained a new disorderly region R [1..... N-2] and an orderly area R [N-1..n], and still satisfy the relationship R [1..-..N-2] .keys≤r [n-1..n] .keys, also to r [1..N-2] Adjust to the heap.
Nympho
Until there are only one element in the disorderly area.
(2) Basic operation of the sorting algorithm of a large roots:
① Initialize operation: Construction of R [1..n] into the initial heap;
② Basic operation of each sort: Exchange the last record of the pile record R [1] in the current disorderly area, and then adjust the new disorderly area into a pile (also known as rebuilding).
Notice:
① Just do N-1 sorting and select a larger N-1 keyword to increase the file in order.
② Sorting with small roots is similar to a large roots, but its sort results are decreasing and orderly. The stacking order and direct selection of sorting are opposite: at any moment, the disorderly region is always before the orderly area, and the orderly area is gradually expanding from the rear of the original vector to the entire vector.
Code implementation:
Public class test {public static int [] heap = {10, 32, 1, 9, 5, 7, 0, 4, 3}; // Preset data array Public Static void Main (String ARGS []) {) {) { int i; // The cycle count variable int index = heap.length; // data index variable system.out.print ("" before sort: "); for (i = 1; i <inDex -1; i ++) System.out .printf ("%3s", heap); System.out.println (""); heapsort (index- 2); // Heap System.out.print ("After sorting:"); for (i = 1 ; I <Index -1; I ++) System.out.printf ("%3s", Heap); System.out.println ("");} / *** Create a pile* / PUBLIC VOID CREAP (INT ROOT, int index) {int i, j; // cycling count variable int temp; // temporarily stored variable int Finish; // determine whether the heap is established to complete the J = 2 * ROOT; // The submp = head [root] ; // The root value of heap fINISH = 0; // The preset stack is established without completion. [J] <heap [j+1]) j ++; if (temp> = heap [j]) finish = 1; // Pack to complete the ELSE {heap [j / 2] = heap [j]; // = The current node j = 2 * j;}} Heap [j / 2] = Temp; // parent node = root value} public static void heapsort (int index) {int i, j, temp; Head for (i = (index / 2); i> = 1; i-) Createheap (i, index); // Start the stack sort for (i = index-1; i> = 1; i------- {Temp = Heap; // Heap's root value and the last value exchange heap = head [1]; heap [1] = testheap (1, i); // Reconstruction System.out.print (// "Sorting:"); for (j = 1; j <= index; j ++) system.out.printf ("%3s", heap [j]); System.out.println ("");}}}}}}
The heap can be regarded as a tree, and the height of the node in the height can be defined as the number of the longest simple drop path from the node to the leaf node; the height of the defined height is the height of the tree root. We will see that some of the basic operations on the heap structure are mostly proportional to the height of the tree, which is O (LGN). Through reading this article, I hope to help you.
It is hoped that this article is helpful to everyone's Java program design.