1. Bubble sort
PackageSort;Importjava.util.Arrays; Public classBubblesort { Public Static voidMain (string[] args) {int[] A = {1,100,234,44,3,2,4,5}; Bubblesort (A,a.length+1); System.out.println (Arrays.tostring (a)); } Public Static int[] Bubblesort (int[] A,intN) { for(inti=0;i<a.length;i++){ for(intj=i+1;j<a.length;j++){ if(A[i] >A[j]) { intTMP =A[i]; A[i]=A[j]; A[J]=tmp; } } } returnA; }}2. Quick Sort
PackageSort;Importjava.util.Arrays; Public classQuickSort { Public Static voidMain (string[] args) {intA[] = {1,6,9, 2, 3, 1, 5, 4 }; QuickSort (A,0, 7); System.out.println (arrays.tostring (A)); } Public Static voidQuickSort (int[] A,intLeftintRight ) { if(Left <Right ) { //One Division intMID =partion (A, left, right); QuickSort (A,0, Mid-1); QuickSort (A, Mid+ 1, right); } } Public Static voidSwapint[] A,intLintr) {intTMP =A[l]; A[L]=A[r]; A[R]=tmp; } Public Static intPartion (int[] A,intLeftintRight ) { //axis value, which selects the first digit of the array by default while(Left <Right ) { while(Left < right && A[left] <=A[right]) { Right--; } if(left<Right ) {Swap (A, left, right); } while(Left < right && A[left] <=A[right]) { Left++; } if(left<Right ) {Swap (A, left, right); } } returnLeft ; }}3. Insert Sort
Package Sort;public class Insertionsort {public int[] Insertionsort (int[] A, int n) { int I, J, temp; for (i = 1; i < n; i++) { temp = A[i], for (j = i; j > 0 && a[j-1] > temp; j-- ) {A[j] = A[j-1];} A[J] = temp;} return A;}}
3.1 Hill sort (insert sort of one)
PackageSort;Importjava.util.Arrays; Public classShellsort { Public Static voidMain (string[] args) {int[] A = {54, 35, 48, 36, 27, 12, 44, 44, 8, 14, 26, 17, 28 }; Sort (a); System.out.println (Arrays.tostring (a)); } Public Static voidSortint[] a) {//set the step size by default to half the length of the array intStep = A.LENGTH/2; while(Step >= 1) { for(inti = step; i < a.length; i + =Step) { intTMP =A[i]; intJ; for(j = i; j > 0 && a[j-step] > tmp; J-=Step) {A[j]= A[j-step];//element Move back} A[j]= tmp;//inserted position, note that at this point J has been in the for loop once--} step/= 2; } }}
4. Select sort
PackageSort; Public classSelectionsort { Public int[] Selectionsort (int[] A,intN) {//Write code here for(inti = 0; i < n-1; i++) { intindex =i; intJ; //find the minimum worthy element subscript for(j = i + 1; j < N; j + +) { if(A[j] <A[index]) {Index=J; } } intTMP =A[index]; A[index]=A[i]; A[i]=tmp; } returnA; }}
5. Merge sort
PackageSort; Public classMergeSort { Public Static voidMain (string[] args) {int[] A = {1, 4, 3, 2, 5 }; MergeSort (A,5); for(inti = 0; i < a.length; i++) {System.out.println (a[i]); } } Public Static int[] MergeSort (int[] A,intN) {//Write code hereSort (A, 0, n-1); returnA; } Public Static voidSortint[] A,intLeftintRight ) { if(Left <Right ) { intMid = (left + right)/2; Sort (A, left, mid); Sort (A, mid+ 1, right); Merge (A, left, Mid, right); } } Public Static voidMergeint[] A,intLeftintMidintRight ) { //Temporary Array intn = right-left + 1; int[] Tmparr =New int[n]; intL =Left ; intr = Mid + 1; intt = 0;//Temporary array subscript element//Compare the size of two sub-sequence elements while(L <= mid && R <=Right ) { if(A[l] <A[r]) {tmparr[t+ +] = a[l++]; } Else{tmparr[t+ +] = a[r++]; } } //the remaining join temporary array while(L <=mid) {tmparr[t+ +] = a[l++]; } //the remaining join temporary array while(R <=Right ) {tmparr[t+ +] = a[r++]; } //Put temporary array elements back into the original array for(inti = 0; I < T; i++) {A[left+ i] =Tmparr[i]; } }}
6. Heap Sequencing
PackageSort;Importjava.util.Arrays; Public classHeapsort { Public Static voidMain (string[] args) {int[] A = {2, 5, 9, 6, 1, 4, 8, 7, 12, 50 }; Sort (a); System.out.println (Arrays.tostring (a)); } Public Static voidSortint[] a) {intLen =a.length; for(inti = 0; i < len-1; i++) { //Build a heapBuildheap (A, len-1-i); //swap heap top and last elementSwap (A, 0, len-1-i); } } Private Static voidSwapint[] A,intIintj) {//TODO auto-generated Method Stub intTMP =A[i]; A[i]=A[j]; A[J]=tmp; } Public Static voidBuildheap (int[] A,intLastIndex) { //start from the parent node of the last node for(inti = (lastIndex-1)/2; I >= 0; i--) { //The current node has child nodes while(I * 2 + 1 <=LastIndex) { //left node subscript value intL = i * 2 + 1; //Right node subscript value intR = i * 2 + 2; //The default left node is the maximum value intBiggerindex =l; //There is a right node. if(L <LastIndex) { //the value of the right node is larger than the left node if(A[r] >A[l]) {Biggerindex=R; } } //the value of the current node is smaller than the minimum value of the child node, swapping if(A[i] <A[biggerindex]) {Swap (A, I, biggerindex); //assigns the maximum subscript to the current node and enters the next while loop to judgei =Biggerindex; } Else { Break; } } } }}
7. Sort Count 8. Base sort 9. Bucket sort
Java implementation of nine sorting algorithms