/*
 * OrderSort.java
 * Created on 2007年10月31日, 下午10:19
 * @author mu yu
 */
public class OrderSort {
    
    /** Creates a new instance of OrderSort */
    public OrderSort() {
    }
    //插入排序
    static void insertSort(int[] a){
        int in,out;
        for(out=1;out<a.length;out++){
            int temp=a[out];
            for(in=out;in>0 && a[in-1]>temp;--in)
                a[in]=a[in-1];
            a[in]=temp;
        }
    }
    //选择排序
    static void selectSort(int[] a){
        int in,out;
        int minIndex; //最小值的下标
        for(out=0;out<a.length-1;out++){
            minIndex=out;
            for(in=out+1;in<a.length;in++)
                if (a[in]<a[minIndex])
                    minIndex=in;
            if(out!=minIndex){
                int temp=a[out];
                a[out]=a[minIndex];
                a[minIndex]=temp;
            }
        }
    }
    //冒泡排序
    static void bubbleSort(int[] a){
        int in,out;
        for(out=1;out<a.length;out++){
            for(in=0;in<a.length-out;in++)
                if (a[in]>a[in+1]){
                    int temp=a[in];
                    a[in]=a[in+1];
                    a[in+1]=temp;
                }
        }
    }
}
class sortApp{
    int[] a=new int[10];
    void setValue(){
        for(int i=0;i<a.length;i++)
            a[i]=(int)(Math.random()*100);
    }
    void display(){
        for(int i=0;i<a.length;i++)
            System.out.print(a[i]+" ");
        System.out.println();
    }
    public static void main(String[] args){
        sortApp s1=new sortApp();
        System.out.println("***************插入排序******************");
        System.out.print("\t排序前:\n\t");
        s1.setValue();
        s1.display();
        System.out.print("\t排序后:\n\t");
        OrderSort.insertSort(s1.a);
        s1.display();
        System.out.println("***************选择排序******************");
        System.out.print("\t排序前:\n\t");
        s1.setValue();
        s1.display();
        System.out.print("\t排序后:\n\t");
        OrderSort.selectSort(s1.a);
        s1.display();
        
        System.out.println("***************冒泡排序******************");
        System.out.print("\t排序前:\n\t");
        s1.setValue();
        s1.display();
        System.out.print("\t排序后:\n\t");
        OrderSort.bubbleSort(s1.a);
        s1.display();
    }
}
init:
deps-jar:
Compiling 1 source file to E:\javawork\DataStructure\build\classes
compile:
run:
***************插入排序******************
        排序前:
        90 34 0 10 46 0 21 99 88 47 
        排序后:
        0 0 10 21 34 46 47 88 90 99 
***************选择排序******************
        排序前:
        46 7 47 59 56 63 23 80 3 92 
        排序后:
        3 7 23 46 47 56 59 63 80 92 
***************冒泡排序******************
        排序前:
        65 65 94 57 29 44 80 41 57 95 
        排序后:
        29 41 44 57 57 65 65 80 94 95 
生成成功(总时间:1 秒)

 
											





