Let S be a set of n distinct positive integers, where n is odd.  The median of S is the (n+1)/2th smallest number of this set - i.e., the number in the middle of the set once the set is sorted. For example, if S = [70, 10, 20, 60, 30], then the sorted set is [10, 20, 30, 60, 70], from which we see that the median is 30. There is an obvious linearithmic algorithm to determine the median of S.  First you sort the set, which takes O(n log n) time.  Then you output the value of S[(n+1)/2}, which takes O(1) time.  But is this the best we can do? Here is a bold claim: there is an O(n) algorithm to determine the median of set S. Determine whether the above claim is TRUE or FALSE.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Let S be a set of n distinct positive integers, where n is odd. 

The median of S is the (n+1)/2th smallest number of this set - i.e., the number in the middle of the set once the set is sorted.

For example, if S = [70, 10, 20, 60, 30], then the sorted set is [10, 20, 30, 60, 70], from which we see that the median is 30.

There is an obvious linearithmic algorithm to determine the median of S.  First you sort the set, which takes O(n log n) time.  Then you output the value of S[(n+1)/2}, which takes O(1) time.  But is this the best we can do?

Here is a bold claim: there is an O(n) algorithm to determine the median of set S.

Determine whether the above claim is TRUE or FALSE.

Expert Solution
Step 1

The claim is TRUE.

Quick Sort is the algorithm to determine the median of set S. It is used as a sorting algorithm to sort the set S in first step. In the Quick Sort algorithm, a pivot element is being picked first and then move the pivot element to its correct position, as per the sorting condition, and then the partition of the set is done around it.

The idea in this procedure is that the quick sort will be stopped at the point where the pivot element itself is that particular nth smallest element and hence the quicksort will not be completed. There will be recurrence of only one side of the pivot element and not both sides, as per the position of pivot element, hence the time will be consumed comparatively less.

The time complexity, in the worst case scenario of this algorithm is O(n2), though the average case scenario is O(n) on an average. As in the worst case, a randomized function tries to pick a corner element always.

trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Topological Sort
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education