The basic sort that needs to be mastered
One, insert sort 1) Direct insert sort 2) Hill sort
Two, Exchange sort 1) bubble sort 2) Quick sort
Iii. Select sort 1) simple select sort 2) heap sort
Iv. Merging and sorting
First, insert sort
The simple understanding is to traverse the entire array, and in the process each number is compared to the previous one as long as it is smaller than it is moved forward.
1) Direct Insert Sort implementation
Public Static voidDirectinsert (int[] data) { intTemp=0; intlen=data.length; for(inti=1;i<len;i++) {Temp=Data[i]; intJ=i-1; while(J>-1 && temp<Data[j]) {Data[j+1]=Data[j]; J--; } data[j+1]=temp; } }
2) Hill Sort
Understanding: http://blog.csdn.net/morewindows/article/details/6668714
The direct insertion of the array is sorted by the interval of N/2,N/4,N/8,,,, 1. When the array is basically ordered, the number of insertion sort comparisons is minimal, the interval is 1, the time is basically orderly, the last execution of the insertion sort works best.
Public Static voidShellinsert (int[] data) { intTemp=0; intlen=data.length; for(intgap=len/2;gap>0;gap/=2)//the outermost narrowing increment { for(intI=0;I<LEN;I+=GAP)//interval sort such as 13579 246810 such that the equal interval grouping is inserted directly into the sort{Temp=Data[i]; intj=i-Gap; while(J>-1 && temp<Data[j]) {Data[j+gap]=Data[j]; J-=Gap; } data[j+gap]=temp; } } }
Second, exchange sort
1) Bubble sort
The number of adjacent two numbers is compared to the size until the interchange is complete.
The first trip to the largest number to the right, the second to the second largest change to the second-to-last, each time the number of the first n-i to sort.
Public Static voidBubbleint[] data) { intlen=data.length; intcnt=0;//The number of exchanges. If it's 0, the sort is done jumping out intTemp=0; for(inti=1;i<len;i++)//to sort n-1 { for(intj=0;j<len-i;j++) { if(data[j]>data[j+1]) {temp=Data[j]; DATA[J]=data[j+1]; Data[j+1]=temp; CNT++; } } if(cnt==0) Break; CNT=0; } }
2) Quick Sort
Basic sort Java Implementation