Java八大排序算法之选择排序

Java选择排序之简单选择排序

直接贴一个简单的Demo

package com.ysk.xuanze;

public class jiandanxuanze {
    public void slectsort() {
        int a[] = { 49, 38, 65, 97, 76, 1, 13, 27, 49, 78, 34, 12, 64, 5, 4, 62, 99, 98, 54, 56, 17, 18, 23, 34, 15, 35,
                25, 53, 51 };
        int position = 0;
        for (int i = 0; i < a.length; i++) {
            int temp = a[i];
            position = i;
            int j = i + 1;
            for (; j < a.length; j++) {
                if (temp > a[j]) {
                    temp = a[j];
                    position = j;
                }
            }
            a[position] = a[i];
            a[i] = temp;
        }
        for (int i = 0; i < a.length; i++) {
            System.out.print(a[i] + " ");
        }
    }
}

你可能感兴趣的:(Java算法)