Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/vs/base/common/arrays.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,16 @@ export function binarySearch2(length: number, compareToKey: (index: number) => n

type Compare<T> = (a: T, b: T) => number;


/**
* Finds the nth smallest element in the array using quickselect algorithm.
* The data does not need to be sorted.
*
* @param nth The zero-based index of the element to find (0 = smallest, 1 = second smallest, etc.)
* @param data The unsorted array
* @param compare A comparator function that defines the sort order
* @returns The nth smallest element
* @throws TypeError if nth is >= data.length
Copy link

Copilot AI Nov 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The @throws documentation is incomplete. The function also needs to handle the case where nth is negative. Looking at line 124, nth = nth | 0; will convert negative values to negative integers, which when compared at line 126 with nth >= data.length won't throw an error, but will likely cause incorrect behavior in the recursive calls. The documentation should clarify the valid range for nth (i.e., 0 <= nth < data.length) or the function should validate negative values.

See below for a potential fix:

 * @throws TypeError if nth is negative or nth is >= data.length
 */
export function quickSelect<T>(nth: number, data: T[], compare: Compare<T>): T {

	nth = nth | 0;

	if (nth < 0 || nth >= data.length) {

Copilot uses AI. Check for mistakes.
*/
export function quickSelect<T>(nth: number, data: T[], compare: Compare<T>): T {

nth = nth | 0;
Expand Down
Loading