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