public class sort { /** * @param args */ public static void main (String[] args) { int[] data = { 8,4,9,2,1,6,3,7,5}; for (int i = 0; i < data.length; i++ ) { system.out.print (data[i]+ " "); } system.out.println (); sort s = new sort (); //s.sort4 (data,0,data.length-1); S.sort5 (data,0,data.length-1); for (int i = 0; i < data.length; i++) { system.out.print (data[i]+ " "); } } /* * Bubble Sort: compares the adjacent two elements in turn, placing the largest (or smallest) element of the unordered sequence at the end of the unordered sequence by a single comparison */ public void sort1 (Int[] data) { for (int i = 0; i < data.length -1; i++) { for (int j = 0; j < data.length - i - 1; j++) { if (Data[j] &NBSP;>&NBSP;DATA[J&NBSP;+&NBSP;1]) { //The big back row int temp = data[j ]; data[j] = data[j + 1]; data[j + 1 ] = temp; } } } } /* * Select Sort: Each time a minimum (or maximum) element is selected from the data element to be sorted, the order is placed at the end of the ordered sequence until all the data elements to be sorted are exhausted. */ public void sort2 (int data[]) { int minVal; int minindex; for (int i = 0; i < data.length - 1; i++) { minVal = data[i]; minIndex = i; //Select the smallest element and the smallest subscript for (int j = i + 1; j < data.length; j++) { if (Data[j] < minval) { minval = data[j]; minIndex = j; } } //found the smallest element if (Minval != datA[i] && minindex != i) { data[minIndex] = data[i]; data[i] = minVal; } } } /* * Insert sort: divides the sequence into ordered and unordered two parts, and each processing is to compare the first element of an unordered sequence with the elements of an ordered sequence of numbers, to find the insertion position, Insert the element into the appropriate position in the ordered series. */ public void sort3 (int data[]) { for (int i = 1; i < data.length; i++) { for (int j = i; j > 0; j--) { if (data[j] < data[j - 1]) { int temp = data[j]; data[j] = data[j - 1]; data[j - 1] = temp; } } } } /* * Merge Sort: merges two (or more than two) ordered tables into a new ordered table, That is, the ordered sequence is divided into several sub-sequences, each of which is ordered. Then the ordered subsequence is combined into a whole ordered sequence. The sorting process is as follows: (1) application space, which is the sum of two sorted sequences, which is used to hold the combined sequence (2) to set two pointers, the initial position is the starting position of two sorted sequences, respectively (3) Compare the elements pointed to by two pointers, select a relatively small element into the merge space, and move the pointer to the next position (4) Repeat step 3 until a pointer reaches the end of the sequence (5) Copies all remaining elements of the other sequence directly to the end of the merge sequence &NBSP;&NBSP;*/&NBSP;&NBSP;PUBLIC&NBSP;VOID&NBSP;SORT4 (Int data[], int start, int end) { if (Start < end) { int mid = (start + end) / 2; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SORT4 (Data, start, mid); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SORT4 (Data, mid + 1, end); merge (data, Start, mid, end); } } public static void merge (int data[], int start, int mid, int end) { int temp[] = new int[end - start + 1]; int i = start; int j = mid + 1; int k = 0; while (i <= mid & & j <=&nbSp;end) { if (Data[i] < data[j]) { temp[k++] = data[i++]; } else { temp[k++] = data[j++]; } } while (I <= mid) { temp[k++] = data[i++]; } while (j <= end) { temp[k++] = data[j++]; } for (K = 0, i = start; k < temp.length; k++, i++) { data[i] = temp[k]; } } /* * Quick Sort: divides the sorted data into separate two parts by a single pass, and all of the data in one part is smaller than any other part of the data, Then the two parts of the data are quickly sorted by this method, and the whole sorting process can be recursive toThis reaches the entire data into an ordered sequence. &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;*/&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;PUBLIC&NBSP;&NBSP;VOID&NBSP;SORT5 (int data[], int start, int end) { if (end - start <= 0) { return; } int last = start; for (int i = start + 1; i <= end; i++) { if (Data[i] < data[start]) { int temp = data[++last]; data[last] = data[i]; data[i] = temp; } } int temp = data[last]; data[last] = data[start]; data[start] = Temp; sort5 (data, start, last - 1); sort5 (data, last + 1, end); } }
Implementation of various sorting algorithms Java