插入法排序
前面看到流星雨发的排序算法 我把插入法单独扣出来怎么排出来不对呢 那个方法也没调用其他函数啊帮偶看看错在什么地方了 谢谢
public class Sort4 {
public static void main(String []args) {
int vec[] = new int[] { 37, 47, 23, -5, 19, 56 };
long begin = System.currentTimeMillis();
for (int k = 0; k < 1000000; k++) {
for (int i = 1; i < vec.length; i++) {
int j = i;
while (vec[j - 1] < vec[i]) {
vec[j] = vec[j - 1];
j--;
if (j <= 0) {
break;
}
}
vec[j] = vec[i];
}
}
long end = System.currentTimeMillis();
System.out.println("插入法用时为:" + (end - begin));
for (int i = 0; i < vec.length; i++) {
System.out.println(vec[i]);
}
}
}
----------------解决方案--------------------------------------------------------
public class Sort4 {
public static void main(String []args) {
int vec[] = new int[] { 37, 47, 23, -5, 19, 56 };
long begin = System.currentTimeMillis();
for (int k = 0; k < 1000000; k++) {
for (int i = 1; i < vec.length; i++) {
int j = i;
while (vec[j - 1] < vec[i]) {
int temp = vec[j]; //加入
vec[j] = vec[j - 1];
vec[j-1] = temp; //加入
j--;
if (j <= 0) {
break;
}
}
//vec[j] = vec[i]; 取消这句
}
}
long end = System.currentTimeMillis();
System.out.println("插入法用时为:" + (end - begin));
for (int i = 0; i < vec.length; i++) {
System.out.println(vec[i]);
}
}
}
[ 本帖最后由 lampeter123 于 2009-11-17 09:26 编辑 ]
----------------解决方案--------------------------------------------------------
谢谢
----------------解决方案--------------------------------------------------------