菜鸟求助 最简单的插入排序问题 不知道哪里出问题了
时间:2011-09-02
来源:互联网
程序如下package sort;
public class InsertSort {
static int[] myarray = new int[10];
public static void ran_num(){
for (int i = 0 ; i < 10; i++){
myarray[i] = (int) (Math.random() * 10);
System.out.print(myarray[i]+ " ");
}
}
public static void MyInsertSort(){
int temp;
for(int i=1; i<myarray.length ; i++ ){
temp = myarray[i];
for(int j=0;j<i;j++){
if(temp<myarray[j]){
for(int k=i ; j<k ; k--)
{
myarray[k]=myarray[k-1];
}
myarray[j]=temp;
}
for(int m=0;m<10;m++)
System.out.print(myarray[m]+" ");
System.out.println();
}
}
for(int m=0;m<10;m++)
System.out.print(myarray[m]+" ");
}
public static void main(String[] args) {
ran_num();
System.out.println();
MyInsertSort();
}
}
这是输出结果
8 2 8 8 0 2 5 1 5 8
2 8 8 8 0 2 5 1 5 8
2 8 8 8 0 2 5 1 5 8
2 8 8 8 0 2 5 1 5 8
2 8 8 8 0 2 5 1 5 8
2 8 8 8 0 2 5 1 5 8
2 8 8 8 0 2 5 1 5 8
0 2 8 8 8 2 5 1 5 8
0 0 2 8 8 2 5 1 5 8
0 0 0 2 8 2 5 1 5 8
0 0 0 0 2 2 5 1 5 8
0 0 0 0 2 2 5 1 5 8
0 0 0 0 2 2 5 1 5 8
0 0 0 0 2 2 5 1 5 8
0 0 0 0 2 2 5 1 5 8
0 0 0 0 2 2 5 1 5 8
0 0 0 0 2 2 5 1 5 8
0 0 0 0 2 2 5 1 5 8
0 0 0 0 2 2 5 1 5 8
0 0 0 0 2 2 5 1 5 8
0 0 0 0 2 2 5 1 5 8
0 0 0 0 2 2 5 1 5 8
0 0 0 0 2 2 5 1 5 8
0 0 0 0 2 2 5 1 5 8
0 0 0 0 2 2 5 1 5 8
0 0 0 0 2 2 5 1 5 8
0 0 0 0 1 2 2 5 5 8
0 0 0 0 1 1 2 2 5 8
0 0 0 0 1 1 1 2 5 8
0 0 0 0 1 1 1 2 5 8
0 0 0 0 1 1 1 2 5 8
0 0 0 0 1 1 1 2 5 8
0 0 0 0 1 1 1 2 5 8
0 0 0 0 1 1 1 2 5 8
0 0 0 0 1 1 1 2 5 8
0 0 0 0 1 1 1 2 5 8
0 0 0 0 1 1 1 2 5 8
0 0 0 0 1 1 1 2 5 8
0 0 0 0 1 1 1 2 5 8
0 0 0 0 1 1 1 2 5 8
0 0 0 0 1 1 1 2 5 8
0 0 0 0 1 1 1 2 5 8
0 0 0 0 1 1 1 2 5 8
0 0 0 0 1 1 1 2 5 8
0 0 0 0 1 1 1 2 5 8
0 0 0 0 1 1 1 2 5 8
0 0 0 0 1 1 1 2 5 8
public class InsertSort {
static int[] myarray = new int[10];
public static void ran_num(){
for (int i = 0 ; i < 10; i++){
myarray[i] = (int) (Math.random() * 10);
System.out.print(myarray[i]+ " ");
}
}
public static void MyInsertSort(){
int temp;
for(int i=1; i<myarray.length ; i++ ){
temp = myarray[i];
for(int j=0;j<i;j++){
if(temp<myarray[j]){
for(int k=i ; j<k ; k--)
{
myarray[k]=myarray[k-1];
}
myarray[j]=temp;
}
for(int m=0;m<10;m++)
System.out.print(myarray[m]+" ");
System.out.println();
}
}
for(int m=0;m<10;m++)
System.out.print(myarray[m]+" ");
}
public static void main(String[] args) {
ran_num();
System.out.println();
MyInsertSort();
}
}
这是输出结果
8 2 8 8 0 2 5 1 5 8
2 8 8 8 0 2 5 1 5 8
2 8 8 8 0 2 5 1 5 8
2 8 8 8 0 2 5 1 5 8
2 8 8 8 0 2 5 1 5 8
2 8 8 8 0 2 5 1 5 8
2 8 8 8 0 2 5 1 5 8
0 2 8 8 8 2 5 1 5 8
0 0 2 8 8 2 5 1 5 8
0 0 0 2 8 2 5 1 5 8
0 0 0 0 2 2 5 1 5 8
0 0 0 0 2 2 5 1 5 8
0 0 0 0 2 2 5 1 5 8
0 0 0 0 2 2 5 1 5 8
0 0 0 0 2 2 5 1 5 8
0 0 0 0 2 2 5 1 5 8
0 0 0 0 2 2 5 1 5 8
0 0 0 0 2 2 5 1 5 8
0 0 0 0 2 2 5 1 5 8
0 0 0 0 2 2 5 1 5 8
0 0 0 0 2 2 5 1 5 8
0 0 0 0 2 2 5 1 5 8
0 0 0 0 2 2 5 1 5 8
0 0 0 0 2 2 5 1 5 8
0 0 0 0 2 2 5 1 5 8
0 0 0 0 2 2 5 1 5 8
0 0 0 0 1 2 2 5 5 8
0 0 0 0 1 1 2 2 5 8
0 0 0 0 1 1 1 2 5 8
0 0 0 0 1 1 1 2 5 8
0 0 0 0 1 1 1 2 5 8
0 0 0 0 1 1 1 2 5 8
0 0 0 0 1 1 1 2 5 8
0 0 0 0 1 1 1 2 5 8
0 0 0 0 1 1 1 2 5 8
0 0 0 0 1 1 1 2 5 8
0 0 0 0 1 1 1 2 5 8
0 0 0 0 1 1 1 2 5 8
0 0 0 0 1 1 1 2 5 8
0 0 0 0 1 1 1 2 5 8
0 0 0 0 1 1 1 2 5 8
0 0 0 0 1 1 1 2 5 8
0 0 0 0 1 1 1 2 5 8
0 0 0 0 1 1 1 2 5 8
0 0 0 0 1 1 1 2 5 8
0 0 0 0 1 1 1 2 5 8
0 0 0 0 1 1 1 2 5 8
作者: quit5575 发布时间: 2011-09-02
从结果看不是排对了吗?
另外,插入排序不是你这么做的,不会有myarray[k]=myarray[k-1];这种步骤,而是每次发现比第一个小的话就跟第一个换,代码就不写了,给个例子好了。
第一次循环:(换第一个)
8 2 8 8 0 2 5 1 5 8
2 8 8 8 0 2 5 1 5 8
0 8 8 8 2 2 5 1 5 8
第二次循环: (换第二个)
0 2 8 8 8 2 5 1 5 8
0 1 8 8 8 2 5 2 5 8
第三次循环: (换第三个)
0 1 2 8 8 8 5 2 5 8
。。。
。。。
以此类推
另外,插入排序不是你这么做的,不会有myarray[k]=myarray[k-1];这种步骤,而是每次发现比第一个小的话就跟第一个换,代码就不写了,给个例子好了。
第一次循环:(换第一个)
8 2 8 8 0 2 5 1 5 8
2 8 8 8 0 2 5 1 5 8
0 8 8 8 2 2 5 1 5 8
第二次循环: (换第二个)
0 2 8 8 8 2 5 1 5 8
0 1 8 8 8 2 5 2 5 8
第三次循环: (换第三个)
0 1 2 8 8 8 5 2 5 8
。。。
。。。
以此类推
作者: lz0625 发布时间: 2011-09-02
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28