手记

算法:选择排序法 - Selection Sort (Java)

package com.yuyong.alorithms;

public class SelectionSort {

    /**
     * @param args
     */
    public void selectionSort(int arr[], int n) {

        for (int i = 0; i < n; i++) {
            // 寻找[i,n)区间范围内的最小值
            int minIndex = i;
            int temp;
            for (int j = i + 1; j < n; j++) {
                if (arr[j] < arr[minIndex]) {
                    minIndex = j;
                }
            }
            temp = arr[minIndex];
            arr[minIndex] = arr[i];
            arr[i] = temp;
        }

    }

    public static void main(String[] args) {

        int[] a = { 10, 8, 9, 6, 7, 5, 4, 3, 1, 2 };
        SelectionSort ss = new SelectionSort();
        ss.selectionSort(a, 10);
        for (int i = 0; i < 10; i++) {
            System.out.print(a[i] + " ");
        }
    }

}
2人推荐
随时随地看视频
慕课网APP