468,484 Members | 2,096 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,484 developers. It's quick & easy.

Quicksort doesn't sort correctly

I am attempting to write a Quicksort algorithm and my partition function is not behaving correctly.
here is the code

int CDataAnalyzerDlg::partition1(int first, int last)
{
int pivot = myVector[((first + last) / 2)];

int left = first;
int right = last;
swap(myVector[left], myVector[right]);
while (left < right)
{
while (pivot < myVector[right])
right--;

while (left < right && (myVector[left] < pivot || myVector[left] == pivot))
left++;

if (left < right)
swap(myVector[left], myVector[right]);

}

int pos = right;
myVector[first] = myVector[pos];
myVector[pos] = pivot;
return pos;
}

void CDataAnalyzerDlg::Quicksort(int first, int last)
{
int location;
if(first < last)
{
location = partition1(first, last);
Quicksort(first, location - 1);
Quicksort(location + 1,last);
}

when this runs the elements in my vector are duplicated at several points and
as I continue to run it they duplicate even more (although the number of elements remains the same). I'm using Microsoft Visual Studio .Net 2003
Oct 20 '06 #1
0 1018

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

6 posts views Thread by Exekute | last post: by
3 posts views Thread by Lieven | last post: by
36 posts views Thread by HERC777 | last post: by
2 posts views Thread by camotito | last post: by
2 posts views Thread by camotito | last post: by
8 posts views Thread by aparnakakkar2003 | last post: by
8 posts views Thread by aparnakakkar2003 | last post: by
12 posts views Thread by aparnakakkar2003 | last post: by
reply views Thread by NPC403 | last post: by
2 posts views Thread by gieforce | last post: by
reply views Thread by theflame83 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.