Java 实现二分(折半)插入排序

Java 实现二分(折半)插入排序

大家好,又见面了,我是全栈君,今天给大家准备了Idea注册码。

全栈程序员社区此处内容已经被作者隐藏,请输入验证码查看内容
验证码:
请关注本站微信公众号,回复“验证码”,获取验证码。在微信里搜索“全栈程序员社区”或者“www_javaforall_cn”或者微信扫描右侧二维码都可以关注本站微信公众号。

设有一个序列a[0],a[1]…a[n];当中a[i-1]前是已经有序的,当插入时a[i]时,利用二分法搜索a[i]插入的位置

效率:O(N^2),对于初始基本有序的序列,效率上不如直接插入排序;对于随机无序的序列,效率比直接插入排序要高

/* * 二分(折半)插入排序 * 设有一个序列a[0],a[1]...a[n];当中a[i-1]前是已经有序的,当插入时a[i]时,利用二分法搜索a[i]插入的位置 */public class BinaryInsertSort {	public static void main(String[] args) {		int len = 10;		int[] ary = new int[len];		Random random = new Random();		for (int j = 0; j < len; j++) {			ary[j] = random.nextInt(1000);		}		binaryInsert(ary);		/*		 * 复杂度分析: 最佳情况,即都已经排好序,则无需右移,此时时间复杂度为:O(n lg n) 最差情况,所有逆序,此时复杂度为O(n^2)		 *  无法将最差情况的复杂度提升到O(n|logn)。		 */		// 打印数组		printArray(ary);	}	/**	 * 插入排序	 * @param ary	 */	private static void binaryInsert(int[] ary) {		int setValueCount = 0;		// 从数组第二个元素開始排序,由于第一个元素本身肯定是已经排好序的		for (int j = 1; j < ary.length; j++) {// 复杂度 n			// 保存当前值			int key = ary[j];			// ∆ 利用二分查找定位插入位置//			int index = binarySearchAsc(ary, ary[j], 0, j - 1);// 复杂度:O(logn)//			int index = binarySearchDesc(ary, ary[j], 0, j - 1);// 复杂度:O(logn)			int index = binarySearchDesc2(ary, ary[j], 0, j - 1);// 复杂度:O(logn)			printArray(ary);			System.out.println("第" + j +"个索引上的元素要插入的位置是:" + index);			// 将目标插入位置,同一时候右移目标位置右边的元素			for (int i = j; i > index; i--) {// 复杂度,最差情况:(n-1)+(n-2)+...+n/2=O(n^2)				ary[i] = ary[i - 1]; //i-1 <==> index				setValueCount++;			}			ary[index] = key;			setValueCount++;		}		System.out.println("\n 设值次数(setValueCount)=====> " + setValueCount);	}	/**	 * 二分查找 升序 递归	 * 	 * @param ary	 *            给定已排序的待查数组	 * @param target	 *            查找目标	 * @param from	 *            当前查找的范围起点	 * @param to	 *            当前查找的返回终点	 * @return 返回目标在数组中,按顺序应在的位置	 */	private static int binarySearchAsc(int[] ary, int target, int from, int to) {		int range = to - from;		// 假设范围大于0,即存在两个以上的元素,则继续拆分		if (range > 0) {			// 选定中间位			int mid = (to + from) / 2;			// 假设临界位不满足,则继续二分查找			if (ary[mid] > target) {				/*				 * mid > target, 升序规则,target较小,应交换位置 前置, 即target定位在mid位置上,				 * 依据 查找思想, 从from到 mid-1觉得有序, 所以to=mid-1				 */				return binarySearchAsc(ary, target, from, mid - 1);			} else {				/*				 * mid < target, 升序规则,target较大,不交换位置,查找比較的起始位置应为mid+1				 */				return binarySearchAsc(ary, target, mid + 1, to);			}		} else {			if (ary[from] > target) {//如 5,4, 要插入的是4				return from;			} else {				return from + 1;			}		}	}	/**	 * 二分查找 降序, 递归	 */	private static int binarySearchDesc(int[] ary, int target, int from, int to) {		int range = to - from;		if (range > 0) {			int mid = (from + to) >>> 1;			if (ary[mid] > target) {				return binarySearchDesc(ary, target, mid + 1, to);			} else {				return binarySearchDesc(ary, target, from, mid - 1);			}		} else {			if (ary[from] > target) {//如 5,4, 要插入的是4				return from + 1;			} else {				return from;			}		}	}		/**	 * 二分查找 降序, 非递归	 */	private static int binarySearchDesc2(int[] ary, int target, int from, int to) {//		while(from < to) {		for (; from < to; ) {			int mid = (from + to) >>> 1;			if (ary[mid] > target) {				from = mid + 1;			} else {				to  = mid -1;			}		}		//from <==> to;		if (ary[from] > target) {//如 5,4, 要插入的是4			return from + 1;		} else {			return from;		}	}	private static void printArray(int[] ary) {		for (int i : ary) {			System.out.print(i + " ");		}	}}

打印

918 562 442 531 210 216 931 706 333 132 第1个索引上的元素要插入的位置是:1918 562 442 531 210 216 931 706 333 132 第2个索引上的元素要插入的位置是:2918 562 442 531 210 216 931 706 333 132 第3个索引上的元素要插入的位置是:2918 562 531 442 210 216 931 706 333 132 第4个索引上的元素要插入的位置是:4918 562 531 442 210 216 931 706 333 132 第5个索引上的元素要插入的位置是:4918 562 531 442 216 210 931 706 333 132 第6个索引上的元素要插入的位置是:0931 918 562 531 442 216 210 706 333 132 第7个索引上的元素要插入的位置是:2931 918 706 562 531 442 216 210 333 132 第8个索引上的元素要插入的位置是:6931 918 706 562 531 442 333 216 210 132 第9个索引上的元素要插入的位置是:9 设值次数(setValueCount)=====> 24931 918 706 562 531 442 333 216 210 132 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/117937.html原文链接:https://javaforall.net

(0)
上一篇 2021年12月16日 下午1:00
下一篇 2021年12月16日 下午1:00


相关推荐

  • 使用【百度云推送】第三方SDK实现推送功能具体解释

    使用【百度云推送】第三方SDK实现推送功能具体解释

    2021年12月6日
    66
  • icmp协议是tcp还是udp_icmp协议使用udp

    icmp协议是tcp还是udp_icmp协议使用udp2018年4月11日11:41:29更新工具发包触发点结局traceroute初始发udp包ttl递增,icmp每一跳报ttl超时udp端口不可达tracert初始发icmprequest包触发点:ttl递增,icmp超时icmpechoreply注:触发点都是根据ttl超时来检测参考项traceroutetracert…

    2025年11月29日
    11
  • 计算机图形学中的空间坐标系

    计算机图形学中的空间坐标系

    2021年6月9日
    98
  • duststorm和sandstorm_Stormwind

    duststorm和sandstorm_Stormwindvirustracker·2016/03/0310:17www.cylance.com/hubfs/2015_…CylanceSPEAR发现了一起针对日本、韩国、美国、欧洲以及其他几个东南亚国家的威胁行动,在上述国家中,有大量的行业部门都遭到了攻击。0x00多样的权利形式我们研究发现DustStorm最早从2010年开始活动,使用了大量不同的作战技术,包括钓鱼、水坑攻击和0-day漏洞。…

    2022年10月14日
    5
  • python-sort函数[通俗易懂]

    python-sort函数[通俗易懂]sort调用方式:sorted(str)list.sort()sorted()函数函数原型sorted(iterable,cmp=None,key=None,reverse=False

    2022年7月6日
    27
  • LSTM神经网络详解

    LSTM神经网络详解LSTM 长短时记忆网络 LongShortTer LSTM 是一种改进之后的循环神经网络 可以解决 RNN 无法处理长距离的依赖的问题 目前比较流行 长短时记忆网络的思路 原始 RNN 的隐藏层只有一个状态 即 h 它对于短期的输入非常敏感 再增加一个状态 即 c 让它来保存长期的状态 称为单元状态 cellstate 按照上面时间维度展开 在 t 时刻 LSTM 的输入有三个 当前时刻网络的输入值 X t 上一时刻 LSTM 的输出值 h t 1 以及上

    2026年3月19日
    2

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号