4.1 Priority Queues
1. Priority Queues
-
區(qū)分:
- Stack: 最后添加的item,最先被刪(LIFO)
- Queue:最早添加的item,最先被刪(FIFO)
- Randomized queue:隨機刪除一個item
- Priority queue:刪除最大(或最小)的item
-
思路:
- Plan A :先不排序致稀,insert后就放在序列尾;要remove時,找出最大的刪除
- Plan B:每insert一個乖坠,就按升序排一次序;要remove時刀闷,直接刪除最后一個
-
Performance
implementation insert delMax max Plan A:Unordered array 1 N N Plan B:ordered array N 1 1 -
Java Implementation(Plan A)
public class UnorderedMaxPQ<Key extends Comparable<Key>>{ private Key[] pq; private int n; // number of elements on pq // create an empty priority queue public UnorderedMaxPQ(int capacity){ // no generic array creation pq = (Key[]) new Comparable[capacity]; } // insert a key into the priority queue public void insert(Key v){ pq[n++] = x; } // return and remove the largest key public Key delMax(){ int max = 0; for(int i = 1; i < n; i++){ if(less(pq, max, i)){ max = i; } } exch(pq, max, n-1); return pq[--n]; // null out entry to prevent loitering } public Key max(){ int max = 0; for(int i = 1; i < n; i++){ if(less(pq, max, i)){ max = i; } } return pq[max]; } // is the priority queue empty? public boolean isEmpty(){ return n == 0; } private static boolean less(Comparable a, int i, int j){ return a[i].CompareTo(a[j]) < 0; } private static void exch(Comparable[] a, int i, int j){ Comparable swap = a[i]; a[i] = a[j]; a[j] = swap; } }
2. Binary heaps
-
complete tree
除了最底層熊泵,其他都完全對稱
N個節(jié)點,高度:[lgN]
-
思路
- Array代表了一個heap-ordered complete binary tree
- heap-ordered complete binary tree
- Keys在各個Nodes
- Parent‘s key不能小于children’s key
- Array
- 讓indices從1開始
- Nodes按層排序
- heap-ordered complete binary tree
- array[1]就是最大的key甸昏,即為binary tree的root
- 對于array[k]
- Parent: array[k/2]
- Children: array[2k]顽分,array[2k+1]
- Array代表了一個heap-ordered complete binary tree
-
Performance
implementation insert delMax max Plan A:Unordered array 1 N N Plan B:ordered array N 1 1 Binary heap lgN lgN 1 -
Java implementation
public class MaxPQ<Key extends Comparable<Key>>{ private Key[] pq; private int n; public MaxPQ(int capacity){ pq = (Key[]) new Comparable[capacity+1]; // 因為我們假設(shè)從1開始,所以capacity需要+1 } public boolean isEmpty(){ return n == 0; } public void insert(Key key){ // at most 1+lgN compares pq[++n] = x; swim(n); } public Key delMax(){ // at most 2lgN compares Key max = pq[1]; exch(1, n); sink(1); pq[n] = null; n--; return max; } // Child's key變得比它的parent‘s key大 private void swim(int k){ while(k>1 && less(k/2, k)){ exch(k/2, k); k = k/2; } } // Parent's key 變得比它的其中一個child或者比兩個children都小 private void sink(int k){ while(2*k <= n){ int j = 2*k; if(j < n && less(j, j+1)){ //找出兩個children中較大的那個 j++; } if(!less(k, j)){// parent和較大的那個child比較 break; } exch(k, j); k = j; } } private static boolean less(int i, int j){ return pq[i].CompareTo(pq[j]) < 0; } private static void exch(int i, int j){ Key swap = pq[i]; pq[i] = pq[j]; pq[j] = swap; } }
3. Immutability
-
immutable data type: 一旦創(chuàng)建施蜜,data type value不能改變
-
immutable:String卒蘸, Integer,Double翻默,Vector
// can't override instance methods public final class Vector{ // all instance variable private and final private final int n; private final double[] data; public Vector(double[] data){ this.n = data.length; this.data = new double[n]; for(int i = 0; i < n; i++){ this.data[i] = data[i]; } } // instance methods don't change instance variable ... }
-
- mutable:StringBuilder,Stack修械,Counter趾牧,Java array
4. Heapsort
-
思路:
開始有一個無序的array
建一個有n個keys的max-heap(依然假設(shè)index是從1到n)
不斷地取出最大的key
-
Performance
- Heap construction :
compares and exchanges
- Heapsort:
compares and exchanges
- Heap construction :
-
Java implementation
public class Heap{ public static void sort(Comparable[] a){ // 假設(shè) array 0 到 1 int n = a.length; for(int k = k/2; k >=1; k--){ sink(a, k, n); } while(n > 1){ exch(a, 1, n); sink(a, 1, --n); } } private static void sink(Comparable[] a, int k, int n){ while(2*k <= n){ int j = 2*k; if(j < n && less(a, j, j+1)){ //找出兩個children中較大的那個 j++; } if(!less(a, k, j)){// parent和較大的那個child比較 break; } exch(a, k, j); k = j; } } private static boolean less(Comparable[] a, int i, int j){ // 注意:是1-based indexing return a[i-1].CompareTo(a[j-1]) < 0; } private static void exch(Comparable[] a, int i, int j){ // // 注意:是1-based indexing Key swap = pq[i-1]; pq[i-1] = pq[j-1]; pq[j-1] = swap; } }
-
總結(jié)(Sorting algorithms)
inplace? stable? worst average best remarks selection √ N exchanges insertion √ √ 適用small N或partially ordered shell √ ? ? code少 quick √ probabilistic guarantee(用shuffle),fastes in practice
3-way quick √ 對quicksort的改良肯污,適用于有很多duplicate keys merge √ guarantee翘单,stable
heap √ guarantee吨枉,inplace