首页 > 代码库 > 《算法导论》习题2.2-2 选择排序

《算法导论》习题2.2-2 选择排序

伪代码:

SELECTION-SORT
1
for i=2 to A.length-12  max = A[i]3  mark = i4 for j=i+1 to A.length5 if A[j]>max6  max=A[j]7 mark = j8 A[mark]=A[i]9 A[i] = max

Java 实现:

public class SelectionSort {        public static double [] sort(double [] A)    {        for(int i = 0; i<A.length-1; i++)        {            double max = A[i];            int mark = i;            for(int j =i+1;j<A.length;j++)              if(A[j]>max)              {                  max = A[j];                  mark = j;              }            A[mark] = A[i];            A[i]=max;        }        return A;    }    public static void main(String[] args) {        double [] A = {1.3, 5 ,2, 6.9, 2.0,7.8,4.3};        A = SelectionSort.sort(A);        for(double a:A)            System.out.print(a+" ");    }}

 

《算法导论》习题2.2-2 选择排序