标题:交换排序 错在哪里啊。
只看楼主
ljxch
Rank: 2
等 级:论坛游民
帖 子:6
专家分:11
注 册:2010-6-1
结帖率:0
已结贴  问题点数:20 回复次数:2 
交换排序 错在哪里啊。

public class Swap {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        //交换法
        
        int[] score = new int[100];
        //初始化数组
        for (int i = 0; i <= score.length - 1; i++) {
            score[i] = (int)(Math.random() * 100);
        }        
        int temp ;//中间变量
        int i ;
        int j ;
        for (i = 0; i < score.length - 1; i++) {
            int min = i;//假设最小值
            
            //找最小值
            for (j = i + 1; j <= score.length - 1; j++) {
                if (score[j] < min) {
                    min = j;
                }
            }
            //交换最小值的位置
            if (min != i) {
                j--;
                temp = score[i];
                score[i] = score[j];
                score[j] = temp ;
            }
        }
        
        System.out.println("排序后的数组:");
        for (int k = 0; k <= score.length - 1; k++) {
            if (k % 10 == 0) {
                System.out.println();
            }
            System.out.print(score[k]+"\t");
        }
        
    }
   

}
搜索更多相关主题的帖子: public method 
2010-06-21 19:29
myhnuhai
Rank: 10Rank: 10Rank: 10
等 级:青峰侠
威 望:3
帖 子:425
专家分:1725
注 册:2010-3-17
得分:20 
public class Swap{
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int[] score = new int[100];
        // 初始化数组
        for (int i = 0; i <= score.length - 1; i++)
            score[i] = (int)(Math.random()*100);      
        int temp ;// 中间变量
        int i ;
        int j ;
        for (i = 0; i < score.length - 1; i++)
            for (j = i + 1; j <score.length; j++)
            {
                if (score[j] < score[i])
                {
                temp = score[i];
                score[i] = score[j];
                score[j] = temp ;
            }
        }
        System.out.println("排序后的数组:");
        for (int k = 0; k <= score.length - 1; k++)
        {
            if (k % 10 == 0)
                System.out.println();
            System.out.print(score[k]+"\t");
        }  
    }
}

不要让肮脏的记忆,迷失了原本纯洁的心灵!
2010-06-21 22:27
ljxch
Rank: 2
等 级:论坛游民
帖 子:6
专家分:11
注 册:2010-6-1
得分:0 
好的,谢谢
2010-06-22 11:16



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-310851-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.495939 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved