I’ve tried a bunch of different ways to write this, but mostly i get stuck in an endless loop.
This version of the code just doesn’t sort it at all and i don’t know what the problem is.
void quickSort(int unsorted[], int left, int right) {
int i = left, j = right;
int pivot = (left + right) / 2;
while (i == pivot || j == pivot) {
if (unsorted[i] >= unsorted[pivot] && unsorted[pivot] >= unsorted[j])
swap(unsorted[i], unsorted[j]);
if (i < pivot)
i++;
if (j > pivot)
j--;
};
if (left < j && unsorted[left] != unsorted[j])
right = pivot, quickSort(unsorted, left, right);
if (i < right && unsorted[right] != unsorted[i])
left = pivot +1, quickSort(unsorted, left, right);
}
unsorted is an array filled with 100 random values from 0 to 200.
I’m sorry for the slow update.
And about the code, i re-wrote most of it.
This is what it looks like now:
void quickSort(int unsorted[], int left, int right)
{
int i = left,
j = right,
count = 0;
int pivot = (left + right) / 2;
do
{
while (unsorted[i] < unsorted[pivot])
i++;
while (unsorted[j] > unsorted[pivot])
j--;
if (unsorted[i] >= unsorted[j] && i <= j)
{
swap(unsorted[i], unsorted[j]);
i++;
j--;
count++;
}
if (i == pivot && unsorted[pivot] < unsorted[j] && count == 0)
{
swap(unsorted[i], unsorted[j]);
i++;
j--;
count++;
}
if (j == pivot && unsorted[pivot] < unsorted[i] && count == 0)
{
swap(unsorted[i], unsorted[j]);
i++;
j--;
count++;
}
if (i == j && unsorted[i] > unsorted[pivot] && count == 0)
{
swap(unsorted[i], unsorted[pivot]);
i++;
j--;
count++;
}
if (i == j && unsorted[i] < unsorted[pivot] && count == 0)
{
swap(unsorted[i], unsorted[pivot]);
i++;
j--;
}
count = 0;
} while (i < j);
if (left < j)
quickSort(unsorted, left, j);
if (i < right)
quickSort(unsorted, i, right);
}
i’ve been trying this code over and over again with 10 random values and it works most of the time. There are some cases it doesn’t work and i’m trying to figure out when.
An example of when it doesn’t work are when the values are: 160, 151, 159, 112, 7, 121, 105, 48, 186.
Solved it. Removed a lot of the code, made it a lot more simple, but atleast it works.
Don’t even know if u can call it a quicksearch anymore, but here is the final version:
void quickSort(int unsorted[], int left, int right)
{
int i = left,
j = right;
int pivot = right;
do
{
while (unsorted[i] < unsorted[pivot])
i++;
while (unsorted[j] > unsorted[pivot])
j--;
if (unsorted[i] >= unsorted[j] && i <= j)
{
swap(unsorted[i], unsorted[j]);
i++;
j--;
}
} while (i < j);
if (left < j)
quickSort(unsorted, left, j);
if (i < right)
quickSort(unsorted, i, right);
}
You didn’t test your function on an array of length 1.
Get that to work, then try an array of length 2.
Once that’s working, there’s a good chance it’ll work when you give it an array of length 3.
EDIT:
Kudos for including a reproducible example.
This code fails before recursion. The first step is to choose a pivot element and move some elements around until the pivot element is not less than any to the left of it and not more than any to the right of it. Your code loses track of which element it considers the pivot element; it swaps out the pivot element, but seems to think that the same location is still the pivot. Try working the algorithm this far with pencil and paper, and you’ll see what I mean.