This article implements eight commonly used sorting algorithms: inserting sorting, bubbling sorting, selecting sorting, Hill sorting, fast sorting, merger sorting, stacking and LST base sorting
The first is Eightalgorithms.java file . The code is as follows:
Import java.util.arrays; /** implements eight commonly used sorting algorithms: inserting sorting, bubbling sorting, selecting sorting, Hill Sorting*, and Quick Sorting, Mergers Sorting, Dedicated Slip and LSTHOR GKH178 */Public Class EightAlgorithms {// Insert Sorting: Time Complex O (N^2) Public Static Void Insertsort (int a [], int n) {for (int i = 1; i <n; ++ i) {{ int Temp = a [i]; int j = i -1; space (j> = 0 && a [j]> test) {a [j + 1] = a [j]; -j;} a [j [j [j + 1] = Temp;}} // Bubble Sorting: Time complexity o (n^2) Public Static void Bubblesort (int a [], int n) {for (int i = n -1; i> 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0; --I) {for (int j = 0; j <i; ++ j) {if (a [j]> a [j +1]) {int Temp = a [j]; a [j] = a [j + 1]; a [j + 1] = test;}}}} // Select Sorting: Time complexity o (n^2) Public Static void Selectsort (int a [], int n) {for (int i = 0; I <n -1; ++ i) {int min = a [i]; int int index = i; for (int j = i +1; j <n; ++ j) {if (a [a [a [a [a [a [a [a [a [a [a [a [a [a [a j] <min) {min = a [j]; index = j;}} a [index] = a [i]; a [i] = min;} // Hill sorting: time complexity is in O of O. (N^2) and O (NLGN) Public Static Void Shellsort (int a [], int n) {for (int gap = n / 2; gap> = 1; gap / = 2) {for (int i = gap; i <n; ++ i) {int Temp = a [i]; int j = i -gap; while (j> = 0 && a [j]> test) {a [j +gap] = a [j]; j -= gap;} a [j + gap] = test;}} // Quick sorting: time complexity o (nlgn) public static void quicksort (int a [], int n) {_quicksort (_quicksort ( a, 0, n-1);} Public Static void left, q -1); _quicksort (a, q + 1, right);}} Public Static int _partition (int a [], int left, int Right) {int Pivot = A [left]; ) {While (left <Right && A [Right]> = Pivot) {--Right;} a [left] = a [right]; about (left <righ && a [left] <= pivot) {++ left ;} a [right] = a [left];} a [left] = pivot; return left;} // Merge sorting: time complexity o (nlgn) public static void mergesort (int a [], int n) { _mergeSort(a, 0 , n-1); } public static void _mergeSort(int a[], int left, int right) { if (left <right) { int mid = left + (right - left) / 2; _mergeSort (A, left, mid); _merGesort (a, mid + 1, right); _merge (a, left, mid, right);} public static void _merge (int a [], Int left, Int mid, int radi ) {int length = right -left +1; int newa [] = new int [length]; for (int i = 0, j = left; i <= length -1; ++ i, ++ j) {newa [i] = a [j];} int i = 0; int j = mid -Left +1; int K = left; for (; i <= mid -left && j <= length -1; ++ k) {if (newa [i] <newa [j]) {a [k] = newa [i ++];} else {a [k] = newa [j ++];} while (i <= mid -left) {a [k ++] = newa [i ++];} While (j <= Right -LEFT) {a [k ++] = newa [j ++];} // Heap sorting: time complexity O (NLGN) Public Void Heapsort (int a [], int n) {builtmaxheap (a, n); // Create the initial large root pile // exchange the first element, and adjust the array of the tail element after the exchange. > = 1; --i) {int Temp = a [0]; a [0] = a [i]; a [i] = test; upadjust (a, i);}} // Create a length n Public static void builtmaxheap (int a [], int n) {upadjust (a, n);} // The array of length n is adjusted to the Public Static void upadjust (int a [], int n) {// Each element with a child node traversing is traversed. } // Adjust the value of the node of the serial number i Public Static Void AdjustNode (int a [], int n, int i) {// Nodes have left and right children IF (2 * i + 1 <= n) {// Right child The value of the value is greater than the value of the node, exchanged them if (a [2 * i]> a [i -1]) {int Temp = a [2 * i]; a [2 * i] = a [i -1]; a [i -1] = test;} // The value of the left child is greater than the value of the node, and exchange them if (a [2 * i -1]> a [i -1]) {int Temp = a [2 * i -1]; a [2 * i -1] = a [i -1]; a [i -1] = Temp;} // Adjust AdjustNode (a, n, 2 * i); Adjustnode (a, n, 2 * i + 1);} // Node is only left child, the last node Else if (2 * i == n) {// The value of the node, exchange them if (a [2 * i -1]> a [i -1]) {int Temp = a [2 * i -1]; a [2 * i -1] = a - 1]; A [i -1] = TEMP;}}} // The time complexity of the base sorting is O (Distance (N+Radix)), the distance is the number of digits, n is the number of array, radix is the base // This method is to use the LST method for base sorting, and the MST method is not included in it // The parameter RADIX is the base, generally 10; the distance indicates that the number of numbers to the array to be sorted; n is the length of the array PUBLIC Static void void vOID lstradixsort (int a [], int n, int radix, int distance) {int [] new int [n]; // Used to temporarily deposit the array of [] count = new int [radix]; // For it for Counting sorting, the number of elements of the current bit of 0 to RADIX-1 appears int Divide = 1; // From the countdown to the first place to the first place to (int i = 0; i <<<iStance ; ++ I) {System.arrayCopy (A, 0, NEWA, 0, N); // The array to be arranged to be in the newa array arrays.fill (count, 0); // Place the number of counts 0 for (int int j = 0; j <n; ++ j) {int RADIXKEY = (newa [j] /divide) % radix; // get the value of the current processing bit of the array element count [radixkey] ++;} // At this time Each element in count [] is saved by the number of times that appears in the radixkey bit // Calculate the end position of each Radixkey in the array, and the position number range is 1-n for (int j = 1; j <radix; ++ j) {Count [J] = Count [J] + Count [J -1];} // Use the principle of counting sorting to achieve a sort. = 0; --j) {int RADIXKEY = (newa [j] / divide) % radix; a [count [radixkey] -1] = newa [j]; -count [radixkey];} divide = divide * radix ;}}}Then test code Testeightalgorithms.java , the code is as follows:
Public Class TesteightAlgorithms {Public Static Void Printarray (int a [], int n) {for (int i = 0; i <n; ++ i) {system.print (a [i] +""); if (i == n -1) {system.out.println ();}} public static void main (string [] args) {for (int i = 1; i <= 8; ++ i) {int ARRR [] = {45, 38, 26, 77, 128, 38, 25, 444, 61, 153, 9999, 1012, 43, 128}; switch (i) {case 1: eightalgorithms.insertsort (Arr, Arr.length ); Case 2: EightAlgorithms.bubblesort (Arr, Arr.Length); Break; Case 3: EightAlgorithms.selectsort (Arr, Arr.Length); Break; Case 4: EIGHT Algorithms.shellsort (Arr, Arr.Length); Break; Case 5: EightAlGorithMS.quicksort (Arr, Arr.Length); Break; Case 6: EightAlgorithms.Mergesort (Arr, Arr.Length); Break; Case 7: EIGHTALGOR iTHMS.HEAPSORT (Arr, Arr.Length); Break; Case 8: EightAlgorithms.lstradixsort (Arr, Arr.Length, 10, 4); Break; DEFAULT: Break;} Printarray (Arr, Arr.length);}}}}} The last run results are as follows:
The above is all the code for Java to implement eight commonly used sorting algorithms. I hope everyone has a better understanding of the C ++ sorting algorithm.