Java implementation of binary sorting

Source: Internet
Author: User

1. What is a binary sort:

Binary sorting is a kind of insertion sorting algorithm, which uses the idea of dichotomy to improve the insertion sort, which can be used to locate the elements of the specified index quickly, which is different from the binary ordering.

Algorithm thought: The dichotomy inserts the sort is when inserts the first element, the front 0~i-1 element carries on the binary, first with their intermediate element ratio, if small, then carries on the binary to the first half, otherwise to the second half carries on the binary, until Left>right, Then move all elements between the first 1 bits of the first element and the target position, and then place the element I in the target position.

2. This is an ordinary insert sort Java implementation:

1      Public Static voidInsertsort (int[] arr) {2          for(inti=0;i<arr.length;i++){3             intj =i;4             intK = J-1;5              while(k>=0){6                 if(arr[k]>Arr[j]) {7                     intTMP =Arr[k];8ARR[K] =Arr[j];9ARR[J] =tmp;Tenj--; Onek--; A}Else{ -                      Break; -                 } the             } -         } -}

3. The two-minute sorting Java code is implemented as follows:

1      Public Static voidBinarysort (int[] arr) {2          for(inti=1;i<arr.length;i++){3             intleft= 0;4             intright= i-1;5             intPivot = (left+right)/2;6              while(left<Right ) {7                  while(left<Right ) {8                     if(arr[pivot]>Arr[i]) {9right = Pivot-1;TenPivot = (left+right)/2; One}Else{ A                          Break; -                     } -                 } the                  while(left<Right ) { -                     if(arr[pivot]<Arr[i]) { -left = pivot+1; -Pivot = (left+right)/2; +}Else{ -                          Break; +                     } A                 } at             } -             if(left>=Right ) { -                 if(arr[pivot]<Arr[i]) { -pivot++; -                 } -                 intj =i; in                 intK = J-1; -                  while(k>=pivot) { to                     intTMP =Arr[j]; +ARR[J] =Arr[k]; -ARR[K] =tmp; thej--; *k--; $                 }Panax Notoginseng             } -         } the          +}

Time complexity for binary sorting: O (n^2), spatial complexity: O (1)

Java implementation of binary sorting

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.