In the previous summary, the main practice of the algorithm in the ordering of the exchange, then we continue to practice the selection of two kinds of sorting: direct selection and heap sorting algorithm.

----- Direct Selection sort

Package com.sort;/** * Direct selection sorting algorithm * @author Weixing-yang * * Algorithm ideas: * first find the largest element, and A[n-1] position replacement. * then find the largest element in the remaining n-1 elements and replace it with the a[n-2] position. * so go on. Know that 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++) {f or (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 the tree selection sort, which uses less auxiliary space and requires only one element for space exchange.

What is a heap? Heap: The key code value of the root node is either greater than the left or right subtree, or is less than the left and right subtree, and the left and right subtree is also a heap. Suppose that the value of each node is greater than the key value of the left and right sub-tree, called the Big Top heap.

The value of each node is less than the key code value of the left and right sub-tree, called the small top heap.

The heap is a completely binary tree .

Heap sequencing has two steps: (1) first build heap; (2) Adjustment heap

The following code practices:

Package com.sort;/** * Heap sorting algorithm * @author Weixing-yang * * Algorithm idea: * First file r[0:n-1] built a large heap, this heap is the initial unordered area; * Then keyword the largest record r[0] (that is, the heap Top) and the last record of the unordered zone r[n-1] Exchange. * This results in a new unordered area R[0:n-2] and an ordered area of r[n-1], and satisfies r[0:n-2].keys≤r[n-1].key. * Because the new root r[0 after swapping may violate the heap nature. Therefore, the current unordered zone r[0:n-2] should be adjusted to a heap (rebuild heap). * then re-r[0:n-2] keyword the largest record r[0] and the last record of the interval r[n-2] exchange, * thus obtaining a new unordered area r[0:n-3] and ordered area R[n-2:n-1]. And still satisfy the relationship r[0..n-3].keys≤r[n-2:n-1].keys, * the same to adjust r[0:n-3] to the heap ... R[0:n-1] is an ordered sequence until the unordered area has only one element r[0]. */public class Heapsort {//Sort function public void heapsort (int[] array) {//array filter, build 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 top element of the heap with the last element, that is, the output maximum swap (array , 1, i); Put the last one out. The array's maximum subscript becomes i-1. From the top of the team to the leaves to adjust to form a new heap. This process is called filtering Heapadjust (array, 1, i-1); }}//Build heap function. Feel "s. M "only s//corresponding keyword does not meet the large top heap definition, by adjusting so that" s,m "becomes the Big Top heap public void Heapadjust (int[] array, int s, int m) {//with 0 Mark element as a staging unit array[0] = Array[s]; Filter down on the child's larger node for (int j = 2 * s; j <= m; J *= 2) {//Guarantee J for larger child node subscript, J < m guarantee J+1 <= m, no Cross-border if (J < m && Array[j] < Array[j + 1]) {j + +; } if (! ( Array[0] < Array[j])) { Break }//If the s bit is small, the larger child should be moved up array[s] = array[j]; The value of the larger child becomes the small value of s bit, which may cause the imbalance of the top heap, so the heap to which it is located is filtered s = j; }//If the s bit is large, the value is the same; otherwise, the s bit moves down to 2*s, 4*s 、。。

。 Array[s] = array[0]; }//Interchange 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 = {$, $, 45,40, N, V, v}; int n = array.length; Selectsort select = new Selectsort (); Heapsort heap = new Heapsort (); Long start = System.currenttimemillis (); select.selectsort (array, n);//Direct selection sort// 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]+ "");}}

-----Execution Results

The time complexity of direct selection sequencing is N2, and the time complexity of high-speed sequencing is NLOGN. The spatial complexity is O (1).

@@ ZZFCTHOTFIXZ >> Next chapter to continue practice two sort algorithms for inserting sorting

The essentials of data structure------direct selection and heap sorting algorithm