Common sorting methods include insert sorting, select sorting, Bubble sorting, Merge Sorting, fast sorting, and heap sorting. The expected time of these sorting methods is T (n)> O (nlgn ). the linear sorting methods include count sorting, base sorting, and bucket sorting. The selection of sorting methods should consider several important features: time complexity, space complexity, stability, constant factor, algorithm implementation difficulty, input dependence, etc. Based on these factors, linear sorting is not necessarily better than comparative sorting in practical applications.
The following are common sorting implementations (for counting sorting and quick sorting, see the previous blog ):
Public class mysort {# Region insert sorting // <summary> // insert Sorting Algorithm // In-situ replacement: Yes // average performance: O (N ^ 2) /// optimal performance O (n): the sorting of the target has been met; // worst case O (N ^ 2): the inverted sorting of the target; /// </Summary> /// <Param name = "Source"> array to be sorted </param> Public static void insertionsort (INT [] source) {int thelength = source. length; // starting from 2nd, insert the current number to 0 .. in the I-1 array. for (INT I = 1; I <thelength; I ++) {int thecurrval = source [I]; Int J = I-1; // locate the insert position, and move the element later. while (j> = 0 & thecurrval <source [J]) {source [J + 1] = source [J]; j --;} source [J + 1] = thecurrval ;}} # endregion # merge and sort region. /// <summary> /// Merge Sorting /// performance: O (nlog (n), which is stable and independent of input // In-situ replacement: non-in-situ replacement sorting requires O (n) auxiliary space. /// stability: stable. /// </Summary> /// <Param name = "Source"> array to be sorted </param> /// <Param name = "start"> Start position </ param> // <Param name = "end"> end position </param> Public static void mergesort (INT [] source, int start, int End) {If (start + 1 = END) {If (source [start]> source [end]) {int thetmp = source [end]; source [end] = source [start]; source [start] = thetmp;} return;} If (START = END) {return;} int thecenter = (start + end) /2; mergesort (source, start, thecenter); mergesort (source, thecenter + 1, end); mergearray (source, start, thecenter, thecenter + 1, end );} /// <summary> /// merge the two groups. /// </Summary> /// <Param name = "Source"> source array </param> /// <Param name = "S1"> index starting from array 1 </param> /// <Param name = "E1"> array 1 end index </param> /// <Param name = "S2"> array 2 start index </param> /// <Param name = "E2 "> array 2 end index </param> Private Static void mergearray (INT [] source, int S1, int E1, int S2, int E2) {int thelen = e2-S1 + 1; int [] thetemp = new int [thelen]; int I = S1; int J = S2; int K = 0; while (I <= e1 & J <= e2) {If (source [I] <so Urce [J]) {thetemp [k] = source [I]; I ++;} else {thetemp [k] = source [J]; j ++ ;} k ++;} while (I <= e1) {thetemp [k] = source [I]; I ++; k ++;} while (j <= e2) {thetemp [k] = source [J]; j ++; k ++ ;}for (I = 0; I <thelen; I ++) {source [S1 + I] = thetemp [I] ;}# endregion # region heap sorting /// <summary> // heap sorting: // average performance: O (N * lgn), the performance depends on the input, and the performance is best to sort the input in reverse order (the maximum heap, the target is in ascending order) // stability: unstable; // In-situ replacement: sort by in-situ replacement; // </Summary>/ // <Param name = "A"> </param> Public static void heapsort (INT [] A) {// heap creation O (n) heapbuilder (); int theheapsize =. length; // cyclically obtain the maximum value of the current heap and sort out the remaining heap // o (N * lgn) for (INT I =. length-1; I> 0; I --) {int thetmp = A [0]; A [0] = A [I]; A [I] = thetmp; theheapsize --; heapmax (A, 0, theheapsize );}} /// <summary> /// obtain the left son of the current node /// </Summary> /// <Param name = "I"> index of the current node </param> /// <returns> left son node index </returns> private Static int heapl (int I) {return I * 2 + 1 ;} /// <summary> /// obtain the right son of the current node /// </Summary> /// <Param name = "I"> index of the current node </param> /// <returns> right-son node index </returns> Private Static int heapr (int I) {return I * 2 + 2 ;} /// <summary> /// retrieve the index of the parent node of the current node /// </Summary> /// <Param name = "I"> index of the current node </Param> /// <returns> parent node index </returns> Private Static int heapp (int I) {return (I + 1)/2-1;} // <summary> // downward adjustment The entire specified node makes the current heap maintain the maximum heap nature. Up to lgn adjustments. /// </Summary> /// <Param name = "A"> the heap array to be adjusted </param> /// <Param name = "I"> to be adjusted </param> /// <Param name = "heapsize"> heap size </param> Private Static void heapmax (INT [], int I, int heapsize) {int Thel = heapl (I); int ther = heapr (I); int thelargest = I; if (Thel