Common sorting algorithms have
1. Select sort
2. Insert Sort
3. Bubble sort
4. Quick Sort
5. Merge sort
Here are 5 sort of Java demo, there are many sort, hill sort, count sort, heap sort, cardinal sort etc.
Sort.java
Public classSort { Public Static voidMain (string[] args) {int[] arr = {3, 6, 9, 2, 5, 4}; int[] temp =New int[Arr.length]; MergeSort (arr,0, Arr.length-1, temp);//Insertsort (arr);//Selectsort (arr);//Bubbosort (arr);//QuickSort (arr, 0, arr.length-1); for(intI:arr) {System.out.print (i+ ", "); } } Public Static voidMergeSort (int[] Array,intStartintEndint[] temp) { if(Start <end) { intMiddle = (end + start)/2; MergeSort (array, start, middle, temp); MergeSort (array, Middle+ 1, end, temp); Mergearray (array, start, middle, end, temp); } } Private Static voidMergearray (int[] Array,intStartintMiddle,intEndint[] temp) { intTempindex = 0; ints =start; intm = middle + 1; intE =end; while(S <= Middle && m <=e) { if(Array[s] <=array[m]) Temp[tempindex+ +] = Array[s + +]; ElseTemp[tempindex+ +] = array[m + +]; } while(S <=middle) Temp[tempindex+ +] = Array[s + +]; while(M <=e) Temp[tempindex+ +] = array[m + +]; for(inti = 0; i < Tempindex; i++) Array[start+ i] =Temp[i]; } Public Static voidInsertsort (int[] Array) { for(inti = 0; i < Array.Length; i++){ for(intj = i + 1; J > 0 && j< array.length; j--){ if(Array[j] < array[j-1]){ inttemp =Array[j]; ARRAY[J]= Array[j-1]; Array[j-1] =temp; } } } } Public Static voidSelectsort (int[] Array) { for(inti = 0; i < Array.Length; i + +){ intMaxindex =i; for(intj = i + 1; J < Array.Length; J + +) {Maxindex= Array[maxindex] < Array[j]?J:maxindex; } if(Maxindex! =i) { inttemp =Array[i]; Array[i]=Array[maxindex]; Array[maxindex]=temp; } } } Public Static voidBubbosort (int[] Array) { BooleanIsexchanged =true; for(inti = array.length-1; I > 0 && isexchanged; I--) {isexchanged=false; for(intj = 0; J < I; J + +){ if(J < array.length-1 && Array[j] > array[j + 1]){ inttemp =Array[j]; ARRAY[J]= Array[j + 1]; Array[j+ 1] =temp; Isexchanged=true; } } } } Public Static voidQuickSort (int[] Array,intLintR) { if(L <R) { ints =l; intE =R; intPivotvalue =Array[l]; while(L <R) { while(L < R && Array[r] >pivotvalue) {R--; } if(L <r) Array[l++] =Array[r]; while(L < R && Array[l] <pivotvalue) {L++; } if(L <r) Array[r--] =Array[l]; } // whileARRAY[L] =Pivotvalue; QuickSort (array, S, l-1); QuickSort (Array, L+ 1, E); } }}
A common sorting algorithm