首页 > 其他 > 详细

运用递归进行二分查找

时间:2020-01-05 23:19:21      阅读:82      评论:0      收藏:0      [点我收藏+]

package com.lm.digui;

 

import java.util.Arrays;

 

public class MergonQuery {

public static void main(String[] args) {

int merget[] = {2, 5, 39, 2, 2, 4, 2, 0, 74, 7, 5, 6, 3, 2, 1};

System.out.println(Arrays.toString(megerSort(merget)));

 

}

 

private static int[] megerSort(int[] merget) {

if (merget.length < 2) {

return merget;

}

int mid = merget.length / 2;

int[] left = Arrays.copyOfRange(merget, 0, mid);

int[] after = Arrays.copyOfRange(merget, mid, merget.length);

 

return sertNumber(megerSort(left), megerSort(after));

 

}

 

private static int[] sertNumber(int[] left, int[] after) {

int[] result = new int[left.length + after.length];

for (int index = 0, i = 0, j = 0; index < result.length; index++) {

if (i >= left.length) {

result[index] = after[j++];

} else if (j >= after.length) {

result[index] = left[i++];

} else if (left[i] > after[j]) {

result[index] = after[j++];

} else {

result[index] = left[i++];

}

}

return result;

}

 

 

}

运用递归进行二分查找

原文:https://www.cnblogs.com/limingming1993/p/12154001.html

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