The following is a basic sorting algorithm in 7 of the data structures implemented in Java code:
(1) Direct Insert sort
/** Direct Insert Sort **//** array is a reference type, element value will be changed **/public static void Insertsort (int[] table) {/** n-1 scan **/for (int i = 1; i < tabl E.length; i++) {/** Each trip will table[i] into the previously sorted sequence **/int temp = Table[i], j;/** move the previous larger element backward **/for (j = i-1; J >-1 && Temp & Lt TABLE[J]; j--) {table[j + 1] = table[j];} /** Temp value arrives at insertion position **/table[j + 1] = temp;}}
(2) Hill Sort
<span style= "White-space:pre" ></span>/** Hill sort **/public static void Shellsort (int[] table) {/** control increment, increment halved, Several scan **/for (int delta = TABLE.LENGTH/2; delta > 0; Delta/= 2) {/** A number of groups in a trip, with each element inserted directly into the group within its own groups **/for (int i = Delt A i < table.length; i++) {/** currently to be inserted element **/int temp = table[i];/** Delta far **/int j = i-delta;/** A group in front of the larger element moves backward **//** continues to compare with the previous element **/while ( J >= 0 && Temp < Table[j]) {table[j + Delta] = table[j];j-= Delta;} /** Insert element position **/table[j + Delta] = temp;}}}
(3) Bubble sort
<span style= "White-space:pre" ></span>/** bubble sort **/public static void Bubblesort (int[] table) {/** whether to swap tokens **/ Boolean exchange = true;/** There is an exchange for the next trip, up to n-1 **/for (int i = 1; i < table.length && Exchange; i++) {/** assumed element not delivered Swap **/exchange = false;/** Once compare, interchange **/for (int j = 0; J < Table.length-i; J + +) {/** reverse order, Interchange **/if (Table[j] > table[ J + 1]) {int temp = Table[j];table[j] = table[j + 1];table[j + 1] = temp;/** has interchange **/exchange = True;}}}
(4) Quick Sort
<span style= "White-space:pre" ></span>/** quick sort **/public static void QuickSort (int[] table) {QuickSort ( Table, 0, table.length-1);} /** a quick sort, recursive algorithm **/private static void QuickSort (int[] table, int low, int. high) {//low, high the lower and upper bounds of the specified sequence/** sequence valid **/if (lo W < high) {int i = low, j = high;/** The first value as the base value **/int Vot = table[i];/** a trip sort **/while (i! = j) {/** looks for smaller values from backward forward **/while (I < J && Vot <= Table[j]) j--;if (i < j) {/** Smaller elements move forward **/table[i] = table[j];i++;} /** from front to back looking for larger values **/while (i < J && Table[i] < Vot) I++;if (i < j) {/** larger elements move backwards **/table[j] = table[i];j--;} }/** the final position of the reference value **/table[i] = vot;/** Front terminal sequence Reorder **/quicksort (table, Low, j-1),/** post-terminal sequence Reorder **/quicksort (table, i + 1, high) ;}}
(5) Direct Select sort
<span style= "White-space:pre" ></span>/** Direct selection sort **/public static void Selectsort (int[] table) {/** n-1 sequencing * * /for (int i = 0; i < table.length-1; i++) {/** Each trip finds the smallest element in a subsequence starting from table[i] **//** set the first Data element min **/int min = i;/** Find in subsequence Minimum value **/for (int j = i + 1; j < Table.length; J + +) if (Table[j] < table[min])/** Remember minimum element subscript **/min = j;/** Swap the smallest element of the trip to the front **/if (min! = i) {int temp = Table[i];table[i] = table[min];table[min] = temp;}}}
(6) Heap sorting
<span style= "White-space:pre" ></span>/** heap sort **/public static void Heapsort (int[] table) {int n = table.lengt h;/** Create the minimum heap **/for (int j = N/2-1; J >= 0; j--) sift (table, J, n-1),/** each trip will exchange the minimum value to the back, and then adjust the pile **/for (int j = n-1; j > 0; j--) {int temp = table[0];table[0] = table[j];table[j] = temp;sift (table, 0, j-1);}} /** adjusts the subtree with the root of low to the minimum heap **/private static void Sift (int[] table, int low, int.) {/** Low, High is the root of the sequence lower and upper bounds **//** subtree **/int i = low;/** J is the left child of the i node **/int j = 2 * i + 1;/** gets the value of the i element **/int temp = table[i];/** along the smaller child nodes filter down **/while (J <= High) { /** array element comparison (changed to < for maximum heap) **/if (J < high && table[j] > table[j + 1])/** J for the smaller of the children **/j++;/** if the parent node value is larger (change to < Max heap) **/if (temp > Table[j]) {/** the smaller value of the child node is moved up **/table[i] = table[j];/** I, j down one layer **/i = J;j = 2 * i + 1;} Elsej = high + 1;} /** position after the original root value of the current subtree is adjusted **/table[i] = temp;}
(7) Merge sort
<span style= "white-space:pre" ></span>/** merge sort **/public static void MergeSort (int[] X) {/** sorted sub-sequence length, initial value of 1 * */int n = 1;/** y array length with X array **/int[] y = new Int[x.length];d o {/** A trip merge, merges each subsequence in the x array into y **/mergepass (x, Y, n);/** subsequence length doubled **/ n *= 2;if (n < x.length) {/** merges each subsequence in the Y array into the X **/mergepass (y, X, n); n *= 2;}} while (n < x.length);} /** A trip merge **/private static void Mergepass (int[] x, int[] Y, int n) {int i = 0;while (i < x.length-2 * n + 1) {merge (x , Y, I, i + N, n); i + = 2 * n;} if (i + N < x.length)/** again merges **/merge (x, Y, I, i + N, n); else/** copies the remaining elements of X to Y **/for (int j = i; J < X.length; J + +) Y[J] = x[j];} /** merge **/private static void merge (int[] x, int[] Y, int m, int r, int n) {int i = m, j = r, K = m;/** merges two contiguous subsequence in X into Y **/while (I < R && J < r + N && J < x.length)/** smaller value copied to Y **/if (X[i] < x[j]) y[k++] = X[i++];e lsey[k++] = x[j++];/** Copies the remaining elements of the previous subsequence into Y **/while (i < r) y[k++] = x[i++];/** Copies the remaining elements of the latter subsequence to Y **/while (J < R + N &Amp;& J < x.length) y[k++] = x[j++];}
Java code Implementation of data structure common sorting algorithm