Algorithms - Week 3-0 Mergesort
来源:互联网 发布:淘宝明朗体育怎么样 编辑:程序博客网 时间:2024/06/17 22:11
Mergesort
- Divide array into two halves.
- Recursively sort each half.
- Merge two halves.
Java implementation, notice that aux
public class Merge { private static void sort(Comparable[] a, Comparable[] aux, int lo, int hi); public static void sort(Comparable[] a) { aux = new Comparable[a.length]; sort(a, aux, 0, a.length - 1); }}
Running time estimates:
- Laptop executes
108 compares/second. - Supercomputer executes
1012 compares/second.
A sorting algorithm is in-place if it uses
proof by picture, proof by expansion, proof by induction
practical improvements
Use insertion sort for small subarrays.
- Mergesort has too much overhead for tiny subarrays.
- Cutoff to insertion sort for
≈7 items.
Stop if already sorted.
- Is biggest item in first half
≤ smallest item in second half? - Helps for partially-ordered arrays.
Eliminate the copy to the auxiliary array. Save time (but not space) by switching the role of the input and auxiliary array in each recursive call.
private static void sort(Comparable[] a, Comparable[] aux, int lo, int hi){ if (hi <= lo) return; int mid = lo + (hi - lo) / 2; sort (aux, a, lo, mid); sort (aux, a, mid+1, hi); merge(a, aux, lo, mid, hi);}
Bottom-up Mergesort
- Pass through array, merging subarrays of size 1.
- Repeat for subarrays of size 2, 4, 8, 16, …
Bottom line. Simple and non-recursive version of mergesort. but about 10% slower than recursive, top-down mergesort on typical systems
public static void sort(Comparable[] a){ int N = a.length; Comparable[] aux = new Comparable[N]; for (int sz = 1; sz < N; sz = sz+sz) for (int lo = 0; lo < N-sz; lo += sz+sz) merge(a, aux, lo, lo+sz-1, Math.min(lo+sz+sz-1, N-1));}
Sorting Complexity
Decision tree
- Model of computation. Allowable operations.
- Cost model. Operation count(s).
- Upper bound. Cost guarantee provided by some algorithm for X.
- Lower bound. Proven limit on cost guarantee of all algorithms for X.
- Optimal algorithm. Algorithm with best possible cost guarantee for X.
Example: sorting.
- Model of computation: decision tree.
- Cost model: # compares.
- Upper bound: ~
NlogN from mergesort. - Lower bound: ~
NlogN . - Optimal algorithm = mergesort.
Comparators
different sort keys
public interface Comparator<Key> int compare(Key v, Key w);public static void sort(Object[] a, Comparator comparator){ int N = a.length; for (int i = 0; i < N; i++) for (int j = i; j > 0 && less(comparator, a[j], a[j-1]); j--) exch(a, j, j-1);}private static boolean less(Comparator c, Object v, Object w){ return c.compare(v, w) < 0; }private static void exch(Object[] a, int i, int j){ Object swap = a[i]; a[i] = a[j]; a[j] = swap; } public class Student{ public static final Comparator<Student> BY_NAME = new ByName(); public static final Comparator<Student> BY_SECTION = new BySection(); private final String name; private final int section; ... private static class ByName implements Comparator<Student> { public int compare(Student v, Student w) { return v.name.compareTo(w.name); } } private static class BySection implements Comparator<Student> { public int compare(Student v, Student w) { return v.section - w.section; } }}
Stability
A stable sort preserves the relative order of items with equal keys.
Insertion sort is stable. Equal items never move past each other.
Selection sort is not stable. Long-distance exchange might move an item past some equal item.
Shellsort sort is not stable. Long-distance exchanges.
Mergesort is stable. Merge operation is stable. Takes from left subarray if equal keys.
- Algorithms - Week 3-0 Mergesort
- Princeton Algorithms: Part 1 [week 3:Mergesort]
- Algorithms - Week 3-1 Quicksort
- Sorting Algorithms ------ [MergeSort]
- Algorithms - Week 2-0 Stacks and Queues
- Algorithms - Week 4-0 Priority Queues
- Princeton Algorithms: Part 1 [week 3: Quicksort]
- Stanford Algorithms: Design and Analysis, Part 1[week 3]
- Stanford Algorithms: Design and Analysis, Part 2[week 3]
- Princeton Algorithms: Part 2 [week 3: Maxflow and Mincut]
- Princeton Algorithms: Part 2 [week 3: Radix Sorts]
- Algorithms - Week 1-1 Analysis of Algorithms
- Princeton Algorithms: Part 1 [week 1:Analysis of Algorithms]
- Stanford - Algorithms: Design and Analysis, Part 1 - Week 3 Assignment: Contraction
- Week 5 Assignment - Princeton-Algorithms-PartI
- Princeton Algorithms: Part 2 [week 5: Tries]
- Princeton Algorithms: Part 2 [week 7: Reductions]
- Princeton Algorithms: Part 2 [week 7: Intractability]
- C++11系列学习之二-----lambda表达式
- Algorithms - Week 2-1 Elementary Sorts
- HttpClient 4.3教程 第一章 基本概念
- js获取url参数值的两种方式
- A *a=new B();
- Algorithms - Week 3-0 Mergesort
- Java基础--------网络编程
- hdu 5427 A problem of sorting 解题报告
- 关于Runtime Permissions的学习以及使用的一些注意事项
- 在Spark中自定义Kryo序列化输入输出API
- javascript打印网页的特定内容(以打印table为例)
- java提高篇(三十)-----Iterator
- 游戏人工智能开发之进阶版随机技术
- List接口中特有方法