# help me with this programme

• 07-01-2008
ChrisWang
help me with this programme
i write this programme to find the Kth number in the vector a
the algorithms just like the quick sort

but it doesnot work right:(

Code:

void quickSelect(vector<int> & a, int left=0, int right=a.size(), int k) {

int middle = (left + right) / 2;
Comparable pivot = a[middle];
int i = left, j = right;
for (;;) {
while (a[i] < pivot) {
i = i + 1;
}
while (a[j] > pivot) {
j = j - 1;
}

if (i < j) {
swap(a[i], a[j]);
i = i + 1;
j = j - 1;
} else
break;
}
a[i+1] = pivot;

if (k <= i)
quickSelect(a, left, i - 1, k);
else if (k > i + 1)
quickSelect(a, i + 1, right, k - i);
else
cout << "the " << k << "th number is " << pivot << endl;
}

• 07-01-2008
cyberfish
1. Write a more descriptive subject next time (your subject fits pretty much all threads here)
2. Need more detail. What do you mean it doesn't work? doesn't compile? (post compiler erros) doesn't run? (what happens) doesn't give you the result you expect? (provide the input, output, and expected output)