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);
}
}
折半查找排序的java实现
最新推荐文章于 2023-02-13 10:12:58 发布