Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7778861
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T18:30:16+00:00 2026-06-01T18:30:16+00:00

Suppose I had an unsorted array A of size n. How to find the

  • 0

Suppose I had an unsorted array A of size n.

How to find the n/2, n/2−1, n/2+1th smallest element from the original unsorted list in linear time?

I tried to use the selection algorithm in wikipedia (Partition-based general selection algorithm is what I am implementing).

function partition(list, left, right, pivotIndex)
 pivotValue := list[pivotIndex]
 swap list[pivotIndex] and list[right]  // Move pivot to end
 storeIndex := left
 for i from left to right-1 
     if list[i] < pivotValue
         swap list[storeIndex] and list[i]
         increment storeIndex
 swap list[right] and list[storeIndex]  // Move pivot to its final place
 return storeIndex


function select(list, left, right, k)
 if left = right // If the list contains only one element
     return list[left]  // Return that element
 select pivotIndex between left and right //What value of  pivotIndex shud i select??????????
 pivotNewIndex := partition(list, left, right, pivotIndex)
 pivotDist := pivotNewIndex - left + 1 
 // The pivot is in its final sorted position, 
 // so pivotDist reflects its 1-based position if list were sorted
 if pivotDist = k 
     return list[pivotNewIndex]
 else if k < pivotDist 
     return select(list, left, pivotNewIndex - 1, k)
 else
     return select(list, pivotNewIndex + 1, right, k - pivotDist)

But I have not understood 3 or 4 steps. I have following doubts:

  1. Did I pick the correct algorithm and will it really work in linear time for my program. I am bit confused as it resembles like quick sort.
  2. While Calling the Function Select from the main function, what will be the value of left, right and k. Consider my array is list [1…N].
  3. Do I have to call select function three times, one time for finding n/2th smallest, another time for finding n/2+1 th smallest and one more time for n/2-1th smallest, or can it be done on a single call, if yes, how?
  4. Also in function select (third step) “select pivotIndex between left and right”, what value of pivotIndex should I select for my program/purpose.

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T18:30:18+00:00Added an answer on June 1, 2026 at 6:30 pm

    It is like quicksort, but it’s linear because in quicksort, you then need to handle both the left and right side of the pivot, while in quickselect, you only handle one side.

    The initial call should be Select(A, 0, N, (N-1)/2) if N is odd; you’ll need to decide exactly what you want to do if N is even.

    To find the median and left/right, you probably want to call it to find the median, and then just do the max of the components of the array to the left and the min of the components to the right, because you know once the median selection phase is done that all elements to the left of the median will be less than it and to the right will be greater (or equal). This is O(n) + n/2 + n/2 = O(n) total time.

    There are lots of ways to choose pivot indices. For casual purposes, either the middle element or a random index will probably suffice.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose i had list as below list_fields = ['Full-Time', 'Options:', 'Express your interest', 'Email
Suppose I'm making a JDBC call and I had fetched data from a database
Suppose I had two brushes. One that was a linear gradient brush that was
Ok, suppose I had a body element in my stylesheet and I set visibility:hidden;
I am trying to validate a list of object on Struts2. Suppose you had
I would like to find the combinations of an array of size 5 that
Suppose I had a list of ID associated with an entity stored in a
Suppose I had a query select name from tbl where dept = 123 which
Suppose I had a string for example: >>> stri = "日本" >>> res =
Suppose you had this: def wipeProduct(hash, nameToDelete) hash.each do |i| key = i[0] productName

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.