There are many kinds of sorting algorithms, mainly divided into insert sort (direct insert sort, dichotomy insert sort), exchange sort (bubble sort, quick sort), select sort (Simple select sort, heap sort), and so on, do not say principle, only paste code. The principle can be seen Min data structure or the principle of the Internet is very detailed. Recommend a link Click to open a link
1. Direct Insert Sort
Package Sortpackage;import Java.util.arrays;public class Insertsort {public void sort (Int. a[]) {for (int i=1;i< a.length;i++) { if (A[i]<a[i-1]) { int j,temp; Temp=a[i]; for (j=i;j>0&&temp<a[j-1];j--) { a[j]=a[j-1]; } a[j]=temp;}}} public static void Main (String args[]) {insertsort object=new insertsort (); int A[]={3,2,6,5,8,9,0,2,1,23,12,45,32};o Bject.sort (a); System.out.println (Arrays.tostring (a));}}
2. Two-way direct insertion sort (find location by means of two-point method)
Package Sortpackage;import Java.util.arrays;public class Insertsortbybinarysearch {public void binarysearchsort (int a[ ]) {int low, high, middle, temp;if (a! = null) {for (int i = 1; i < a.length; i++) {temp = A[i];low = 0;high = I-1;wh Ile (Low >= 0 && Low <= high) {middle = (low + high)/2;if (A[middle] > A[i]) {high = middle-1;} else< C0/>{low = middle + 1;}} for (int j = i-1, J >= Low; j--) {a[j + 1] = a[j];} A[low] = temp;}}} public static void Main (String args[]) {Insertsortbybinarysearch object = new Insertsortbybinarysearch (); int a[] = {3, 1, 6, 2, 8, 9, 6, 5, 4,};object.binarysearchsort (a); System.out.println (Arrays.tostring (a));}}
3. Hill sort
Package Sortpackage;import Java.util.arrays;public class Insertsortbybinarysearch {public void binarysearchsort (int a[ ]) {int low, high, middle, temp;if (a! = null) {for (int i = 1; i < a.length; i++) {temp = A[i];low = 0;high = I-1;wh Ile (Low >= 0 && Low <= high) {middle = (low + high)/2;if (A[middle] > A[i]) {high = middle-1;} else< C0/>{low = middle + 1;}} for (int j = i-1, J >= Low; j--) {a[j + 1] = a[j];} A[low] = temp;}}} public static void Main (String args[]) {Insertsortbybinarysearch object = new Insertsortbybinarysearch (); int a[] = {3, 1, 6, 2, 8, 9, 6, 5, 4,};object.binarysearchsort (a); System.out.println (Arrays.tostring (a));}}
4. Bubble sort
Package Sortpackage;import Java.util.arrays;public class Bubblesort {public void sort (Int. a[]) {for (int i=0;i< a.length-1;i++) {boolean flag=true;for (int j=0;j<a.length-1-i;j++) {if (a[j]>a[j+1]) { int temp=a[j];a[j]=a [J+1];a[j+1]=temp;flag=false;}} if (flag) {return;}}} public static void Main (string[] args) {int[] a={3,7,1,4,8,4,3,9,0,12,2};//int[] B={1,2,3,4,8};bubblesort object=new Bubblesort (); Object.sort (a); System.out.println (Arrays.tostring (a));}}
5. Fast sorting algorithm
Package Sortpackage;import Java.util.arrays;public class Quicksort {public void sort (int l, int r, int a[]) {//Quick sort first method, one All the pits. if (L < r) {int i = l, j = r, temp = A[i];while (i < J) {while (A[j] >= temp && i < j) {j--;} if (I < j) {a[i++] = a[j];} while (A[i] < temp && I < j) {i++;} if (I < j) {a[j--] = A[i];}} A[i] = Temp;sort (i + 1, R, a); Recursive call sort (L, i-1, a);}} public void Sort1 (int l,int r,int a[]) {//fast-track the second method, from both sides found less than and greater than the initial value of the position, the two elements exchanged. if (l<0| | r>a.length-1| | a==null| | L>r) {return; } int i=l,j=r; while (I<J) {while (i<j&&a[j]>=a[l]) j--; while (I<j&&a[i]<=a[l]) i++; if (i<j) {int t=a[i]; A[I]=A[J]; a[j]=t; }} int t=a[l]; A[l]=a[i]; a[i]=t; Sort1 (L,i-1,a); Sort1 (I+1,r,a); }public static void Main (string[] args) {int[] a = {3, 7, 1, 4, 8, 4, 3, 9, 0,, 2};quicksort object = new Quicksort () ; Object.sort1 (0, A.length-1, a); System.out.println ("Fast sorting Algorithm"); System.out.println (Arrays.tostring (a));}}6. Simple selection of sorting (algorithm as its name, the principle is relatively simple)
Package Sortpackage;import Java.util.arrays;public class Simpleselect {public void sort (Int. a[]) {for (int i=0;i<a.len gth-1;i++) {int min=i; for (int j=i+1;j<a.length;j++) {if (a[min]>a[j]) min=j; } if (min!=i) {int temp=a[i]; a[i ]=a[min]; A[min]=temp; }}} public static void Main (string[] args) {int[] a = {3, 7, 1, 4, 8, 4, 3, 9, 0,, 2};simpleselect object = New Simpleselect (); Object.sort (a); System.out.println (Arrays.tostring (a));}}
Common sorting algorithms