|
-
Feb 8th, 2003, 12:41 AM
#1
Thread Starter
Lively Member
Finding n-th Smallest Element Using Partition List Of Quick Sort
Good day !
Need helps here.
I was told that I can search for a n-th smallest element in an array using the partition list function of quick sort, without having to sort it completely before getting the value.
For example, I got an array of 10 items. I would like to know the 3rd smallest element in the array.
I wonder how can I accomplish this ?
Thanks a lot.
SonicWave 
p/s: Below is the funciton of the partition list i am using. THanks again.
void parti(int left, int right)
{
int pivot, l_hold, r_hold;
l_hold = left;
r_hold = right;
pivot = A[left];
while (left < right)
{
while ((A[right] >= pivot) && (left < right))
right--;
if (left != right)
{
A[left] = A[right];
left++;
}
while ((A[left] <= pivot) && (left < right))
left++;
if (left != right)
{
A[right] = A[left];
right--;
}
}
A[left] = pivot;
pivot = left;
left = l_hold;
right = r_hold;
displayArray();
if (left < pivot)
parti(left, pivot-1);
if (right > pivot)
parti(pivot+1, right);
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|