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; }



LinkBack URL
About LinkBacks



