Sort Algorithms

1. Standerd

Running time and memory

2. Types of Data

Any type of data that implements Comparable

public class Data implements Comparable<Data> {
  public int compareTo(Data that) {
    case 1: return + 1;
    case 2: return - 1;
    case 3: return  0;
  }
}

compareTo() must implement a total order

3. Simple sort algorithms

  • Selection sort

repeatedly selecting the smallest remaining item

~N^2 / 2 compares
N in place exchange
Insensitive to input
Data movement minimal

public class  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);
     }
  }
}
  • Insertion sort

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容