Some cleanup of qsort and vec

This commit is contained in:
2025-10-27 00:28:51 +00:00
parent 8ee90f0d06
commit e3863557f5
2 changed files with 10 additions and 12 deletions

View File

@@ -33,13 +33,12 @@ void quicksort(int *arr, int arr_size)
print_arr(arr, arr_size);
printf("\n");
#endif
// Optimisation: for small arr_size, a different sort like the
// bubble sort may be better, instead of initialising new stack
// frames.
// TODO Optimisation: for small arr_size, a different sort like the bubble
// sort may be better, instead of initialising new stack frames.
if (arr_size < 2)
return;
// Optimisation: Use some heuristics to figure out a better pivot
// than some constant choice of element.
// TODO Optimisation: Use some heuristics to figure out a better pivot than
// some constant choice of element.
int pivot = 0;
int pivot_value = arr[0];
for (int i = 1; i < arr_size; ++i)
@@ -57,13 +56,12 @@ void quicksort(int *arr, int arr_size)
print_arr(arr, arr_size);
printf("\n");
#endif
// Optimisation: Use a stack to do this instead of recursion.
// TODO Optimisation: Use a stack to do this instead of recursion.
//
// This algorithm is similar to a pre order traversal/dfs, in this
// case on an array where left and right partition are children
// nodes to the original array node. Also with recursion there's a
// real risk for very large ARR_SIZE of running out of space in the
// call stack.
// This algorithm is similar to a pre order traversal/dfs, in this case on an
// array where left and right partition are children nodes to the original
// array node. Also with recursion there's a real risk for very large
// ARR_SIZE of running out of space in the call stack.
if (pivot != 0)
quicksort(arr, pivot);
if (pivot < arr_size - 1)