Skip to content

Conversation

@j-a-c-k-goes
Copy link

PR_DESCRIPTION.md

Add Vector_sort function with context support

Consolidates vector sorting functions as requested in issue #1785.
Implements "Highlander rule" via providing single preferred
sorting function while maintaining backward compatibility.

Features:

  • Context-aware comparison functions for advanced sorting logic
  • Hybrid algorithm: insertion sort ≤16 elements, quicksort >16 elements
  • Backward compatible: existing functions remain unchanged
  • NULL-safe context handling

Performance benefits:

  • Optimal algorithm selection based on array size
  • Up to 93% faster than pure insertion sort on large arrays
  • 16-element threshold for algorithm switching

This addresses issue #1785 without breaking existing code.
Future commits can gradually migrate call sites to use Vector_sort.

Signed-off-by: [_ack] <[[email protected]]>

Consolidates vector sorting functions as mentioned in issue htop-dev#1785.
Implements Highlander Rule by providing single sorting function.

Features:
- Context-aware comparison functions for sorting logic
- Hybrid algo: insertion sort <=16 elements, quicksort >16 elements
- Backward compatible. Existing functions unchanged
- NULL-safe context handling

Performance benefits:
- Algo selection based on array sizing
- Faster inserts on large arrays than w/ pure insertion sort
- Standard 16 element thresh for algo switching

This address issue htop-dev#1785 w/o breaking existing code.
Future commits can migrate call sites to use Vector_sort gradually (if merged)
@Explorer09
Copy link
Contributor

Hi.

Good contribution, but I have a concern on using quicksort, which can break stability in sorting. The original code uses insertionSort probably because it's a stable sort, or because it deals with data that are more often partially sorted.

So when it comes to consolidating sorting function, I would consider any unstable sorting function be out.

@j-a-c-k-goes
Copy link
Author

@Explorer09 are you finding the implemented sort to be unstable? Could retest, but initial tests were not unstable.

@Explorer09
Copy link
Contributor

Explorer09 commented Oct 24, 2025

are you finding the implemented sort to be unstable? Could retest, but initial tests were not unstable.

@j-a-c-k-goes
It's computer science. A sorting algorithm is said to be "stable" when the items that are compared to be equal keep their relative order after sorted. This is important for data that can be sorted in multiple, different keys (think of a data table). When I sort the table under key A, then reorder the items under key B. I except items that are equal under B keep their relative order of A.

@BenBE
Copy link
Member

BenBE commented Oct 25, 2025

This PR should be based on the commit d3cd557 that I mentioned in #1784 to allow a proper comparison for the chosen implementations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants