The example of this article tells the implementation and simple analysis of several Java sorting algorithms. Share it for everyone for your reference. The specifics are as follows:
Package test; Public Class First {/*Ordinary insertion sorting*/Public Void Insertsort (int [] list) {int i, J; List [0] = -999; // It is equivalent to setting a surveillance sentinel without judging whether it is determined whether Vietnamese, // but the request of the array starts from the second number. I = 1 starts to store for (i = 1; i <list.Length; i ++) {j = i; about (list [j] <list [j -1 1 ]) {int Temp = List [j]; list [j] = list [j -1]; list [j - 1] = test; j = j -1; On the basis, add binary search*/place void bininsertsort (int [] r, int low, int high) {for (int i = low+1; i <= high; i ++) {int Temp = r [i]; // Save the element int hi = i -1; int Lo = Low; // Set the initial interval while (lo <= hi) {// Fold half to confirm the inserting position int mid = (lo + hi) / 2; if (if (if (IF (if (if + hi) / 2; if (if temp <r [mid]) hi = mid -1; elselo = mid + 1;} for (int j = i -1; j> hi; j -) r [j + 1] = r [j]; / / /Mobilize element R [hi + 1] = test; // Insert element}/ *Hill sort or shell */public void shellsort (int [] r, int low, int high, int [] delta) {for (for (for (for (]. int k = 0; k <delngth; k ++) shellinsert (R, Low, HIGH, Delta [K]);} Private void shellinsert (int [] r, int low, int deldak) {for (int I = Low+Deltak; I <= HIGH; I ++) if (R [i] <r [I-Deltak]) {int Temp = R [i]; int j = I-Deltak; for (; j> = Low && Temp <r [j]; j = j-deltak) r [j+deltak] = r [j]; r [j+deltak] = test;}/*Simply select exchanges*/public void selectsort (int [] R, int Low, int High) {for (int k = low; k <hIGH -1; k ++) {// as n -1 trips to int min min = k; for (int i = min+1; i <= high; i ++) // Select the element with the smallest keyword if (R [i] <r [min]) min = i; if (k! = min) {int Temp = r [k]; // The minimum keyword with the smallest keyword Element and element r [k] exchange r [k] = r [min]; r [min] = test;} // end of if}}/*Heap sort-large top pile*/public void heapsort (int [] r) {int n = r.Length-1; for (int i = n/2; i> = 1; i-) heapadjust (r, i, n); for (int i = n; i> 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; i-) {int Temp = r [1]; r [1] = r [i]; r [i] = test; heapadjust (R, 1, I-1); (int [] r, int low, int high) {int Temp = r [low]; for (int j = 2 * low; j <= high; j = j * 2) {if (j <hIGH && R [ j] <r [j+1]) j ++; if (temp> r [j]) break; r [low] = r [j]; low = j;} r [low] = test; (String [] args) {first fs = new first (); int [] a = {100, 9, 9, 9, 7, 7, 0, 99, 55, 7, 5, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 4, 4, 5, 4 , 3, 2, 1}; int [] k = {5,3,1}; // fs.insertsort (a); // fs.bininsertsort (a, 0, a.Length - 1); // fs .shellsort (a, 0, a.length-1, k); // fs.selectsort (a, 0, a.length-1); .length; I ++) {system.out.println (a [i]);}}}
Insert sorting, exchange sorting, selection sorting, merger sorting and other sorting methods have a common characteristic, that is, they all determine the relative position between elements through comparative elements, that is, the above sorting methods are based on comparison. Sorting method. Below, we will compare and summarize based on comparative sorting methods.
We mainly compare the sorting methods from the average time complexity of the algorithm, the worst time complexity, the complexity of the space, and the stability of the sorting.
Sorting method average time complexity The worst time complexity complexity The complexity of the space and stability of the stability directly inserted (n2) (n2) ο (1) stable foam sorting (n2) ο (n2) ο (1) stable and fast sorting ) (N log n) l (n2) ο (log n) unstable and simply select sort ο (n2) ο (1) ο (1) unstable heap sort (n log n) ο (n log n) ο (n log n) ο (n log) 1) Unstable merger sort ο (n log n) ο (n log n) ο (n) stable
In terms of time performance, fast sorting is the best actual performance in all sorting algorithms. However, the time performance of fast sorting in the worst case is not as good as heap sorting and merging sorting. This can be avoided by changing rapid sorting. A random and fast sort that randomly selects the pivot element can make the worst case of the worst situation very small. It can be considered in actual use. In the comparison of stack sorting and merger sorting, when N is large, the time required for mergers and sorting is less, but it requires more auxiliary storage space.
From the perspective of method stability, most of the time complexity is ο (N2) sorting is a stable sorting method. Except for simply selection of sorting. Most of the sorting methods with good performance, such as fast sorting, stacking sorting, and Hill's sorting are unstable. Generally speaking, the sorting method of the comparison between the sorting process is stable.
In addition, the stability of the sorting method is determined by the method itself. For unstable sorting methods, regardless of its description form, an unstable instance can always be found.
In summary, no one of the sorting methods discussed above is absolutely optimal. In the actual use process, appropriate sorting methods should be selected according to different situations.
It is hoped that this article is helpful to everyone's Java program design.