赞
踩
public static void InsertSort(int[] arr) { for(int i = 1;i<arr.length;i++) {//从1开始是因为要和前面的数有一个比较的过程 int InsertIndex=i-1;//要插入的理想目的地 int Insertvalue= arr[i]; while (InsertIndex>=0&&Insertvalue<arr[InsertIndex]) { //insertindex后移动一位 arr[InsertIndex+1]=arr[InsertIndex]; InsertIndex--; //一直在减去,所以while结束后代表找到 //后面要加还给他,代表找到的那个位置 } // if(InsertIndex+1!=i) { //+1代表找的那个位置 arr[Insertvalue+1]=Insertvalue; } } for(int i = 0;i<arr.length;i++) { System.out.print(arr[i]+"\t"); } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。