87
Sorting in Linear Time Sorting in Linear Time Lecture 5 Asst. Prof. Dr. İlker Kocabaş

Sorting in Linear Time Lecture 5 Asst. Prof. Dr. İlker Kocabaş

Embed Size (px)

Citation preview

Page 1: Sorting in Linear Time Lecture 5 Asst. Prof. Dr. İlker Kocabaş

Sorting in Linear TimeSorting in Linear Time

Lecture 5

Asst. Prof. Dr. İlker Kocabaş

Page 2: Sorting in Linear Time Lecture 5 Asst. Prof. Dr. İlker Kocabaş

How fast can we sort?How fast can we sort?

Page 3: Sorting in Linear Time Lecture 5 Asst. Prof. Dr. İlker Kocabaş

Decision-tree exampleDecision-tree example

Page 4: Sorting in Linear Time Lecture 5 Asst. Prof. Dr. İlker Kocabaş

Decision-tree exampleDecision-tree example

Page 5: Sorting in Linear Time Lecture 5 Asst. Prof. Dr. İlker Kocabaş

Decision-tree exampleDecision-tree example

Page 6: Sorting in Linear Time Lecture 5 Asst. Prof. Dr. İlker Kocabaş

Decision-tree exampleDecision-tree example

Page 7: Sorting in Linear Time Lecture 5 Asst. Prof. Dr. İlker Kocabaş

Decision-tree exampleDecision-tree example

Page 8: Sorting in Linear Time Lecture 5 Asst. Prof. Dr. İlker Kocabaş

Decision-tree exampleDecision-tree example

Page 9: Sorting in Linear Time Lecture 5 Asst. Prof. Dr. İlker Kocabaş

Lower Bound for decision-tree Lower Bound for decision-tree sortingsorting

Page 10: Sorting in Linear Time Lecture 5 Asst. Prof. Dr. İlker Kocabaş

Lower bound for comparison Lower bound for comparison sortingsorting

Page 11: Sorting in Linear Time Lecture 5 Asst. Prof. Dr. İlker Kocabaş

Sorting in linear timeSorting in linear time

• Counting something, like histograms, can help us to sort an array. – clr = getcolor(x,y)– count[ clr ]++; Limitation: count[0..255]

Page 12: Sorting in Linear Time Lecture 5 Asst. Prof. Dr. İlker Kocabaş

Sorting in linear timeSorting in linear time

Page 13: Sorting in Linear Time Lecture 5 Asst. Prof. Dr. İlker Kocabaş

Counting sortCounting sort

COUNTING-SORT(A,B,k)

// Initialize aux. store

// Count how many times occurred.

// Cumulative sums of aux. store

// ???

Page 14: Sorting in Linear Time Lecture 5 Asst. Prof. Dr. İlker Kocabaş

Counting-sort exampleCounting-sort example

Page 15: Sorting in Linear Time Lecture 5 Asst. Prof. Dr. İlker Kocabaş

Counting-sort exampleCounting-sort example

Page 16: Sorting in Linear Time Lecture 5 Asst. Prof. Dr. İlker Kocabaş

Counting-sort exampleCounting-sort example

Page 17: Sorting in Linear Time Lecture 5 Asst. Prof. Dr. İlker Kocabaş

Counting-sort exampleCounting-sort example

Page 18: Sorting in Linear Time Lecture 5 Asst. Prof. Dr. İlker Kocabaş

Counting-sort exampleCounting-sort example

Page 19: Sorting in Linear Time Lecture 5 Asst. Prof. Dr. İlker Kocabaş

Counting-sort exampleCounting-sort example

Page 20: Sorting in Linear Time Lecture 5 Asst. Prof. Dr. İlker Kocabaş

Counting-sort exampleCounting-sort example

Page 21: Sorting in Linear Time Lecture 5 Asst. Prof. Dr. İlker Kocabaş

Counting-sort exampleCounting-sort example

Page 22: Sorting in Linear Time Lecture 5 Asst. Prof. Dr. İlker Kocabaş

Counting-sort exampleCounting-sort example

Page 23: Sorting in Linear Time Lecture 5 Asst. Prof. Dr. İlker Kocabaş

Counting-sort exampleCounting-sort example

Page 24: Sorting in Linear Time Lecture 5 Asst. Prof. Dr. İlker Kocabaş

Counting-sort exampleCounting-sort example

Page 25: Sorting in Linear Time Lecture 5 Asst. Prof. Dr. İlker Kocabaş

Counting-sort exampleCounting-sort example

Page 26: Sorting in Linear Time Lecture 5 Asst. Prof. Dr. İlker Kocabaş

Counting-sort exampleCounting-sort example

Page 27: Sorting in Linear Time Lecture 5 Asst. Prof. Dr. İlker Kocabaş

Counting-sort exampleCounting-sort example

Page 28: Sorting in Linear Time Lecture 5 Asst. Prof. Dr. İlker Kocabaş

Counting-sort exampleCounting-sort example

Page 29: Sorting in Linear Time Lecture 5 Asst. Prof. Dr. İlker Kocabaş

AnalysisAnalysis

Page 30: Sorting in Linear Time Lecture 5 Asst. Prof. Dr. İlker Kocabaş

Running TimeRunning Time

Page 31: Sorting in Linear Time Lecture 5 Asst. Prof. Dr. İlker Kocabaş

Stable sortingStable sorting

Page 32: Sorting in Linear Time Lecture 5 Asst. Prof. Dr. İlker Kocabaş

Counting sort array size Counting sort array size limitation problemlimitation problem

• If we need sort 32 bits integers, size of count array 232 = 4GB !

• Unsorted array size is usually smaller than this (1KB << 4GB)

• Solution... Use smaller parts like digits and sort these parts particularly : Radix sort

Page 33: Sorting in Linear Time Lecture 5 Asst. Prof. Dr. İlker Kocabaş

Radix sortRadix sort

Page 34: Sorting in Linear Time Lecture 5 Asst. Prof. Dr. İlker Kocabaş

Operation of radix sortOperation of radix sort

Page 35: Sorting in Linear Time Lecture 5 Asst. Prof. Dr. İlker Kocabaş

Analysis of radix sort Analysis of radix sort

• Given n d-digit number in which each digit can take on up to k-possible values, the running time of RADIX-SORT sorts these numbers is

(d*(n+k))

Page 36: Sorting in Linear Time Lecture 5 Asst. Prof. Dr. İlker Kocabaş

Analysis of radix sortAnalysis of radix sort

•Assume counting sort is the auxilary sort.•Sort n computer words of b-bits each.•Each word can be viewed as having d=b/r (base-2r) digits.

Example: Each key having d-digits of r bits. Let d=4, r=8 (b=32) then each digit is an integer in the

range 0 to 2r-1 = 28-1=255.

r=8 r=8 r=8 r=8

)}2)(/{()}2({)}({)( rr nrbndkndnT

Page 37: Sorting in Linear Time Lecture 5 Asst. Prof. Dr. İlker Kocabaş

Analysis of radix sort: Choosing Analysis of radix sort: Choosing rr

• How many passes should we make?• Each word of length b=rd bit is broken into r-bit

pieces.• For a given word length of b, the running time of an

array of length n that is T(r|n,b) can be minimized with respect to r.

Page 38: Sorting in Linear Time Lecture 5 Asst. Prof. Dr. İlker Kocabaş

Analysis of radix sort: Choosing Analysis of radix sort: Choosing rr

Page 39: Sorting in Linear Time Lecture 5 Asst. Prof. Dr. İlker Kocabaş

Analysis of radix sort: Choosing Analysis of radix sort: Choosing rr

Page 40: Sorting in Linear Time Lecture 5 Asst. Prof. Dr. İlker Kocabaş

ConclusionsConclusions

Page 41: Sorting in Linear Time Lecture 5 Asst. Prof. Dr. İlker Kocabaş

What about floating points?What about floating points?

• Integer can countable or divisible to parts.

• How can speed up sorting array of floating point numbers.

• We can group into k-parts– For numbers beetween 0.0 ≤ f ≤ 1.0– k*f have an integer part like 8.71– int(k*f) is part number.– k can be length of array.

Page 42: Sorting in Linear Time Lecture 5 Asst. Prof. Dr. İlker Kocabaş

Bucket SortBucket Sort

orderin together 1,...,1,0 lists theeconcatanet 6

sortinsertion with list sort 5

1 to0 4

list into insert 3

to1 2

1

SORT(A)-BUCKET

nBBB

iB

ni

inABiA

ni

Alengthn

do

for

do

for

Page 43: Sorting in Linear Time Lecture 5 Asst. Prof. Dr. İlker Kocabaş

Bucket SortBucket Sort

.78

.17

.39

.26

.72

.94

.21

.12

.23

.68

1

2

3

4

5

6

7

8

9

10

A

0

1

3

4

5

6

7

B

9

.17 /.12

.21

.78 /

.26 /

.68 /

.72

.94 /

.23

8

2

Page 44: Sorting in Linear Time Lecture 5 Asst. Prof. Dr. İlker Kocabaş

Bucket SortBucket Sort

Running time:

1

0

2 )()()(n

iinOnnT

Page 45: Sorting in Linear Time Lecture 5 Asst. Prof. Dr. İlker Kocabaş

Bucket SortBucket Sort

)()(

)()(

)()()(

1

0

2

1

0

2

1

0

2

n

ii

n

ii

n

ii

nEOn

nOEn

nOnEnTE

Page 46: Sorting in Linear Time Lecture 5 Asst. Prof. Dr. İlker Kocabaş

Bucket SortBucket Sort

n

jiji

ij

Xn

ijAX

1

else:0

bucket in falls ][:1Let

Page 47: Sorting in Linear Time Lecture 5 Asst. Prof. Dr. İlker Kocabaş

Bucket SortBucket Sort

n

nji

n

jknk

ikij

n

jij

n

j

n

njiik

n

jknk

ijij

n

jik

n

kij

n

jiji

XXEXE

XXXE

XXE

XEnE

11

2

1 1

2

1 1

2

1

2

Page 48: Sorting in Linear Time Lecture 5 Asst. Prof. Dr. İlker Kocabaş

Bucket SortBucket Sort

2

2

1

11

t.independen are and variables the,When

1

110

11

n

nnXXE

XXjk

n

nnXE

ikij

ikij

ij

Page 49: Sorting in Linear Time Lecture 5 Asst. Prof. Dr. İlker Kocabaş

Bucket SortBucket Sort

n

nnn

nn

nnnE

n

nji

n

jknk

n

ii

12

1)1(

1

11

2

12

1

2

Page 50: Sorting in Linear Time Lecture 5 Asst. Prof. Dr. İlker Kocabaş

Bucket SortBucket Sort

)(

)/12()()(

n

nOnnnT

1

0

2 )()()(n

iinOnnT

Page 51: Sorting in Linear Time Lecture 5 Asst. Prof. Dr. İlker Kocabaş

Order StatisticsOrder Statistics

Page 52: Sorting in Linear Time Lecture 5 Asst. Prof. Dr. İlker Kocabaş
Page 53: Sorting in Linear Time Lecture 5 Asst. Prof. Dr. İlker Kocabaş
Page 54: Sorting in Linear Time Lecture 5 Asst. Prof. Dr. İlker Kocabaş
Page 55: Sorting in Linear Time Lecture 5 Asst. Prof. Dr. İlker Kocabaş
Page 56: Sorting in Linear Time Lecture 5 Asst. Prof. Dr. İlker Kocabaş
Page 57: Sorting in Linear Time Lecture 5 Asst. Prof. Dr. İlker Kocabaş
Page 58: Sorting in Linear Time Lecture 5 Asst. Prof. Dr. İlker Kocabaş
Page 59: Sorting in Linear Time Lecture 5 Asst. Prof. Dr. İlker Kocabaş
Page 60: Sorting in Linear Time Lecture 5 Asst. Prof. Dr. İlker Kocabaş
Page 61: Sorting in Linear Time Lecture 5 Asst. Prof. Dr. İlker Kocabaş
Page 62: Sorting in Linear Time Lecture 5 Asst. Prof. Dr. İlker Kocabaş
Page 63: Sorting in Linear Time Lecture 5 Asst. Prof. Dr. İlker Kocabaş

Hairy recurrence (cont.)Hairy recurrence (cont.)

8

3

48

3

2)1

2(

2

)1(

2

)1(

2)1

2(

2

21

2/

1

2/

1

2/

12/

1

1

1

1

0

n

nnnnnnk

nnk

nn

kkkk

n

nk

n

nk

n

nk

n

k

n

k

n

k

Page 64: Sorting in Linear Time Lecture 5 Asst. Prof. Dr. İlker Kocabaş
Page 65: Sorting in Linear Time Lecture 5 Asst. Prof. Dr. İlker Kocabaş
Page 66: Sorting in Linear Time Lecture 5 Asst. Prof. Dr. İlker Kocabaş
Page 67: Sorting in Linear Time Lecture 5 Asst. Prof. Dr. İlker Kocabaş
Page 68: Sorting in Linear Time Lecture 5 Asst. Prof. Dr. İlker Kocabaş
Page 69: Sorting in Linear Time Lecture 5 Asst. Prof. Dr. İlker Kocabaş
Page 70: Sorting in Linear Time Lecture 5 Asst. Prof. Dr. İlker Kocabaş
Page 71: Sorting in Linear Time Lecture 5 Asst. Prof. Dr. İlker Kocabaş
Page 72: Sorting in Linear Time Lecture 5 Asst. Prof. Dr. İlker Kocabaş
Page 73: Sorting in Linear Time Lecture 5 Asst. Prof. Dr. İlker Kocabaş
Page 74: Sorting in Linear Time Lecture 5 Asst. Prof. Dr. İlker Kocabaş
Page 75: Sorting in Linear Time Lecture 5 Asst. Prof. Dr. İlker Kocabaş
Page 76: Sorting in Linear Time Lecture 5 Asst. Prof. Dr. İlker Kocabaş
Page 77: Sorting in Linear Time Lecture 5 Asst. Prof. Dr. İlker Kocabaş
Page 78: Sorting in Linear Time Lecture 5 Asst. Prof. Dr. İlker Kocabaş
Page 79: Sorting in Linear Time Lecture 5 Asst. Prof. Dr. İlker Kocabaş
Page 80: Sorting in Linear Time Lecture 5 Asst. Prof. Dr. İlker Kocabaş
Page 81: Sorting in Linear Time Lecture 5 Asst. Prof. Dr. İlker Kocabaş
Page 82: Sorting in Linear Time Lecture 5 Asst. Prof. Dr. İlker Kocabaş
Page 83: Sorting in Linear Time Lecture 5 Asst. Prof. Dr. İlker Kocabaş
Page 84: Sorting in Linear Time Lecture 5 Asst. Prof. Dr. İlker Kocabaş
Page 85: Sorting in Linear Time Lecture 5 Asst. Prof. Dr. İlker Kocabaş
Page 86: Sorting in Linear Time Lecture 5 Asst. Prof. Dr. İlker Kocabaş
Page 87: Sorting in Linear Time Lecture 5 Asst. Prof. Dr. İlker Kocabaş