Quick sort (array a from small to large, parameter 1 is the array to be sorted, parameter 2 is the starting subscript, parameter 3 is the subscript):
1 Static voidSortint[] A,intLintR) {2 intm = l+r>>1;3 intI=l, j =R;4 Do{5 while(A[i]<a[m]) i++;6 while(A[j]>a[m]) j--;7 if(i<=j) {8 intt =A[i];9A[i] =A[j];TenA[J] =T; Onei++; j--; A } -} while(i<j); - if(l<j) sort (a,l,j); the if(r>i) sort (a,i,r); -}
Sort
Unique function (like the unique C + +, the return value is the last number of the subscript, parameter 1 is the array to be sorted, parameter 2 is the starting subscript, parameter 3 is the terminating subscript)
1 Static intUniqueint[] A,intLintR) {2 inti =l;3 intj = i+1;4 while(j<=R) {5 while(J<=r&&a[j]<=a[i]) J + +;6 if(i+1<=r&&j<=R) {7A[I+1] =A[j];8i++;9J + +;Ten } One } A - returni; -}
Unique
Lower_bound binary lookup (similar to C + + Lower_bound, parameter 1 is the array to be looked up, parameter 2 is the starting subscript, parameter 3 is the terminating subscript, the parameter 4 is the number to find, return the position of the first >=x)
1 Static intLower_bound (int[] A,intLintRintx) {2r++;l--;3 while(r-l>1 ){4 intm = l+r>>1;5 if(a[m]<x) L =m;6 ElseR =m;7 }8 returnR;9}
Lower_bound
Upper_bound binary lookup (same as above, the return value is the position of the first >x)
1 Static intUpper_bound (int[] A,intLintRintx) {2r++; l--;3 while(r-l>1 ){4 intm = l+r>>1;5 if(a[m]<=x) L =m;6 ElseR =m;7 }8 returnR;9}
Upper_bound
[Template Summary] Some templates for Java