折半查找排序的java实现

本文介绍了一种插入排序算法的具体实现过程。通过一个公共类InsertSort和其成员方法insertSort,实现了对整型数组的排序。该算法采用折半查找确定待插入元素的位置,并通过移动已排序元素来完成整个排序过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


public class InsertSort {

		public void insertSort(int array[])
		{
			int length=array.length;
			int low=0,high=0,mid=0;
			int sortArray[]=new int[length];
			sortArray[0]=array[0];
			for(int i=1;i<length;i++)//将要插入的数
			{
				low=0;high=i-1;
				while(low<=high)//折半查找所要插入的位置low为最终要插入的位置
				{
					mid=(low+high)/2;
					if(sortArray[mid]>array[i])
						low=mid+1;
					else
						high=mid-1;
				}
				for(int j=i-1;j>=low;j--)//整体向后移动
					sortArray[j+1]=sortArray[j];
				sortArray[low]=array[i];//插入数据
			}
			for(int i:sortArray)
				System.out.print(i+" ");
			
		}
		public static void main(String[] args) {
			int array[]={1,2,3,4,5,6,7,8,9,10};
			InsertSort a= new InsertSort();
			a.insertSort(array);
		}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值