I just learned the quicksort algorithm and tried to implement it, but it feels dirty:
#include <iostream>
void quicksort(int list[], int low, int high)
{
if(low >= high)
return;
else
{
int pivot = low, i = low, j = high;
while(i < j)
{
while(list[i] <= list[pivot] && i < high)
{
i++;
}
while(list[j] > list[pivot] && j > low)
{
j--;
}
if(i > j)
break;
int temp = list[i];
list[i] = list[j];
list[j] = temp;
}
int temp = list[pivot];
list[pivot] = list[j];
list[j] = temp;
quicksort(list, low, j-1);
quicksort(list, j+1, high);
}
}
int main()
{
int arr[10] = { 12, 2, 24, 32, 5, 1203, 7, 123, 2354, 2 };
quicksort(arr, 0, 9);
for(int i = 0; i < 10; i++)
{
std::cout << arr[i] << " ";
}
}
The break
condition in the while loop feels really cheap; as if I did something wrong and needed to put it there...
What can I improve?