In the previous summary, we mainly used the exchange sorting of the algorithm's inner sorting. Next we will continue to use the two types of sorting: Direct selection and heap sorting algorithms.
----- Directly select sorting
Package com. Sort;/*** directly select the Sorting Algorithm * @ author weixing-yang **. Idea: * first, find the largest element and replace it with a [n-1] position. * Then find the largest element in the remaining n-1 elements and replace it with the position of a [N-2. * If this happens, n elements are sorted. */Public class selectsort {public void selectsort (INT [] arr, int N) {int I, j, k; int temp = 0; for (I = 0; I <n-1; I ++) {for (k = I, j = I + 1; j <n; j ++) {If (ARR [k]> arr [J]) k = J;} If (K! = J) {temp = arr [k]; arr [k] = arr [I]; arr [I] = temp ;}}}}
----- Heap sorting
Heap sorting is an improvement of tree-based sorting. It uses less auxiliary space and only needs one element for space exchange.
What is heap? Heap: the key code value of the root node is greater than or equal to the left and right subtree or smaller than the left and right subtree, and the left and right subtree is also heap. If the value of each node is greater than the key code value of the left and right subtree, it is called a big top heap. The value of each node is smaller than the key code value of the left and right subtree. Heap is a Complete Binary Tree.
Two steps are involved in heap sorting: (1) creating a heap; (2) adjusting the heap
The following code practices:
Package COM. sort;/*** heap Sorting Algorithm * @ author weixing-yang ** algorithm idea: * first, build the initial file R [0: n-1] into a large root heap, this heap is the initial unordered zone. * record R [0] (that is, the top of the heap) with the largest keyword is exchanged with the last record R [n-1] In the unordered zone, * The New unordered zone R [0: N-2] and ordered zone R [n-1] are obtained, and R [0: N-2] is satisfied. keys ≤ r [n-1]. key; * since the new root R [0] After switching may violate the heap nature, the current unordered zone R [0: N-2] should be adjusted to heap (rebuild heap ). * Then swap the record R [0] with the maximum keyword in R [0: N-2] with the last record R [N-2] In the interval, * The new disordered zone R [0: n-3] and ordered zone R [N-2: n-1] are obtained, and the relationship R [0 .. n-3]. keys ≤r [N-2: n-1]. keys, * Similarly to adjust R [0: n-3] to heap ...... Until there is only one element R [0] In the disordered area, R [0: n-1] is an ordered sequence. */Public class heapsort {// sorting function public void heapsort (INT [] array) {// filter the array to create a large top heap double Len = array. length-1; for (INT I = (INT) math. floor (LEN/2); I> 0; I --) {heapadjust (array, I, array. length-1);} For (INT I = array. length-1; I> 0; I --) {// swap the heap element with the last element, that is, the output maximum value swap (array, 1, I ); // clear the last bit, the maximum subscript of the array into the I-1. The heapadjust (array, 1, I-1) ;}/// heap creation function is used to form a new heap, m] only the keyword corresponding to S // does not meet the definition of the Big Top heap. Adjust [s, m] to make the big top heap public void heapadjust (INT [] array, int s, int m) {// use the 0 subscript element as the temporary storage unit array [0] = array [s]; // filter for (Int J = 2 * s; j <= m; j * = 2) along the child's larger nodes) {// ensure that J is the subscript of a large child node, and j <m guarantees J + 1 <= m, if (j <M & array [J] <array [J + 1]) {J ++;} If (! (Array [0] <array [J]) {break;} // If the S bit is small, move the older child up to array [s] = array [J]; // if the value of a large child is smaller than the value of S, the heap may be unbalanced. Therefore, the heap where the child is located is filtered by S = J ;} // If the S bit is large, the value remains unchanged; otherwise, the S bit moves down to 2 * s, 4 * s ,... Array [s] = array [0];} // exchange function public void swap (INT [] array, int I, Int J) {int temp; temp = array [I]; array [I] = array [J]; array [J] = temp ;}}
----- Test function
Package COM. sort; public class main {public static void main (string [] ARGs) {int [] array = {25, 36, 45, 40, 12, 34, 55 }; int n = array. length; selectsort select = new selectsort (); heapsort heap = new heapsort (); long start = system. currenttimemillis (); select. selectsort (array, n); // select sort directly // heap. heapsort (array); long end = system. currenttimemillis (); long sum = end-start; system. out. println ("Total number of milliseconds spent in sorting:" + sum); For (INT I = 0; I <array. length; I ++) {system. out. print (array [I] + "");}}}
----- Running result
Total number of milliseconds spent in sorting: 012 25 34 36 40 45 55
The time complexity of direct sorting is N2, the time complexity of fast sorting is nlogn, and the space complexity is O (1 ).
@ -------> In the next article, we will continue to use the two sort algorithms for inserting sorting.