C
C#16mo ago
Adetu

❔ Problem with Quick Sort

It seems that it partially sorts it and doesn't finish. I really can't see what is wrong
void QuickSort(int[] A, int left, int right)
{
if (left < right)
{

int pivotIndex = Partition(A, left, right);
QuickSort(A, left, pivotIndex - 1);
QuickSort(A, pivotIndex + 1, right);
}


}

int Partition(int[] A, int left, int right)
{

int p = A[left];
int i = left + 1;
for (int j = left + 1; j < right; j++)
{
if (A[j] <= p)
{
int temp1 = A[j];
A[j] = A[i];
A[i] = temp1;
i += 1;
}
int temp2 = A[i - 1];
A[i - 1] = A[left];
A[left] = temp2;
}
return i - 1;

}
void QuickSort(int[] A, int left, int right)
{
if (left < right)
{

int pivotIndex = Partition(A, left, right);
QuickSort(A, left, pivotIndex - 1);
QuickSort(A, pivotIndex + 1, right);
}


}

int Partition(int[] A, int left, int right)
{

int p = A[left];
int i = left + 1;
for (int j = left + 1; j < right; j++)
{
if (A[j] <= p)
{
int temp1 = A[j];
A[j] = A[i];
A[i] = temp1;
i += 1;
}
int temp2 = A[i - 1];
A[i - 1] = A[left];
A[left] = temp2;
}
return i - 1;

}
3 Replies
phaseshift
phaseshift16mo ago
$debug
MODiX
MODiX16mo ago
Tutorial: Debug C# code - Visual Studio (Windows)
Learn features of the Visual Studio debugger and how to start the debugger, step through code, and inspect data in a C# application.
Accord
Accord16mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server