Simple selection and sorting (1) Basic Idea: In the number of a group to be sorted, the minimum number is selected and the number at the first position is exchanged; 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) implement public class selectSort {public selectSort () {int a [] = {,} using java; int position = 0; www.2cto.com for (int I = 0; I <. length; I ++) {int j = I + 1; position = I; int temp = a [I]; for (; j <. length; j ++) {if (a [j] <temp) {temp = a [j]; position = j ;}} a [position] = a [I]; a [I] = temp;} for (int I = 0; I <. length; I ++) System. out. println (a [I]) ;}} Heapsort is a sort algorithm designed by using the data structure of the stacked tree (Heapsort, you can use the features of arrays to quickly locate The index element. Heap sorting is an unstable sorting method. The auxiliary space is O (1), and the worst time complexity is O (nlog2n ), the average performance of heap sorting is closer to the worst performance. Heap sorting utilizes the largest (or least) keyword of the top record of a large root heap (or a small root heap) so that the largest (or least) keyword is selected in the current unordered area) keyword record becomes simple. (1) the basic idea of sorting with a large root heap ① first import the initial file R [1 .. n] build a large heap. The heap is the initial unordered zone. ② record R [1] (that is, the heap top) with the largest keyword) exchange with R [n], the last record of the unordered zone, and obtain the new unordered zone R [1 .. n-1] And the ordered zone R [n], and meet the requirements of R [1 .. n-1]. keys ≤ R [n]. key ③ because the new root R [1] After switching may violate the heap nature, the current unordered zone R [1 .. n-1] changed to heap. Then re-set R [1 .. in n-1], the record R [1] with the largest keyword is exchanged with the last record R [n-1] In the interval, and a new unordered zone R [1 .. n-2] and ordered zone R [n-1 .. n], and still satisfies the relationship R [1 .. n-2]. keys ≤ R [n-1 .. n]. keys, also set R [1 .. n-2] adjusted to heap. ...... Until there is only one element in the unordered area. (2) Basic operations of the Sorting Algorithm of the big root heap: ① initialization operation: Set R [1 .. n] is constructed as the initial heap. ② basic operations for sorting each trip: swap the top record R [1] of the unordered zone with the last record in the interval, then adjust the new unordered area to the heap (also weigh and build the heap ). Note: ① A large n-1 keyword can be selected to sort the files in ascending order. ② Sorting with a small root heap is similar to using a large root heap, but the sorting result is descending and ordered. Opposite to Direct selection sorting: In heap sorting, the unordered area is always before the ordered area at any time, and the ordered area gradually expands from the back to the end of the original vector to the whole vector. Code implementation: public class Test {public static int [] Heap = {10, 32, 1, 9, 5, 7, 12, 0, 4, 3 }; // preset data array public static void main (String args []) {int I; // The cyclic count variable int Index = Heap. length; // The data index variable System. out. print ("Before sorting:"); for (I = 1; I <Index-1; I ++) System. out. printf ("% 3 s", Heap); System. out. println (""); HeapSort (Index-2); // heap sorting System. out. print ("sorted:"); for (I = 1; I <Index-1; I ++) Sort E M. out. printf ("% 3 s", Heap); System. out. println ("");}/*** create heap */public static void CreateHeap (int Root, int Index) {int I, j; // The int Temp variable of the cyclic count; // Save the int Finish variable; // determine whether the Heap has been established. j = 2 * Root; // The Index Temp = Heap [Root] of the subnode. // The Root value of the temporary Heap Storage is Finish = 0; // The default Heap creation is not completed while (j <= Index & Finish = 0) {if (j <Index) // find the largest subnode if (Heap [j] <Heap [j + 1]) j ++; if (Temp> = Heap [j]) Finish = 1; // Stack creation completed else {Heap [j/2] = Heap [j]; // parent node = current node j = 2 * j ;}} heap [j/2] = Temp; // parent node = Root value} public static void HeapSort (int Index) {int I, j, Temp; // convert a binary tree to Heap for (I = (Index/2); I> = 1; I --) CreateHeap (I, Index ); // start Heap sorting for (I = Index-1; I> = 1; I --) {Temp = Heap; // The Root value of Heap is exchanged with the last value. Heap = Heap [1]; Heap [1] = Temp; CreateHeap (1, I ); // rebuild the heap System for other values. out. print ("in sorting :"); For (j = 1; j <= Index; j ++) System. out. printf ("% 3 s", Heap [j]); System. out. the println ("") ;}} heap can be regarded as a tree. The height of a node in the heap can be defined as the maximum number of simple drop paths from the current node to the leaf node; defines the height of the heap as the height of the root. We will see that the running time of some basic operations in the heap structure is at most proportional to the height of the tree, which is O (lgn ). By reading this article, I hope to help you