sequences

30
SEQUENCES SORTING Merge, Quick, Bucket, Heap, Radix

Upload: aulii

Post on 06-Jan-2016

15 views

Category:

Documents


0 download

DESCRIPTION

SEQUENCES. SORTING Merge, Quick, Bucket, Heap, Radix. Divide-and-Conquer. Divide if input size < threshold, solve directly else, divide data into 2 or more subsets Recurse recursively solve subproblems associated with subsets Conquer take solutions to subproblems - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: SEQUENCES

SEQUENCES

SORTINGMerge, Quick, Bucket, Heap, Radix

Page 2: SEQUENCES

Divide-and-Conquer

Divide if input size < threshold, solve directly else, divide data into 2 or more subsets

Recurse recursively solve subproblems associated

with subsetsConquer

take solutions to subproblems combine into solution to orginal problem

Page 3: SEQUENCES

Merge Sort

Divide divide S into 2 equal sequences S1 and

S2

Recurse recursively sort sequences S1 and S2

Conquer merge sorted sequences S1 and S2 back

into S

Page 4: SEQUENCES

public class ListMergeSort implements SortObject{

public void sort(Sequence S, Comparator c){

int n=S.size();if (n<2) return;Sequence S1=(Sequence) S.newContainer();for (int i=1;i<=(n+1)/2;i++)

S1.insertLast(S.remove(S.first()));Sequence S2=(Sequence) S.newContainer();for (int i=1;i<=n/2;i++)

S2.insertLast(S.remove(S.first()));sort(S1,c);sort(S2,c);merge(S1,S2,c,S);

}…}

Page 5: SEQUENCES

public class ListMergeSort implements SortObject{…

public void merge(Sequence S1,Sequence S2, Comparator c, Sequence S){ while(!S1.isEmpty() && !S2.isEmpty()) if (c.isLessThanOrEqualTo(S1.first().element(),S2.first().element())) S.insertLast(S1.remove(S1.first())); else S.insertLast(S2.remove(S2.first())); if(S1.isEmpty()) while(!S2.isEmpty()) S.insertLast(S2.remove(S2.first())); if(S2.isEmpty()) while(!S1.isEmpty()) S.insertLast(S1.remove(S1.first()));}

}

Page 6: SEQUENCES

Merge Sort Example

6 2 8 5 10 9 12 12 6 5 8 10 9 12 12 6 5 8 10 9 12 12 5 6 8 10 9 12 12 5 6 8 9 10 12 12 5 6 8 9 10 1 122 5 6 8 1 9 10 121 2 5 6 8 9 10 12

Page 7: SEQUENCES

Closer Look At Merging

S1: 24-45-63-85S2: 17-31-50-96S:

S1: 24-45-63-85S2: 31-50-96S: 17

S1: 45-63-85S2: 31-50-96S: 17-24

S1: 45-63-85S2: 50-96S: 17-24-31

S1: 63-85S2: 50-96S: 17-24-31-45

S1: 63-85S2: 96S: 17-24-31-45-50

S1: 85S2: 96S: 17-24-31-45-50-

63

S1:S2: 96S: 17-24-31-45-50-63-85

S1:S2:S: 17-24-31-45-50-

63-85-96

Page 8: SEQUENCES

Merge Sort TC

t(n) = time to merge sort n elementst(n/2) = time to merge sort n/2

elementst(n) = 2t(n/2) + O(n), a recurrence

relation

==> merge sort is O(n lg n)Note: merge sort requires extra space

Page 9: SEQUENCES

Recurrence Relations

T(n) = aT(n/b) +cnk

T(n) = O(nlogba) if a>bk

T(n) = O(nklogn) if a=bk

T(n) = O(nk) if a<bk

Page 10: SEQUENCES

Quick SortDivide: if S has at least 2 elements,

select x from S called the pivot. Put elements of S into 3 subsequences: L, all elements in S less than x E, all elements in S equal to x G, all elements in S greater than x

Recurse: recursively sort L and GConquer: Put back elements into S in

order by concatenating L, E, and G

Page 11: SEQUENCES

public class ArrayQuickSort implements SortObject{

public void sort(Sequence S, Comparator c){

sort(S, c, 0, S.size()-1);}

private void quicksort(Sequence S, Comparator c, int leftBound, int rightBound){

if(S.size()<2) return;if(leftBound>=rightBound) return;Object pivot=S.atRank(rightBound).element();int leftIndex=leftBound;int rightIndex=rightBound-1;

…}

}

Page 12: SEQUENCES

public class ArrayQuickSort implements SortObject{ …

privat void quicksort(Sequence S, comparator c, int leftBound, int rightBound){ …

while(leftIndex<=rightIndex){

while( (leftIndex<=rightIndex) && (c.isLessThanOrEqualTo(S.atRank(leftIndex).element(), pivot) )

leftIndex++;while( (rightIndex>=leftIndex) &&

(c.isGreaterThanOrEqualTo(S.atRank(rightIndex).element(), pivot) )rightIndex--;

if (leftIndex < rightIndex)

S.swap(S.atRank(leftIndex),S.atRank(rightIndex));}S.swap(S.atRank(leftIndex),S.atRank(rightBound));quicksort(S, c, leftBound, leftIndex-1);quicksort(S, c, leftIndex+1, rightBound);

}}

Page 13: SEQUENCES

Partitioning Example

partitioning around pivot 50

85 24 63 45 17 31 96 5031 24 63 45 17 85 96 5031 24 17 45 63 85 96 5031 24 17 45 50 85 96 63

Page 14: SEQUENCES

Quick Sort Example

new pivot is selected each line

85 24 63 45 17 31 96 5031 24 17 45 50 85 96 6331 24 17 45 50 85 96 6317 24 31 45 50 85 96 6317 24 31 45 50 85 96 6317 24 31 45 50 63 96 8517 24 31 45 50 63 85 96

Page 15: SEQUENCES

Quick Sort TC

time complexity depends on pivot choice

average O(n lg n)worst O(n2)

Page 16: SEQUENCES

Heaps

binary treekeys satisfy heap property

key of every node is greater than or equal to key of its children

implements priority queue

Page 17: SEQUENCES

Implicit Representation of Heaps

array A[0...k-1] where k is max. no. of elements heap will contain

let n=current no. of elements in heapA[0...n-1] is of interest at any momentroot is stored at A[0]children of any node A[i] are at

A[2i+1] and A[2i+2]

Page 18: SEQUENCES

Heapsort

Algorithm Heapsort(A,n) Input: A (an array in the range 1 to n)Output:A (the array in sorted order) Build_Heap(A) for i n to 2 do

swap (A[1],A[i]) Rearrange_Heap(i-1)

Page 19: SEQUENCES

Example of Building Heap (Bottom up)

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 162 6 8 5 10 9 12 1 15 7 3 13 4 11 16 142 6 8 5 10 9 12 14 15 7 3 13 4 11 16 12 6 8 5 10 9 16 14 15 7 3 13 14 11 12 12 6 8 5 10 13 16 14 15 7 3 9 4 11 12 12 6 8 5 10 13 16 14 15 7 3 9 4 11 12 12 6 8 15 10 13 16 14 5 7 3 9 4 11 12 12 6 16 15 10 13 12 14 5 7 3 9 4 11 8 12 15 16 14 10 13 12 6 5 7 3 9 4 11 8 116 15 13 14 10 9 12 6 5 7 3 2 4 11 8 1

Page 20: SEQUENCES

Heapsort Example1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 1616 15 13 14 10 9 12 6 5 7 3 2 4 11 8 115 14 13 6 10 9 12 1 5 7 3 2 4 11 8 1614 10 13 6 8 9 12 1 5 7 3 2 4 11 15 1613 10 12 6 8 9 11 1 5 7 3 2 4 14 15 1612 10 11 6 8 9 4 1 5 7 3 2 13 14 15 1611 10 9 6 8 2 4 1 5 7 3 12 13 14 15 1610 8 9 6 7 2 4 1 5 3 11 12 13 14 15 169 8 4 6 7 2 3 1 5 10 11 12 13 14 15 168 7 4 6 5 2 3 1 9 10 11 12 13 14 15 167 6 4 1 5 2 3 8 6 10 11 12 13 14 15 166 5 4 1 3 2 7 8 9 10 11 12 13 14 15 165 3 4 1 2 6 7 8 9 10 11 12 13 14 15 164 3 2 1 5 6 7 8 9 10 11 12 13 14 15 163 1 2 4 5 6 7 8 9 10 11 12 13 14 15 162 1 3 4 5 6 7 8 9 10 11 12 13 14 15 161 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

Page 21: SEQUENCES

O(n lg n) Algorithms

Algorithm Time Space

Merge O(n lg n) Not in-place

Quick O(n lg n) ave.O(n2) worst

in-place

Heap O(n lg n) In-place

Page 22: SEQUENCES

Big Omega Notation

“lower bound”Let f(n) and g(n) be functions

mapping integers to real numbers. Then:

f(n) is Ω (g(n)) if g(n) is O(f(n)), i.e., if there exist real constants c>0 and n0>=1 such that f(n) >= cg(n) for n>=n0

Page 23: SEQUENCES

Lower Bound for Sorting

any comparison-based sorting algorithm requires (n log n)

sorting can be seen as permutation problem determine a permutation of indices i1, i2,

…, in such that a[i1]<=a[i2]<=…<=a[in] n! possible permutations

comparison can eliminate up to 1/2 of search space ==> log(n!) comparisons required

log(n!)= (n log n) [by Stirling’s formula]

Page 24: SEQUENCES

Bucket Sortinput restriction: sequence S of n

items with keys in the range [0,N-1]

can sort S in O(n+N) timeif N is O(n), then sorting time is

O(n)note: NOT based on comparison

Page 25: SEQUENCES

Pseudocode

Algorithm BucketSortInput: Sequence S with integer keys in range [0,N-1]Output: S sortedlet B=array of N initially empty sequencesfor each item x in S do

k=key of xremove x from S and insert at end of sequence B[k]

for i 0 to N-1 dofor each item x in sequence B[i] do

remove x from B[i] and insert at end of S

Page 26: SEQUENCES

Radix Sort

generalization of bucket sortuses several passes of bucket sort

(digit by digit)two versions:

radix-exchange sort - “left-to-right” radix sort (most significant digit first)

straight radix sort - “right-to-left” radix sort (least significant digit first)

Page 27: SEQUENCES

Radix Exchange Sort Example

A 00001S 10011O 01111R 10010T 10100I 01001N 01110G 00111E 00101X 11000A 00001M 01101P 10000L 01100E 00101

A 00001E 00101O 01111L 01100M 01101I 01001N 01110G 00111E 00101A 00001X 11000T 10100P 10000R 10010S 10011

A 00001E 00101A 00001E 00101G 00111I 01001N 01110M 01101L 01100O 01111S 10011T 10100P 10000R 10010X 11000

A 00001A 00001E 00101E 00101G 00111I 01001N 01110M 01101L 01100O 01111S 10011R 10010P 10000T 10100

A 00001A 00001E 00101E 00101G 00111

L 01100M 01101N 01110O 01111P 10000R 10010S 10011

A 00001A 00001E 00101E 00101

L 01100M 01101N 01110O 01111

R 10010S 10011

A 00001A 00001E 00101E 00101G 00111I 01001L 01100M 01101N 01110O 01111P 10000R 10010S 10011T 10100X 11000

Page 28: SEQUENCES

Straight Radix Sort Example

A 00001S 10011O 01111R 10010T 10100I 01001N 01110G 00111E 00101X 11000A 00001M 01101P 10000L 01100E 00101

R 10010T 10100N 01110X 11000P 10000L 01100A 00001S 10011O 01111I 01001G 00111E 00101A 00001M 01101E 00101

T 10100X 11000P 10000L 01100A 00001I 01001E 00101A 00001M 01101E 00101R 10010N 01110S 10011O 01111G 00111

X 11000P 10000A 00001I 01001A 00001R 10010S 10011T 10100L 01100E 00101M 01101E 00101N 01110O 01111G 00111

P 10000A 00001A 00001R 10010S 10011T 10100E 00101E 00101G 00111X 11000I 01001L 01100M 01101N 01110O 01111

A 00001A 00001E 00101E 00101G 00111I 01001L 01100M 01101N 01110O 01111P 10000R 10010S 10011T 10100X 11000

Page 29: SEQUENCES

Algorithm StraightRadixSortInput: array S of integers, each with k digitsOutput: S sorted//assume that all items are initially in a global queue GQ

for i 0 to d-1 do // d is is number of possible digits; d=10 if decimalinitialize Q[i] to be empty

for i k down to 1 dowhile GQ is not empty do

delete x from GQd ith digit of xinsert x into Q[d]

for t 0 to d-1 doinsert Q[t] into GQ

for i 0 to n-1 dodelete S[i] from GQ

Page 30: SEQUENCES

Straight Radix Sort Example

GQ: 20131015301328614725686112313254

Q[1]:286168611231

Q[3]:20133013

Q[4]:3254Q[5]:1015

4725

Q[1]:201330131015

Q[2]:4725Q[3]:1231Q[5]: 3254Q[6]:2861

6861

Q[0]:201330131015

Q[2]:12313254

Q[7]:4725Q[8]:2861

6861

Q[1]:10151231

Q[2]:20132861

Q[3]:30133254

Q[4]:4725Q[6]:6861