I have a bit of homework help if you don't mind. Basically the idea is to perform a quickselect on an array of values, however we were given a template and I can't seem to figure out how to get the functions to work with what is provided.
The problem is that the values will not arrange themselves properly, or if they do they will change every time the same value is input.
Here are the functions I'm working with, I'll denote the code that I have written with a /**/ after the code and any other line is part of the template that was provided for me.
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <algorithm>
using namespace std;
int partition(int *A, int len, int pivot_index)
{
int pivot_value = A[pivot_index]; /**/
int left = 0; /**/
int l = left; /**/
int right = len - 1; /**/
while(left < right) /**/
{ /**/
while(A[left] < pivot_value) /**/
{ /**/
left++; /**/
} /**/
while(A[right] > pivot_value) /**/
{ /**/
right--; /**/
} /**/
if(A[left] < pivot_value) /**/
{ /**/
swap(A[left], A[right]); /**/
} /**/
} /**/
return left; /**/
}
int select (int *A, int len, int rank)
{
if (len==1) return A[0];
int pivot_index = rand() % len;
int pivot_rank = partition(A, len, pivot_index);
if (rank == pivot_rank) return A[rank];
if (rank < pivot_rank) return select(A, pivot_rank, rank);
return select(A, len - pivot_rank, rank - pivot_rank ); /**/
}
int main(void)
{
int N, *A;
ifstream fin("test.txt");
fin >> N;
A = new int[N];
for (int i=0; i<N; i++) fin >> A[i];
fin.close();
int r;
cout << "Enter rank (0.." << N-1 << ")\n";
while (cin >> r) {
if (r < 0 || r >= N)
cout << "Invalid rank\n";
else
cout << "The element of rank " << r << " is " << select(A,N,r) << "\n";
}
delete[] A;
}
My "test.txt" file contains the following values:
10 -- this denotes the length of the array or how many values it will read in
7
14
12
2
25
18
15
13
100
63
Any help with this would be greatly appreciated.. my professor did not explain how this would work at all and any other examples I have found online do not answer my question. I have spent numerous hours fiddling with different ways of implementing this and at this point I just have no idea. Thanks
EDIT: changed so now can directly implement and compile. Also added the libraries I'm using