Download presentation
Presentation is loading. Please wait.
Published byἸοκάστη Αλεξιάδης Modified over 5 years ago
1
Algorithms CSCI 235, Spring 2019 Lecture 19 Order Statistics
2
Bucket Sort Bucket sort is like counting-sort except it can be used when the keys are not integers. If the keys are relatively evenly distributed, we can use a general bucket sort. Idea: 1) If you have n elements in a given range, sort them into buckets that divide this range into portions of width 1/n. Do this by first creating an array of n buckets, each containing an empty list. 2) Insert each element into the list associated with the bucket whose range matches that element's value. 3) Sort each of the lists in the buckets. 4) Read elements from the lists in order back into the input array.
3
Example A 0.78 0.17 0.39 0.26 0.72 0.94 0.21 0.12 0.23
4
Analyzing bucket sort Is bucket-sort stable? Is it in place?
Worst case running time of Bucket sort: Best case running time: Average case:
5
Order Statistics Suppose we want to find the ith smallest (or largest) item in an array. Can we find it in less than Q(nlgn) time? Definitions: If S is a set of n distinct elements, the ith order statistic is the ith smallest element. (i.e. it is the element that is larger than i-1 elements in the set). The ith order statistic has rank i. The minimum of the set = the 1st order statistic (rank = 1) The maximum of the set = the nth order statistic (rank = n)
6
The Median The median of S is the element in the middle of the set (the "halfway point"). If n is odd, the median is rank (n+1)/2. E.g. S = {5, 3, 1, 9, 7}; Median is? If n is even, we have two medians: n/2 and (n/2 + 1) E.g. S = {5, 3, 1, 9, 7, 11}; Medians are? Therefore, the median(s) of S is (are) the element(s) with rank: Example: B = {43, 5, 17, 91, 2, 42, 19, 72, 37, 3} BORDERED = {2, 3, 5, 17, 19, 37, 42, 43, 72, 91} Minimum = ? Maximum = ? Median(s) = ? Rank of 17 = ?
7
The Selection Problem Write an algorithm, Select(A,i), that returns the ith order statistic from an array of n distinct elements. Obvious solution: What is the lowest running time for this? Can we do better than this?
8
Finding Max and Min Minimum(A) min <- A[1]
for i <- 2 to length(A) do if min > A[i] then min <- A[i] return min Maximum(A) is similar. What is the running time? Can we do better?
9
Simultaneous Max and Min
Algorithm A: 1) Find minimum (n-1) comparisons 2) Find maximum (n-1) comparisons Total: 2n - 2 comparisons. Algorithm B: Find max and min simultaneously 1) Store value of max and min seen so far 2) Compare 2 input elements with each other a) Compare the smaller of the two with the current min b) Compare the larger of the two with the current max 3) Continue with next pair of elements Algorithm B makes 3 comparisons for each pair of elements. Total comparisons = Example: A = {3, 4, 17, 12, 1, 5, 21}
10
Selection of kth order statistic
We can find the kth element by repeatedly finding the minimum and discarding it. Example: B = {43, 5, 17, 91, 2, 42, 19, 72, 37, 3} Find the 4th element: Running time: We can use a similar method for finding the (n-k)th order statistic, by working with the maximum. (Find the maximum and discard). General running time for finding kth order statistic:
11
Finding the median Note that the position of the median changes with n. As n increases, so does the median (n/2). If we apply the previous strategy: T(n) = ? Can we do better? (Tune in next time...)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.