《算法4第二章》笔记(一)选择排序

算法说明

首先,找到数组中最小的那个元素,其次,将它和数组中的第一个元素交换位置(如果第一个元素就是最小元素那么就和自己交换)。再次,在剩下的元素中找到最小的元素,将它与数组中第二个元素交换位置。如此往复,自导将整个数组排序。

算法复杂度

  • (N-1)+(N-2)+...+2+1=N(N-1)/2 ~ N²/2次比较
  • N次交换

源代码

package edu.princeton.cs.algs4;

import java.util.Comparator;

public class Selection {

    private Selection() { }

    public static void sort(Comparable[] a) {
        int n = a.length;
        for (int i = 0; i < n; i++) {
            int min = i;
            for (int j = i+1; j < n; j++) {
                if (less(a[j], a[min])) min = j;
            }
            exch(a, i, min);
            assert isSorted(a, 0, i);
        }
        assert isSorted(a);
    }

    /**
     * Rearranges the array in ascending order, using a comparator.
     * @param a the array
     * @param comparator the comparator specifying the order
     */
    public static void sort(Object[] a, Comparator comparator) {
        int n = a.length;
        for (int i = 0; i < n; i++) {
            int min = i;
            for (int j = i+1; j < n; j++) {
                if (less(comparator, a[j], a[min])) min = j;
            }
            exch(a, i, min);
            assert isSorted(a, comparator, 0, i);
        }
        assert isSorted(a, comparator);
    }


   /***************************************************************************
    *  Helper sorting functions.
    ***************************************************************************/
    
    // is v < w ?
    private static boolean less(Comparable v, Comparable w) {
        return v.compareTo(w) < 0;
    }

    // is v < w ?
    private static boolean less(Comparator comparator, Object v, Object w) {
        return comparator.compare(v, w) < 0;
    }
        
        
    // exchange a[i] and a[j]
    private static void exch(Object[] a, int i, int j) {
        Object swap = a[i];
        a[i] = a[j];
        a[j] = swap;
    }


   /***************************************************************************
    *  Check if array is sorted - useful for debugging.
    ***************************************************************************/

    // is the array a[] sorted?
    private static boolean isSorted(Comparable[] a) {
        return isSorted(a, 0, a.length - 1);
    }
        
    // is the array sorted from a[lo] to a[hi]
    private static boolean isSorted(Comparable[] a, int lo, int hi) {
        for (int i = lo + 1; i <= hi; i++)
            if (less(a[i], a[i-1])) return false;
        return true;
    }

    // is the array a[] sorted?
    private static boolean isSorted(Object[] a, Comparator comparator) {
        return isSorted(a, comparator, 0, a.length - 1);
    }

    // is the array sorted from a[lo] to a[hi]
    private static boolean isSorted(Object[] a, Comparator comparator, int lo, int hi) {
        for (int i = lo + 1; i <= hi; i++)
            if (less(comparator, a[i], a[i-1])) return false;
        return true;
    }



    // print array to standard output
    private static void show(Comparable[] a) {
        for (int i = 0; i < a.length; i++) {
            StdOut.println(a[i]);
        }
    }

    /**
     * Reads in a sequence of strings from standard input; selection sorts them; 
     * and prints them to standard output in ascending order. 
     *
     * @param args the command-line arguments
     */
    public static void main(String[] args) {
        String[] a = StdIn.readAllStrings();
        Selection.sort(a);
        show(a);
    }
}

算法分析

程序输入来自tiny.txt,内容为

S O R T E X A M P L E

程序入口:

public static void main(String[] args) {
    String[] a = StdIn.readAllStrings();
    Selection.sort(a);
    show(a);
}

逻辑分析:

public static void sort(Comparable[] a) {
    int n = a.length;
    for (int i = 0; i < n; i++) {
        int min = i;
        // 内部循环保证min值为当次循环最小的值
        for (int j = i+1; j < n; j++) {
            if (less(a[j], a[min])) min = j;
        }
        // min与当次第一个元素交换
        exch(a, i, min);
        assert isSorted(a, 0, i);
    }
    assert isSorted(a);
}

算法特点

  • 运行时间和输入无关
    为了找出最小的元素而扫描一遍数组并不能为下一遍扫描提供什么信息。再次排序有序数组或排列主键相等的数组和随机数组排序时间一样。

  • 数据移动是最少的
    每次交换都会改变两个数组元素的值,因此选择排序用了N次交换——交换次数和数组的大小是线性关系。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 概述 排序有内部排序和外部排序,内部排序是数据记录在内存中进行排序,而外部排序是因排序的数据很大,一次不能容纳全部...
    蚁前阅读 5,215评论 0 52
  • 1 初级排序算法 排序算法关注的主要是重新排列数组元素,其中每个元素都有一个主键。排序算法是将所有元素主键按某种方...
    深度沉迷学习阅读 1,435评论 0 1
  • 人生:二十岁没钱,那很正常; 三十岁没钱,可能是没有好的家境,需要更大的努力; 四十岁没钱,只能自己找原因。 无论...
    续写不尽de未来阅读 227评论 0 0
  • 92年的老片 好看 打斗场面很精彩 老牌影星演技真不是盖的 林青霞和张曼玉真心太美 越来越爱看老片子了
    mimihoka阅读 523评论 0 0
  • 啊哒呜啦唔啊嘀呢咕嘚莱嘀哩莱咿呀唯唉哪嗷嘟好嘶哔kī哎呐呢唉哒呜啦咕哇嘀叨哔哈唉呐呼嗖非呦啊嘀哔咿啦呐嘶呋。
    濯微凉阅读 448评论 0 0