Is this algorithm good or can I improve it?
static void QuickSort(int[] a)
{
QuickSort(a, 0, a.Length - 1);
}
static void QuickSort(int[] a, int start, int end)
{
if (start >= end)
{
return;
}
int num = a[start];
int i = start, j = end;
while (i < j)
{
while (i < j && a[j] > num)
{
j--;
}
a[i] = a[j];
while (i < j && a[i] < num)
{
i++;
}
a[j] = a[i];
}
a[i] = num;
QuickSort(a, start, i - 1);
QuickSort(a, i + 1, end);
}
int num = a[start]
could yield better performance (I believe most QS implementations choose the center element, which does excellently for already-sorted [or near sorted] data). – Jesse C. Slicer 2 days agoa[i] = a[j]; while (i < j && a[i] < num) i++; a[j] = a[i];
instead of the conventional swap? Does this code work, as required for CR? (It almost does: the minimal change required is the inclusion of values equal to the pivot in at least one of the conditions in the loop.) – greybeard 11 hours ago