Loading...

PPT – Further Discussion on Sorting PowerPoint presentation | free to download - id: 71e04d-MGU3Y

The Adobe Flash plugin is needed to view this content

Further Discussion on Sorting

- Algorithm Design Analysis
- 9

In the last class

- Heap Structure and Patial Order Tree Property
- The Strategy of Heapsort
- Keep the Partial Order Tree Property after the

maximal element is removed - Constructing the Heap
- Complexity of Heapsort
- Accelerated Heapsort

Further Discussion on Sorting

- Analysis of Accelerated heapSort
- Analysis by Recursion Tree
- Analysis by Guess and Inductive Proof
- Counting Sort
- Shell Sort
- Radix Sort

Filtering Down and Bubbling Up

Vacant filtering down only half-way

90

90

65

65

80

If the element is smaller, filtering down

half-half-way

80

75

75

70

35

70

35

25

60

60

25

For K55

45

50

50

45

15

40

40

15

The bubbling up will not beyond last vacStop

Depth Bounded Filtering Down

- int promote(Element E, int hStop, int vacant,

int h) - int vacStop
- if (h?hStop) vacStopvacant
- else if (E2vacant.key?E2vacant1.key)
- EvacantE2vacant1
- vacStoppromote(E, hStop, 2vacan1,

h-1) - else
- EvacantE2vacant
- vacStoppromote(E, hStop, 2vacant, h-1)
- return vacStop

Depth Bound

A One-Comparison-per-Level Fixing

- Bubble-Up Heap Algorithm
- void bubbleUpHeap(Element E, int root, Element

K, int vacant) - if (vacantroot) EvancantK
- else
- int parentvacant/2
- if (K.key?Eparent.key)
- EvacantK
- else
- EvacantEparent
- bubbleUpHeap(E,root,K,parent)

Bubbling up from vacant through to the root,

recursively

fixHeap Using Divide-and-Conquer

- void fixHeapFast(Element E, Element K, int

vacant, int h) //h?lg(n1)/2? in uppermost call - if (h?1) Process heap of height 0 or 1
- else
- int hStoph/2
- int vacStoppromote(E, hStop, vacant, h)
- int vacParentvacStop/2
- if (EvacParent.key?K.key)
- EvacStopEvacParent
- bubbleUpHeap(E, vacant, K,

vacParent) - else
- fixHeapFast(E, K, vacStop, hStop)

Number of Comparisons in the Worst Case for

Accelerated fixHeap

- Moving the vacant one level up or down need one

comparison exactly in promote or bubbleUpHeap. - In a cycle, t calls of promote and 1 call of

bubbleUpHeap are excuted at most. So, the number

of comparisons in promote and bubbleUpHeap calls

are - At most, lg(h) checks for reverse direction are

excecuted. So, the number of comparisons in a

cycle is at most hlg(h) - So, for accelerated heapSort W(n)nlgn?(nloglogn

)

Recursion Equation of Accelerated heapSort

- The recurrence equation about h, which is about

lg(n1) - Assuming T(h)?h, then

Solving the Recurrence Equation by Recursive Tree

For sorting a sequence of size n, n cycles of

fixHeap are executed, so n.(h ?lg(h1)?)

T(n)

?h/2?1

T(?n/2?)

?h/4?1

T(?n/4?)

?h/8?1

?lg(h1)? levels

So, the total cost is h ?lg(h1)?

T(1)

11

Inductive Proof

- The recurrence equation for fixHeapFast
- Proving the following solution by induction
- T(h) h?lg(h1)?
- According to the recurrence equation
- T(h1)?(h1)/2?1T(?(h1)/2?)
- Applying the inductive assumption to the last

term - T(h1)?(h1)/2?1?(h1)/2? ?lg(?(h1)/2?1)?
- (It can be proved that for any positive integer
- ?lg(?(h)/2?1)?1 ?lg(h1)? )

The sum is h1

Sorting by Counting

......

......

......

The array to be sorted

xn-1

xn

xi

xi-1

x1

x2

xj

If we know that for all i, 0 ? xi ? k and If we

know that there are exacly t elements

which are less than xj

should be here in output

......

......

......

xj

Output

t elements

Counting the elements with less keys

1

n

......

......

......

xn-1

xn

xi

xi-1

A input

x1

x2

xj

1

n

......

......

......

B output

same elements as in A, in nondecreasing order

0

k

......

......

C counter

c0

c1

ci

ck

ci1

Use of the Counter

0

k

......

......

C counter

c0

c1

ci

ck

ci1

Phase 1 ci accumulating the number of elements

with keys equal to i. Phase 2 the value of

each ci changed into the number of elements

with keys equal to or less than i. Phase 3 ci

controling the insertion of each ai into the

right location in B. Note ci going down

during phase 3, to deal with the situation in

which multiple elements with same keys.

Use of the Counter an Example

Input array A

To be inserted from the right

Counter C after phase 1

adding one by one

Counter C after phase 2

7 minus 1 after the insertion, so next 3 will

be here (stable).

Output array B, (to be filled)

Counting Sort the Procedure

- countingSort(Element A,B int n, k)
- Element C int i,j
- i0
- while (i?k) Ci0
- j1
- while (i?n) CAjCAj1
- i1
- while (i?k) CiCiCi-1
- jn
- while (i?1)
- BCAjAj
- CAjCAj-1
- return

Phase 1

Phase 2

Phase 3

Sorting in Linear Time

- void countingSort(Element A,B int n, k)
- Element C int i,j
- i0
- while (i?k) Ci0
- j1
- while (i?n) CAjCAj1
- i1
- while (i?k) CiCiCi-1
- jn
- while (i?1)
- BCAjAj
- CAjCAj-1
- return

Note Array access is used as time measure,

instead of comparison.

in ?(k)

in ?(n)

in ?(k)

In totaling ?(kn), which is ?(n) since k is

usually in O(n).

in ?(n)

Punched Card Something in Meseum

ABCDEFG

80 columms

( a d-digit number occupies d columns)

12 places

hole by which card deck is sorted, looking at one

column at a time.

Radix Sort

Starting from the least significant digit avoids

intermediate piles of cards that must be kept

track of.

48001 48081 38107 53202 65215 90283 90287 65315 97

342 78397 90583 81664 00972 41983

00972 38107 41983 48001 48081 53202 65215 65315 78

397 81664 90283 90287 90583 97342

90283 90287 90583 00972 81664 41983 53202 65215 65

315 97342 48001 48081 38107 78397

00972 38107 41983 48001 48081 53202 65215 65315 78

397 81664 90283 90287 90583 97342

48081 97342 90287 90583 53202 65215 78397 48001 00

972 65315 41983 90283 81664 38107

48081 48001 97342 53202 00972 90583 41983 90283 81

664 65215 65315 90287 78397 38107

48001 53202 38107 65215 65315 97342 81664 00972 48

081 90583 41983 90283 90287 78397

The algorithm used for each digit must be stable!

Complexity of Radix Sort

- Given n d-digit numbers in which each digit can

take on up to k possible values, radix sort

correctly sorts these numbers in ?(d(nk)) time. - Correctness induction on the column being

sorted. (Stability of intermediate algorithm is

necessary.) - Time using counting sort as the intermediate

algorithm, it takes time ?(nk) to process each

digit in the d-digit number.

Radix Generalized

- We can choose a more flexible radix to break

each key into digits. - Example
- To sort an array of 32-bit word, we look at each

word as a 4-digit number with one digit

corresponding 8 bits(i.e. a digit can take up to

255 possible values) - Given n b-bit numbers and any positive integer

r?b, radix sort correctly sorts these numbers in

?((b/r)(n2r)) time. - To minimizing ?((b/r)(n2r))
- If blt?lgn?, let r b, which results a running

time in ?(n) - If b??lgn?, when we decrease r below ?lgn?, the

n2r) term remain at ?(n), even if the b/r term

increases.

Bucket Sort the idea

- If cf(n) is in higher order than ?(n), then the

sum of k cf(n/k) will be less than c. - If the input is generated by a random process

that distributes elements uniformly over the

interval 0,1), then we can divide the interval

into n equal-sized subintervals, which is called

bucket.

Bucket Sort the Process

Array to be sorted

- The general procedure of bucket sorts
- Distributing keys
- Sorting buckets individually
- Combining buckets

One bucket for a specific value of key property

No.1

No.2

No.

n

Bucket Sort the Procedure

- void bucketSort(Element E, int n)
- int i1 int j0 Bucket B
- while (i?n)
- Inserting Ai into B?nAi?
- i
- while (j?n-1)
- Sort Bj with select algorithm of sort
- j
- Combining B0, B1,..., Bn-1 in order
- return

in O(n) in worst

To be considered

Analysis of Bucket Sorts

- Time complexity
- Distribution reasonably should be in ?(n)
- Sorting in Buckets
- Even distribution ck(n/k)lg(n/k)cnlg(n/k), for

seleted k, this may be in ?(n) - Worst case all elements in one bucket
- Combining reasonably should be in ?(n)
- As the number of buckets k inceases, so does the

speed, but so also does the amount of space used.

Using Insertion to Sort the Bucket

Home Assignments

- pp.215 -
- 4.46
- 4-54
- 4-60
- 4-66