Binary insertion sort

recording

//EVERYTHING

import java.util.Arrays;

public class BinaryInsertSort {
    public static void main(String[] args) {
        int[] arr = { 5, 7, 8, 4, 2, 7, 1, 8, 12, 25, 10 };
        binaryInsertSort(arr, arr.length);
    }

    private static void binaryInsertSort(int[] arr, int n) {
        int low, high, mid, index,i;

        for (index = 0; index < n; index++) {
            low = 0;
            high = index-1;
            int temp = arr[index];
            while(high>=low){
                mid = (high+low)/2;
                if(arr[mid]>temp){
                    high = mid-1;
                }else{
                    low = mid+1;
                }
            }
            for(i=index-1; i>high; i--){
                arr[i+1] = arr[i];
            }
            arr[high+1]=temp;
            System.out.println("第"+(index+1)+"趟排序之后,元素序列为"+Arrays.toString(arr));

        }
    }
}

Guess you like

Origin www.cnblogs.com/yinniora/p/12109310.html