直接选择排序(java实现)

import java.util.Arrays;

class DataWraper implements Comparable<DataWraper>{
    int data;
    String flag;
   
    public DataWraper(int data, String flag) {
        super();
        this.data = data;
        this.flag = flag;
    }
    public String toString(){
        return data+flag;
    }
   
    @Override
    public int compareTo(DataWraper dw) {
        // TODO Auto-generated method stub
        return this.data>dw.data?1:(this.data==dw.data?0:-1);
    }
   
}
public class SelectSort {
//直接选择排序
    public static void selectSort(DataWraper[] data){
        System.out.println("开始排序");
        int arrayLength=data.length;
        for(int i=0;i<arrayLength-1;i++){
            int minIndex=i;
            for(int j=i+1;j<arrayLength;j++){
                if(data[i].compareTo(data[j])>0){
                    DataWraper tmp=data[i];
                    data[i]=data[j];
                    data[j]=tmp;
                }
            }
            System.out.println(java.util.Arrays.toString(data));
        }
    }
   
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        DataWraper [] data={
                new DataWraper(21, ""),
                new DataWraper(30, ""),
                new DataWraper(49, ""),
                new DataWraper(30, "*"),
                new DataWraper(16, ""),
                new DataWraper(9, ""),
               
        };
        System.out.println("排序之前:\n"+Arrays.toString(data));
        selectSort(data);
        System.out.println("排序之后:\n"+Arrays.toString(data));
    }

}

结果:

排序之前:
[21, 30, 49, 30*, 16, 9]
开始排序
[9, 30, 49, 30*, 21, 16]
[9, 16, 49, 30*, 30, 21]
[9, 16, 21, 49, 30, 30*]
[9, 16, 21, 30, 49, 30*]
[9, 16, 21, 30, 30*, 49]
排序之后:
[9, 16, 21, 30, 30*, 49]

改进后:

import java.util.Arrays;

class DataWraper implements Comparable<DataWraper>{
    int data;
    String flag;
   
    public DataWraper(int data, String flag) {
        super();
        this.data = data;
        this.flag = flag;
    }
    public String toString(){
        return data+flag;
    }
   
    @Override
    public int compareTo(DataWraper dw) {
        // TODO Auto-generated method stub
        return this.data>dw.data?1:(this.data==dw.data?0:-1);
    }
   
}
public class SelectSort {
//直接选择排序
    public static void selectSort(DataWraper[] data){
        System.out.println("开始排序");
        int arrayLength=data.length;
        //依次进行n-1趟比较,第i趟比较将第i大的值选出放在i位置
        for(int i=0;i<arrayLength-1;i++){
            //minIndex永远保存本趟比较中最小值的索引
            int minIndex=i;
            //第i个数据只需要和它后面的数据比较
            for(int j=i+1;j<arrayLength;j++){
                //如果第minIndex位置的数据>j位置的数据
                if(data[minIndex].compareTo(data[j])>0){
                    minIndex=j;
                }
            }
            //每趟比较最多交换一次
            if(minIndex!=i){
                    DataWraper tmp=data[i];
                    data[i]=data[minIndex];
                    data[minIndex]=tmp;       
            }
            System.out.println(java.util.Arrays.toString(data));
        }
    }
   
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        DataWraper [] data={
                new DataWraper(21, ""),
                new DataWraper(30, ""),
                new DataWraper(49, ""),
                new DataWraper(30, "*"),
                new DataWraper(16, ""),
                new DataWraper(9, ""),
               
        };
        System.out.println("排序之前:\n"+Arrays.toString(data));
        selectSort(data);
        System.out.println("排序之后:\n"+Arrays.toString(data));
    }

}

结果:

排序之前:
[21, 30, 49, 30*, 16, 9]
开始排序
[9, 30, 49, 30*, 16, 21]
[9, 16, 49, 30*, 30, 21]
[9, 16, 21, 30*, 30, 49]//不交换
[9, 16, 21, 30*, 30, 49]//不交换
[9, 16, 21, 30*, 30, 49]
排序之后:
[9, 16, 21, 30*, 30, 49]

posted @ 2011-07-18 11:22  朱旭东  阅读(624)  评论(0编辑  收藏  举报