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 clogN extra memory.

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.

0 0
原创粉丝点击