算法說(shuō)明
首先,找到數(shù)組中最小的那個(gè)元素斑鸦,其次窖贤,將它和數(shù)組中的第一個(gè)元素交換位置(如果第一個(gè)元素就是最小元素那么就和自己交換)砖顷。再次,在剩下的元素中找到最小的元素赃梧,將它與數(shù)組中第二個(gè)元素交換位置滤蝠。如此往復(fù),自導(dǎo)將整個(gè)數(shù)組排序授嘀。
算法復(fù)雜度
- (N-1)+(N-2)+...+2+1=N(N-1)/2 ~ N2/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);
}
}
算法分析
程序輸入來(lái)自tiny.txt物咳,內(nèi)容為
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;
// 內(nèi)部循環(huán)保證min值為當(dāng)次循環(huán)最小的值
for (int j = i+1; j < n; j++) {
if (less(a[j], a[min])) min = j;
}
// min與當(dāng)次第一個(gè)元素交換
exch(a, i, min);
assert isSorted(a, 0, i);
}
assert isSorted(a);
}
算法特點(diǎn)
運(yùn)行時(shí)間和輸入無(wú)關(guān)。
為了找出最小的元素而掃描一遍數(shù)組并不能為下一遍掃描提供什么信息蹄皱。再次排序有序數(shù)組或排列主鍵相等的數(shù)組和隨機(jī)數(shù)組排序時(shí)間一樣览闰。數(shù)據(jù)移動(dòng)是最少的。
每次交換都會(huì)改變兩個(gè)數(shù)組元素的值巷折,因此選擇排序用了N次交換——交換次數(shù)和數(shù)組的大小是線性關(guān)系压鉴。