21
CH08-320201: Algorithms and Data Structures 212 Visualization and Computer Graphics Lab Jacobs University Asymptotic analysis Use Counting Sort as stable sorting algorithm. • Sort n computer words of b bits each. Each word can be viewed as having b/r base-2 r digits. Example: 32-bit word. r = 8: d = b/r = 4 passes of counting sort on base-2 8 digits; r = 16: d = b/r = 2 passes of counting sort on base-2 16 digits. How many passes should we make?

320201 AlgDS Spring 2016 lecture notes - Jacobs University

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: 320201 AlgDS Spring 2016 lecture notes - Jacobs University

CH08-320201: Algorithms and Data Structures 212

Visualization and Computer Graphics LabJacobs University

Asymptotic analysis

• Use Counting Sort as stable sorting algorithm.• Sort n computer words of b bits each.• Each word can be viewed as having b/r base-2r digits.• Example: 32-bit word.

r = 8: d = b/r = 4 passes of counting sort on base-28 digits;

r = 16: d = b/r = 2 passes of counting sort on base-216 digits.

• How many passes should we make?

Page 2: 320201 AlgDS Spring 2016 lecture notes - Jacobs University

CH08-320201: Algorithms and Data Structures 213

Visualization and Computer Graphics LabJacobs University

Choosing r

• Counting Sort takes Θ(n + k) time to sort n numbers in the range from 0 to k –1.

• If each b-bit word is broken into r-bit pieces, each pass of Counting Sort takes Θ(n + 2r) time.

• Since there are b/r passes, we have:

• Choose r to minimize T(n,b).

Page 3: 320201 AlgDS Spring 2016 lecture notes - Jacobs University

CH08-320201: Algorithms and Data Structures 214

Visualization and Computer Graphics LabJacobs University

Choosing r

• Increasing r means fewer passes, but when r >> lg nthe time grows exponentially.

• We do not want 2r > n, but there is no harm asymptotically in choosing r as large as possible subject to this constraint.

• Choosing r = lgn implies T(n,b) = Θ(bn /lgn).• For numbers in the range from 0 to nd–1,

we have b = dr = d lgn, i.e., Radix Sort runs in Θ(dn) time.

Page 4: 320201 AlgDS Spring 2016 lecture notes - Jacobs University

CH08-320201: Algorithms and Data Structures 215

Visualization and Computer Graphics LabJacobs University

Conclusion

• In practice, Radix Sort is fast for large inputs, as well as simple to code and maintain.

• Example (32-bit numbers, i.e., b=32, and n=2000):At most d = 3 passes when sorting 2000 numbers.Merge Sort and Quicksort do at least ceiling (lg 2000) = 11 passes.

Page 5: 320201 AlgDS Spring 2016 lecture notes - Jacobs University

CH08-320201: Algorithms and Data Structures 216

Visualization and Computer Graphics LabJacobs University

2.6 Bucket Sort

Page 6: 320201 AlgDS Spring 2016 lecture notes - Jacobs University

CH08-320201: Algorithms and Data Structures 217

Visualization and Computer Graphics LabJacobs University

Motivation• Can we use the idea of Radix Sort to sort any

numbers, i.e., without assuming them to be integers?• In order to do this efficiently, we make a new

assumption:The to-be-sorted elements shall distribute uniformly and independently over the interval [0,1).

• Remark: Interval [0,1) is not a real restriction, as we can normalize the elements to this interval in linear time.However, uniform distribution and independence are restrictions and we will see that we need this to assure good expected running time.

Page 7: 320201 AlgDS Spring 2016 lecture notes - Jacobs University

CH08-320201: Algorithms and Data Structures 218

Visualization and Computer Graphics LabJacobs University

Idea

• Assuming that we have to sort n numbers, we split the interval [0,1) into n subintervals or buckets.

• Then, we can distribute the n numbers to the nbuckets.

• Assuming uniform distribution, we can conclude that we have only few numbers falling into each bucket.

Page 8: 320201 AlgDS Spring 2016 lecture notes - Jacobs University

CH08-320201: Algorithms and Data Structures 219

Visualization and Computer Graphics LabJacobs University

Example (n=10)

A[i] is put intobucket

Page 9: 320201 AlgDS Spring 2016 lecture notes - Jacobs University

CH08-320201: Algorithms and Data Structures 220

Visualization and Computer Graphics LabJacobs University

Bucket Sort

Page 10: 320201 AlgDS Spring 2016 lecture notes - Jacobs University

CH08-320201: Algorithms and Data Structures 221

Visualization and Computer Graphics LabJacobs University

Time complexity

Time complexity:

where ni denotes the number of elements in bucket i.

Page 11: 320201 AlgDS Spring 2016 lecture notes - Jacobs University

CH08-320201: Algorithms and Data Structures 222

Visualization and Computer Graphics LabJacobs University

Expected time complexity

What is E [ni2]?

Let Xij be the event that A[j] falls into bucket i.Then,

Use assumptions of uniform distribution and independence.

Page 12: 320201 AlgDS Spring 2016 lecture notes - Jacobs University

CH08-320201: Algorithms and Data Structures 223

Visualization and Computer Graphics LabJacobs University

Estimate E [ni2]

Page 13: 320201 AlgDS Spring 2016 lecture notes - Jacobs University

CH08-320201: Algorithms and Data Structures 224

Visualization and Computer Graphics LabJacobs University

Estimate E [ni2]

Page 14: 320201 AlgDS Spring 2016 lecture notes - Jacobs University

CH08-320201: Algorithms and Data Structures 225

Visualization and Computer Graphics LabJacobs University

Expected time complexity

Page 15: 320201 AlgDS Spring 2016 lecture notes - Jacobs University

CH08-320201: Algorithms and Data Structures 226

Visualization and Computer Graphics LabJacobs University

2.7 Searching

Page 16: 320201 AlgDS Spring 2016 lecture notes - Jacobs University

CH08-320201: Algorithms and Data Structures 227

Visualization and Computer Graphics LabJacobs University

Searching problem

• Given a sorted sequence.• Find an element in that sequence.

• Example:Sequence

Find element 9.Brute-force approach (going through the sequence from start until we find the 9) runs in O(n).

Page 17: 320201 AlgDS Spring 2016 lecture notes - Jacobs University

CH08-320201: Algorithms and Data Structures 228

Visualization and Computer Graphics LabJacobs University

Binary Search

Idea: Use a divide & conquer strategy.

1. Divide: Check middle element.

2. Conquer: Recursively search 1 subarray.

3. Combine:Nothing to be done.

Page 18: 320201 AlgDS Spring 2016 lecture notes - Jacobs University

CH08-320201: Algorithms and Data Structures 229

Visualization and Computer Graphics LabJacobs University

Example (find 9)

Page 19: 320201 AlgDS Spring 2016 lecture notes - Jacobs University

CH08-320201: Algorithms and Data Structures 230

Visualization and Computer Graphics LabJacobs University

Time complexity

T(n) = 1T(n/2) + Θ(1)a = 1, b = 2 n logba = n log21 = 1f (n) = Θ(1) Case 2: T (n) = Θ(lgn).

Page 20: 320201 AlgDS Spring 2016 lecture notes - Jacobs University

CH08-320201: Algorithms and Data Structures 231

Visualization and Computer Graphics LabJacobs University

2.8 Summary

Page 21: 320201 AlgDS Spring 2016 lecture notes - Jacobs University

CH08-320201: Algorithms and Data Structures 232

Visualization and Computer Graphics LabJacobs University

SummarySorting problem:• Comparison sorts:

– Insertion Sort: Θ(n) [best], Θ(n2) [average & worst].– MergeSort: Θ(n lgn).– Heap Sort: Θ(n lgn).

• Heap as a data structure– Quicksort: Θ(n lgn) [best & average], Θ(n2) [worst].– Decision trees: Worst case does not get better than Θ(n lgn).

• Sorting in linear time:– Counting Sort: small integers– Radix Sort: large integers– Bucket Sort: any numbers, but uniform distribution.

Searching Problem:– Linear Search: Θ(1) [best], Θ(n) [average & worst]– Binary Search: Θ(1) [best], Θ(lgn) [average & worst]