{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

cse101_10_17_11

# cse101_10_17_11 - Median finding 1 Median of a set of...

This preview shows pages 1–2. Sign up to view the full content.

Median finding 1. Median of a set of numbers a. Given n numbers, find their median Ex. 10 1 60 8 15 10 21 7 3 38 72 Simple algorithm: Sort the numbers Return the one in the middle O(n log n) 1, 3, 7, 8, 10, 10 , 15, 21, 38, 60, 72 If even number, hen anything in the middle of the 2 middle numbers can be the median b. We’ll give a fast algorithm for a more general problem Selection – given: a list of n numbersk S integer k, return: the kth smallest number in S Ex: k = 1, minimum element k(ceiling n/2) k = ceiling (0.9n) 2. A divide-and-conquer scheme for selection a. Ex. S = 10, 1, 60, 8, 15, 10, 21, 7, 3, 38, 72 k = 6 (median) b. Pick an element v from the list S. Split S into three lists. This takes linear time. S c : elements less than v S = :the elements equal to v S > : the elements greater than v Ex. V = 7. S < : 1 3 S = : 7 S > : 10, 60, 8, 15, 10, 21, 38, 72 Median must be 3 rd smallest element in S > since the size of the other lists are 2 and 1 and the median is the 6 th slot. c. Now recurse: Select(S,k) = Select(S c , k) if k < abs(S < ) V if abs(S < ) < k < abs(S c ) + abs(S = ) Select (S > , k – abs(S c ) – abs(S = )) if k > abs(S c ) + S = ) Ex. V = 21: S < : 10, 1, 8, 15, 10, 7, 3 Rest don’t’ matter because you know that it is the 6

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern