Hi,
I am working on a C++ select algorithm using the qsort function.
I am suppose to first sort the array and then return the i-th smallest value, which is given by the stat value.
However, I have no idea how to return the value from a void array.
I would appreciate if someone could help me.
This is my code:
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
using namespace std;
int qsortselect(void *keys, int n, int stat, int size, int (*compare)(const void *,const void *))
{
qsort(keys,n,sizeof(char)*size,compare);
return &keys[stat*size];
}
int compare(const void *x,const void *y)
{
return(* ((int*)x)-*((int *)y));
}
void main()
{
int A[5]={3,5,67, 7, 2};
for(int i=0; i<5;i++)
{
cout << A[i] << " ";
}
int stat = qsortselect(A,5,2,sizeof(int), compare);
cout << endl << stat << endl;
getch();
}