折半查找排序的java实现

来源:互联网 发布:rem px 换算 js 编辑:程序博客网 时间:2024/05/22 08:16
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;elsehigh=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);}}

0 0
原创粉丝点击