Common Sort algorithm Introduction bubble sort
Public class bubblesort { Public Static void Sort(int[] array) {intTValue; for(inti =0; i < Array.Length; i++) { for(intj = i; J < Array.Length; J + +) {if(Array[i] > Array[j]) {tValue = Array[i]; Array[i] = Array[j]; ARRAY[J] = TValue; } } } } Public Static void Main(string[] args) {intA[] = {6,2,4,5,1,9,Ten,7}; Sort (a); for(intV:A) {System.out.print (v +" "); } }}
Input Result:
1 2 4 5 6 7 9 10
Insert Sort:
Public class insertsort { Public Static void Sort(int[] array) {intTValue;intJ for(inti =0; i < Array.Length; i++) {j = i; TValue = Array[i]; while(J >0&& TValue < Array[j-1]) {Array[j] = array[j-1]; j--; } Array[j] = TValue; } } Public Static void Main(string[] args) {intA[] = {6,2,4,5,1,9,Ten,7}; Sort (a); for(intV:A) {System.out.print (v +" "); } }}
Input Result:
1 2 4 5 6 7 9 10
Select Sort:
Public class selectsort { Public Static void Sort(int[] array) {intindex, TValue; for(inti =0; i < Array.Length; i++) {index = i; for(intj = i +1; J < Array.Length; J + +) {if(Array[j] < Array[index]) {index = j; } }if(Index! = i) {tValue = Array[i]; Array[i] = Array[index]; Array[index] = TValue; } } } Public Static void Main(string[] args) {intA[] = {6,2,4,5,1,9,Ten,7}; Sort (a); for(intV:A) {System.out.print (v +" "); } }}
Input Result:
1 2 4 5 6 7 9 10
Quick sort:
Public class QuickSort { Public Static void QuickSort(int[] Array,intLeftintright) {intI, J, BValue, TValue;if(Left > right) {return; } i = left; j = right; BValue = Array[left]; while(I! = j) { while(Array[j] >= bValue && i < J) {j--; } while(Array[i] <= bValue && i < J) {i++; }if(I < J) {tValue = Array[i]; Array[i] = Array[j]; ARRAY[J] = TValue; }} Array[left] = Array[i]; Array[i] = BValue; QuickSort (array, left, I-1); QuickSort (array, i +1, right); } Public Static void Main(string[] args) {intA[] = {6,2,4,5,1,9,Ten,7}; QuickSort (A,0, A.length-1); for(intV:A) {System.out.print (v +" "); } }}
Input Result:
1 2 4 5 6 7 9 10
Resources:
http://blog.jobbole.com/11745/
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Common sort algorithm (Java implementation)