The first is the Eightalgorithms.java file, the code is as follows:

Import java.util.arrays;/* * Implements eight commonly used sorting algorithms: Insert sort, bubble sort, select sort, Hill sort * and quick sort, merge sort, heap sort, and lst cardinality sort * @author gkh178 */public class E IGHTALGORITHMS {//Insert sort: Time complexity 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;while (J >= 0 && a[j] > Temp) {a[j + 1] =a[j];--j;} A[j + 1] = temp;}} Bubble sort: Time complexity O (n^2) public static void Bubblesort (int a[], int n) {for (int i = n-1; i > 0;-i) {for (int j = 0; J & Lt I ++J) {if (A[j] > a[j + 1]) {int temp = A[j];a[j] = a[j + 1];a[j + 1] = temp;}}} Select Sort: 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 index = i;for (int j = i + 1; j < n; ++j) {if (A[j] < min) {min = A[j];index = j;}} A[index] = a[i];a[i] = min;}} Hill sort: Time complexity between 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] > Temp) {a[j + gap] = a[j];j-= gap;} A[j + gap] = temp;}}} Quick sort: Time complexity O (NLGN) public static void QuickSort (int a[], int n) {_quicksort (A, 0, n-1);} public static void _quicksort (int a[], int left, int. right) {if (left < right) {int q = _partition (A, left, right); _qui Cksort (A, 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) {while (left < Right && A[right] >= pivot) {--right;} A[left] = A[right];while (left <right && A[left] <= pivot) {++left;} A[right] = A[left];} A[left] = Pivot;return left;} Merge sort: 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.) {if (left <right) {int mid = left + (right-left)/2;_mer Gesort (A, left, mid), _mergesort (A, mid + 1, right), _merge (A, left, Mid, right);}} public static void _merge (int a[], int. left, int mid, int right) {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] &l T 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 Sort: Time complexity O (NLGN) public static void Heapsort (int a[], int n) {builtmaxheap (A, n);//Establish initial Dagen//swap-first element, and an array of excluded tail elements after swapping for (int i = n-1; I >= 1; i.) {int temp = a[0];a[0] = a[i];a[i] = Temp;upadjust (A, I);}} Create a large root heap of length n public static void builtmaxheap (int a[], int n) {upadjust (A, n);} Adjust the public static void Upadjust (int a[], int n) {//to each element with child nodes, from the back to the root node position for (int i = N/2; I >= 1;-- i) {Adjustnode (A, n, i);}} Adjust the value of the node of the ordinal I to public static void Adjustnode (int a[], int n, int i) {//node has left and right child if (2 * i + 1 <= N) {//the child's value is greater than the value of the node, swap them if ( a[2 * I] > a[i-1]) {int temp = a[2 * i];a[2 * i] = a[i-1];a[i-1] = temp;} The value of the left child is greater than 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[i-1];a[i-1] = temp;} Adjust the Adjustnode (A, N, 2 * i) of the node's left and right child's root node, Adjustnode (A, N, 2 * i + 1);} Node only left child, for the last one has left and right child's node else if (2 * i = = N) {//left child value is greater than 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[i-1];a[i-1] = temp;}}} The time complexity of the radix sort is O (distance (n+radix)), distance is the number of Bits, n is the number of arrays, radix is radix//This method uses the LST method to sort the cardinality, the MST method is not included in//where the parameter radix is the base, typically 10 ; Distance represents the longest number of digits of the array to be sorted; n is the length of the arrays public static void Lstradixsort (int a[], int n, int radix, int distance) {int[] Newa = NE W int[n];//is used for staging arrays int[] Count = new int[radix];//is used for count sorting, the number of elements that hold the current bit value of 0 to radix-1 appears, int divide = 1;//from the last bit to the first t i = 0; i < distance; ++i) {system.arraycopy (A, 0, Newa, 0, N);//array copy to Newa array Arrays.fill (count, 0);//Set the Count array to 0for (int j = 0; J < N; ++j) {in T Radixkey = (Newa[j]/divide)% radix; Gets the value of the current processing bit of the array element count[radixkey]++;} At this time count[]Each element in the save is the number of times the Radixkey bit occurs//calculates the end position of each radixkey in the array, the position ordinal range is 1-nfor (int j = 1; j < Radix; ++j) {count[j] = Count[j] + count[j -1];} Use the principle of counting sorting to achieve a single order, the sorted array output to a[]for (int j = n-1; J >= 0;--j) {int radixkey = (Newa[j]/divide)% Radix;a[count[radix Key]-1] = Newa[j];--count[radixkey];} divide = divide * radix;}}}

Then test the code Testeightalgorithms.java with the following code:

public class Testeightalgorithms {public static void PrintArray (int. a[], int n) {for (int i = 0; i < n; ++i) {System.ou T.print (A[i] + ""); if (i = = n-1) {System.out.println ();}}} public static void Main (string[] args) {for (int i = 1; I <= 8; ++i) {int arr[] = {45, 38, 26, 77, 128, 38, 25, 444, 61 , 153, 9999, 1012, 128};switch (i) {case 1:eightalgorithms.insertsort (arr, arr.length); Break;case 2: Eightalgorithms.bubblesort (arr, arr.length); Break;case 3:eightalgorithms.selectsort (arr, arr.length); Break;case 4: Eightalgorithms.shellsort (arr, arr.length); Break;case 5:eightalgorithms.quicksort (arr, arr.length); Break;case 6: Eightalgorithms.mergesort (arr, arr.length); Break;case 7:eightalgorithms.heapsort (arr, arr.length); Break;case 8: Eightalgorithms.lstradixsort (arr, arr.length, 4); break;default:break;} PrintArray (arr, arr.length);}}}

Finally, the results of the operation are as follows:

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Insert sort, bubble sort, select Sort, hill sort, quick sort, merge sort, heap sort, and LST cardinality sort--java implementation