This paper implements eight commonly used sorting algorithms: Insert sort, bubble sort, select Sort, hill sort, quick sort, merge sort, heap sort and lst cardinal order.
The first is the Eightalgorithms.java file , the code is as follows:
Import Java.util.Arrays; * * Implements eight common 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 Eightalgorithms
{//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 < 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: The time complexity is 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 = _partitio
N (A, left, right);
_quicksort (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 to 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 right) {if (left <right) {int mid = left + (rig
Ht-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 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] < 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++];
Stack sort: Time complexity O (NLGN) public static void Heapsort (int a[], int n) {builtmaxheap (A, n);//Create initial large heap
Swap the first and last elements and adjust the array for the trailing element after swapping for (int i = n-1 i >= 1;-I.) {int temp = a[0];
A[0] = A[i];
A[i] = temp;
Upadjust (A, I);
To establish a large root heap of length n, public static void Builtmaxheap (int a[], int n) {upadjust (A, n); (/////on an array of length n) adjust public static void Upadjust (int a[], int n) {//to each element with child node for traversal, from back to root node for (int i = N/2; I >= 1;
-i) {Adjustnode (A, n, i); The value of the node that adjusts ordinal number i is public static void Adjustnode (int a[], int n, int i) {//node has left or right child if (2 * i + 1 <= N) {//Right child's value is greater than the node's value, swapping 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, swapping 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 root node of the node's left and right children Adjustnode (A, N, 2 * i);
Adjustnode (A, N, 2 * i + 1); //node only left child, for the last node with left and right children else if (2 * i = = N) {//left child's value is greater than node value, 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; }}//Cardinality the time complexity of sorting is O (distance (n+radix)), distance is the number of digits, n is the number of numbers, radix is cardinality/This method is sorted by the LST method, and the MST method is not included in the /Where the parameter radix is the cardinality, generally 10;distance represents the longest number of digits of the array to be sorted; n array length public static void Lstradixsort (int a[], int n, int radix, int dis
tance) {int[] Newa = new int[n];//for staging array int[] count = new int[radix];//for counting sort, saving the number of elements that have the current bit value of 0 to Radix-1 int divide = 1;
Processed from penultimate to first for (int i = 0; i < distance ++i) {system.arraycopy (A, 0, Newa, 0, N); the array to be arranged is copied to the Newa array Arrays.fill (count, 0);//Set the count array to 0 for (int j = 0; J < N; ++j) {int radixkey = (newa[j)/divide)% r Adix;
Gets the value of the current processing bit of the array element count[radixkey]++; ///At this time count[] Each element holds the number of occurrences of the radixkey bit//calculates the end position of each radixkey in the array, and the position ordinal range is 1-n for (int j = 1; j < Radix;
++J) {Count[j] = Count[j] + count[j-1]; ///using the principle of counting sort to achieve a sort, sorted array output to a[] for (int j = n-1; J >= 0;--j) {int radixkey = (Newa[j]/di
vide)% Radix;
A[count[radixkey]-1] = newa[j];
--count[radixkey];
} divide = divide * radix; }
}
}
Then test 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.out.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[] = {4
5, 38, 26, 77, 128, 38, 25, 444, 61, 153, 9999, 1012, 43, 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, 10, 4);
Break
Default:break;
} printArray (arr, arr.length); }
}
}
Finally, the results of the operation are as follows:
The above is the Java implementation of the eight commonly used sorting algorithm of all the code, I hope that we have a further understanding of C + + sorting algorithm.