Continuation sorting algorithm
4. Bubble sort
From the first, compared to the next number, if it is larger than the exchange of position, so do not complete the round, you can choose the largest
Public Static<textendsComparable<t>>t[] Genericbubblesort (t[] a) {intn =a.length; for(inti = 0; i < n-1; i++) { for(intj = 0; J < n-i-1; J + +) { if(A[j].compareto (a[j + 1]) > 0) {T temp=A[j]; A[J]= A[j + 1]; A[j+ 1] =temp; } } } returnA; }
Improve the bubble sort above
Scenario One: Set up an iconic variable pos, which records the last swap position in each order. Because the records after the POS position have been swapped in place,
So when the next trip is sorted, just scan to POS location
Public Static<textendsComparable<t>>t[] Genericbubblesortgai (t[] a) {intn =a.length; inti = n-1; while(I > 0) { intpos = 0; for(intj = 0; J < I; J + +) { if(A[j].compareto (a[j + 1]) > 0) {pos=J; T Temp=A[j]; A[J]= A[j + 1]; A[j+ 1] =temp; }} I=POS; } returnA; }
Improvement Programme II:
Both sides of the same time first find the largest and then find the smallest
Public Static<textendsComparable<t>>t[] GENERICBUBBLESORTGAI2 (t[] a) {intn =a.length; intLow = 0, high = n-1; intJ; T tmp; while(Low <High ) { for(j = Low, J < High; J + +) { if(A[j].compareto (a[j + 1]) > 0) {tmp=A[j]; A[J]= A[j + 1]; A[j+ 1] =tmp; }} High--; for(j = High, J > low; j--) { if(A[j].compareto (a[j-1]) < 0) {tmp=A[j]; A[J]= A[j-1]; A[j-1] =tmp; }} Low++; } returnA; }
Improvement Programme III:
Set a flag if there is a trip that does not occur the order is completed
Public Static<textendsComparable<t>>t[] Genericbubblesortgai3 (t[] a) {intn =a.length; BooleanFlag =true; for(inti = 0; i < n-1; i++) { if(!flag) { returnA; } Flag=false; for(intj = 0; J < n-i-1; J + +) { if(A[j].compareto (a[j + 1]) > 0) {flag=true; T Temp=A[j]; A[J]= A[j + 1]; A[j+ 1] =temp; } } } returnA; }
5. Quick Sort
< quick sort > The basic idea is to divide the sorted data into separate two parts by a single trip, and that part of all the data is smaller than all the data in the other part,
* And then the two parts of the data are quickly sorted by this method, the whole sorting process can be recursive, in order to achieve the entire data into an ordered sequence. Fast sorting is an unstable sorting algorithm
Public Static<textendsComparable<t>>t[] Quicksortstart (t[] list) {QuickSort (list,0, List.length-1); returnlist; } Private Static<textendsComparable<t>>voidquickSort (t[] list,intFirstintLast ) { if(Last >First ) { intPovitindex =partition (list, first, last); QuickSort (list, first, Povitindex-1); QuickSort (list, Povitindex+ 1, last); } } Private Static<textendsComparable<t>>intPartition (t[] list,intFirst ,intLast ) { /** Divide the array into two groups, placing a number smaller than povit in front of it, and a number larger than the povit behind it*/T Povit=List[first]; intLow = first + 1; intHigh =Last ; while(High >Low ) { while(High > Low && list[low].compareto (Povit) <= 0) Low++; while(Low <= && List[high].compareto (povit) > 0) High--; if(High >Low ) {T temp=List[high]; List[high]=List[low]; List[low]=temp; } } while(High > First && list[high].compareto (povit) >= 0) High--; if(Povit.compareto (List[high]) > 0) {List[first]=List[high]; List[high]=Povit; returnHigh ; } returnFirst ; }
6. Merge sort
To split a sequence in half, know that it cannot be split, and then start merging, merge with insert sort
Public Static<textendsComparable<t>>t[] MergeSort (t[] a) {t[] temp=A.clone (); A= Msort (A, temp, 0, a.length); returnA; } Public Static<textendsComparable<t>>t[] Msort (t[] A, t[] temp,intFirstintLast ) { if(First + 1 <Last ) { intMid = (first + last)/2; Msort (A, temp, first, mid); Msort (A, temp, mid, last); intIndex1 =First ; intIndex2 =mid; intIndex3 =First ; while(Index1 < mid && Index2 <Last ) { if(A[index1].compareto (A[index2]) < 0) {Temp[index3]=A[INDEX1]; Index1++; } Else{TEMP[INDEX3]=A[index2]; Index2++; } index3++; } while(Index1 <mid) {temp[index3+ +] = a[index1++]; } while(Index2 <Last ) {Temp[index3+ +] = a[index2++]; } for(inti = First; i < last; i++) A[i]=Temp[i]; } returnA; }
The feeling is to copy the code up, said is not very clear,, there is a heap sort did not write
Finally, the above sorting algorithm was tested, generating 10,000 int, sorting, counting time
1. Merge sort between 50~60 ms
2. Simple selection sort between 135~155 ms
3. Select a sort of MS//Maybe my data is not selected, 10,000 numbers, the generated random number is also 0~10000
4. Quickly sort around 30ms
5. Bubble Sort Improvement Solution two quick points 280ms
6. Insert Sort 70ms around
Then there are 10,000 random numbers, and the range is changed to 0~100000, which results in a shorter time for each sort.
So the length of the order is related to the data itself.
Sorting algorithm Java implementation 2