Problem implementing QuickSort algorithm

M

Meyvn

Dear microsoft.public.dotnet.languages.csharp-ers,

I'm struggling with my implementation of a QuickSort algorithm. I
believe I grasp the concept (after looking up some explanation of the
algorithm), but there seems to be something wrong with my code. Any
help would be appreciated :).

Note: The items in this array are randomized.

Regards,

Meyvn.

private void Sort()
{
QuickSort(0, array.Length - 1);
}

/**************Implementation of The Quick Sort
Algorithm**************

This is an in-place, divide-and-conquer, massively recursive
sort.

Step 1. Pick an element, called a pivot, from the list.
Step 2. Reorder the list so that all elements which are less
than the pivot
come before the pivot and so that all elements
greater than the pivot
come after it (equal values can go either way).
After this partitioning, the pivot is in its final
position.
This is called the partition operation.
Step 3. Recursively sort the sub-list of lesser elements
and the sub-list of greater elements.

The base case of the recursion are lists of size zero or one,
which are always sorted.


**********************************************************************/

private void QuickSort(int left, int right)
{
//set value of pivot to leftmost item in the array
int pivot = getrokkenGetallen
;
int hold_left = left;
int hold_right = right;
int temp;

//partition the list, moving from the left looking for a
value > pivot and
//moving from the right looking for a value <= pivot, if
you find a match,
//swap the values. Keep doing this until left < right
becomes false. Swap the pivot
//with array
and you are done. Recursively sort the
subarrays
//the same way.

while (left < right)
{
//look for items smaller and larger then pivot while
left < right, if so swap.
//When we finish swap pivot with array
.
while (array
<= pivot && left < right)
left++;

while (array
> pivot && left < right)
right--;

if (left != right)
{
temp = array
;
array
= array
;
array
= temp;
}
}

//out of the loop - we are almost done sorting this list
//all that remains is to swap array
with pivot
temp = array
;
array
= array[hold_left];
array[hold_left] = temp;

//partitioning is now complete, pivot is in its final
position,
//recursively sort the two subarrays
pivot = right;
left = hold_left;
right = hold_right;

if (left < pivot)
QuickSort(left, pivot - 1);
if (right > pivot)
QuickSort(pivot + 1, right);
}​
 
M

Meyvn

I have managed to figure it out myself, I made a false assumption
somewhere in my code.
But after extensive debugging I discovered my error :). If anyone
cares here is the fixed code:

private void QuickSort(int left, int right)
{
//set value of pivot to leftmost item in the array
int pivot = array
;
int hold_left = left;
int hold_right = right;
int temp;

//partition the list, moving from the left looking for a
value > pivot and
//moving from the right looking for a value <= pivot, if
you find a match,
//swap the values. Keep doing this until left < right
becomes false. Swap the pivot
//with array
and you are done. Recursively sort the
subarrays
//the same way.

while (left < right)
{
//look for items smaller and larger then pivot while
left < right, if so swap.
//When we finish swap pivot with array
.
while (array
<= pivot && left < right)
left++;

while (array
> pivot)
right--;

if (left != right && left < right)
{
temp = array
;
array
= array
;
array
= temp;
}
}

//out of the loop - we are almost done sorting this list
//all we need to do is swap array
with the pivot
if (pivot != array
)
{
array[hold_left] = array
;
array
= pivot;
}

//partitioning is now complete, pivot is in its final
position,
//recursively sort the two subarrays
pivot = right;
left = hold_left;
right = hold_right;

if (left < pivot)
QuickSort(left, pivot - 1);
if (right > pivot)
QuickSort(pivot + 1, right);
}​
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top