首页 > 编程语言 > 详细

二叉插入排序

时间:2019-12-27 20:01:59      阅读:107      评论:0      收藏:0      [点我收藏+]

记录

//TODO

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));

        }
    }
}

二叉插入排序

原文:https://www.cnblogs.com/yinniora/p/12109310.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!