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

View File

@@ -16,7 +16,7 @@
@brief A vector, consisting of a header and some payload (data). @brief A vector, consisting of a header and some payload (data).
@details The idea is that the user must never actually interact with this @details The idea is that the user must never actually interact with this
structure directly, only the data involved. Any vector related functions structure directly, only the data involved. Any vector related functions
deal with will take the pointer to the data and look behind it to get the header.
*/ */
typedef struct typedef struct
{ {