[Template Summary] Some templates for Java

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.