排序算法———折半插入排序

来源:互联网 发布:淘宝消保保证金30元 编辑:程序博客网 时间:2024/05/19 19:31
package insert;public class BInsertSort {public static void main(String[] args) {int[] a = new int[]{9,7,2,6,8};insert(a);}public static void insert(int[] array) {int low,high,m,temp;int i,j;for (i = 1; i < array.length; i++) {temp = array[i];low = 0; high = i-1;while(low <= high){m = (low+high)/2;if(temp < array[m]){high = m-1;}else{low = m+1;}}for (j = i; j > low; j--) {array[j] = array[j-1];}array[low] = temp;out(array,i);}}public static void out(int[] array,int l) {System.out.print("第"+l+"次排序:");for (int i = 0; i < array.length; i++) {System.out.print(array[i]+" ");}System.out.println();}}

插入排序算法,通过插入查找算法,找到将要插入的位置,并将依次向后移动,然后将待插入的值插入到队列中。

第1次排序:7 9 2 6 8 第2次排序:2 7 9 6 8 第3次排序:2 6 7 9 8 第4次排序:2 6 7 8 9 
这个是运行结果

0 0
原创粉丝点击