Use this tag with questions about the Quickselect algorithm, an algorithm to find the nth smallest member in a list.
Questions tagged [quickselect]
85 questions
1
vote
1 answer
quickSort algorithm using median of medians as a pivot for partitioning
there is a lot of info on StackOverflow but I couldn't exactly figure it out the way I need it.
I'm trying to implement a quickSelect algorithm with a median of medians but not exactly making it work.
the algorithm is supposed to find the i-th…

Eliram Liroy Lugassi
- 41
- 4
1
vote
0 answers
How to transform quicksort to quickselect?
I've the following implementation for quicksort, but trying to transfer it to quickselect fails.
class Solution {
public static void main(String[] args) {
for (int i = 1; i < 5; i++) {
int[] arr = new…

Regyn
- 585
- 3
- 17
1
vote
1 answer
if/elsif/else return behavior difference between Ruby and Python
I am trying to convert a quickselect from Ruby to Python. The Ruby code is as follows:
class SortableArray
attr_reader :array
def initialize(array)
@array = array
end
def quickselect!(kth_lowest_value, left_index,…

Paolo Tormon
- 312
- 2
- 11
1
vote
1 answer
Find kth largest element using quick select algorithm
#include
using namespace std;
class Solution {
public:
int partition(vector &nums, int low,int high,int pivotElement)
{
int pivotPoint=low;
for(int i=low;i<=high-1;i++)
{
…

Ruhan Saini
- 17
- 4
1
vote
1 answer
Finding median of a list using quick select and median-of-medians
Suppose A = [1, 2, 3, 4, 5, 6, 7, 8, 9]. I have to find the median here which is 5 and I am required to use the concept of quick select and median-of-medians. I have made the following code but for some reason, it outputs 4 which is wrong. Where…

mrkupidooo
- 35
- 5
1
vote
0 answers
Quickselect using a median-of-median-of-three partitioning
What is the asymptotic running time of quickselect, using a median-of-median-of-three partitioning strategy? I feel that it should be the same as normal quick select and quicksort except the fact that the worst case gets mitigated significanlty. Am…

Szr
- 29
- 5
1
vote
2 answers
Quickselect algorithm for singly linked list C++
I need an algorithm which can find the median of a singly linked list in linear time complexity O(n) and constant space complexity O(1).
EDIT: The singly linked list is a C-style singly linked list. No stl allowed (no container, no functions,…

alex cojocaru
- 15
- 5
1
vote
0 answers
My quickselect code doesnt work like it should
My algorithm for quickselect have to be faster than seq.sort + seq(k).
I think else if (low.length == 0) a(pivot) is wrong and I think my algorithm is too slow for tests.
Some results for code:
Running performance tests on almost-sorted sequences
…

S.H
- 33
- 5
1
vote
1 answer
Trying to use quickselect by hand
I have a sub array that is {8,9,7}. Assume the pivot that was picked is 8. Running Quickselect on this array is giving me some issue.
So the left pointer starts from the left looking for elements greater than 8 it finds 9. The right pointer starts…

johnnyB
- 11
- 2
1
vote
1 answer
Can QuickSelect find smallest element in an Array with duplicate values?
Does the QuickSelect algorithm work with duplicate values?
If I haven an Array
int[] array = {9, 8, 7, 6, 6, 6, 5, 0, 1, 2, 3, 4, 5, 5, 7, 200};
Will it be able to get the kth smallest element even though there are duplicates?
user7722505
1
vote
2 answers
kth smallest number - quicksort faster than quickselect
I have implemented the following quickselect algorithm to achieve O(n) complexity for median selection (more generally kth smallest number):
static size_t partition(struct point **points_ptr, size_t points_size, size_t pivot_idx)
{
const double…

Jan Wrona
- 73
- 6
1
vote
2 answers
Are duplicates of the nth element always contiguous when using std::nth_element?
vector data = {3, 1, 5, 3, 3, 8, 7, 3, 2};
std::nth_element(data.begin(), data.begin() + median, data.end());
Will this always result in:
data = {less, less, 3, 3, 3, 3, larger, larger, larger} ?
Or would a other possible outcome be:
data =…

aces
- 185
- 1
- 1
- 10
1
vote
1 answer
Median of median algorithm recurrence relation
I know that the linear select (median of medians algorithm) recurrence equation is as follows:
T(n) <= an + T(n/5) + T(7n/10)
But where do these terms come from? I've been trying to understand, but I'm extremely confused. Can anyone please shed…

Vimzy
- 1,871
- 8
- 30
- 56
1
vote
1 answer
Pivot element selection in Quick Select
Is there any significance of selecting a random pivot over last element for quick select?
I can still find the required element by always selecting the last element as pivot. Will it effect the runtime.

mc20
- 1,145
- 1
- 10
- 26
1
vote
2 answers
Partition in Quickselect
I have to implement an algorithm that returns the median of an array. So I chose to implement Quickselect which seems to be efficient for this and I saw that for the tripartition I can use the same partition algorithm used in Quicksort.
The…

LordFenerSSJ
- 195
- 1
- 2
- 10