sort.java

来自「Java经典例程 从外国一大学计算机教授出版物下载的代码 经典」· Java 代码 · 共 30 行

JAVA
30
字号
package myutilities;

public class Sort {

    /* The Sort class   by J M Bishop  Feb 1997
     *                  revised October 1997
     * Provides one sorting method
     * for arrays of objects of any length.
     * where the objects' class implements the
     * Sortable interface.
     */

  public static void selectionSort(Sortable a [], int n) {
    Sortable temp;
    int chosen;

    for (int leftmost = 0; leftmost < n-1; leftmost++) {
      chosen = leftmost;
      for (int j = leftmost+1; j < n; j++) {
        if (a[j].lessThan(a[chosen])) {
          chosen = j;
        }
      }
      temp = a[chosen];
      a[chosen] = a[leftmost];
      a[leftmost] = temp;
    }
  }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?