内排序-简单选择排序

算法思想:  每次从序列 (i=0.1.2.......n-1) 中选出一个i值作为最大或者最小值如此下去完成排序,具体做法是假定,i是最小或最大,再和i+1 .....n-1的值比较,以确定最大或者最小的序号,而后交换他们的值。

package Sort;

public class SelectSort
{
    public static void main(String[] args)
    {

        int[] a = new int[] { 20, 30, 90, 40, 70, 110, 60, 10, 100, 50, 80, 1, 2 };
        SelectSort selectsort = new SelectSort();
        selectsort.Sort(a);
        
        for (int t : a)
            System.out.println(t);
    }

    void ExchangeData(int[] datas, int index1, int index2)
    {
        datas[index1]=  datas[index1]+datas[index2];
         
         datas[index2]=  datas[index1]-datas[index2];
         
         datas[index1]=  datas[index1]-datas[index2];
    }

    public void Sort(int[] array)
    {
        int n=array.length;
        int min_index=0;
        for(int i=0;i<n;i++)
        {
            min_index=i;
            for(int j=min_index+1;j<n;j++)
            {
                if(array[j]<array[min_index])
                {
                    min_index=j;                    
                }
            }
            ExchangeData(array,i,min_index);            
            
        }
        
    }

}

猜你喜欢

转载自www.cnblogs.com/mytrip/p/9478280.html
今日推荐