feat: add quick select algorithm and test #6 #139
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Quick Select Algorithm and Test Cases
The code includes an implementation of the Quick Select algorithm in TypeScript. Quick Select is an algorithm that selects the kth smallest element from an array. It is based on the QuickSort approach and uses a partitioning technique to efficiently identify the desired element.
The implementation consists of two main functions:
partition
The partition function is a helper function used in the Quick Select algorithm to partition the array around a pivot element.
It takes in an array arr, left and right boundaries left and right, and the index of the pivot element pivotIndex.
The function swaps the pivot element with the rightmost element, then iterates through the array from the left boundary to the right boundary.
Elements smaller than the pivot value are swapped with elements at the storeIndex, and the storeIndex is incremented.
Finally, the pivot element is placed at the storeIndex, separating the smaller elements on its left and larger elements on its right.
The function returns the new index of the pivot element after partitioning.
quickSelect
The quickSelect function is the main algorithm based on the Quick Select approach.
It takes in an array arr, left and right boundaries left and right, and the index representing the kth smallest element k to find.
The function first checks if the left and right boundaries are equal. If they are, it means there is only one element, and it is returned as the kth smallest element.
It randomly selects a pivot index between the left and right boundaries, and performs partitioning using the partition helper function.
After partitioning, it compares the pivot's new index pivotNewIndex with the target index k.
If they are equal, the pivot element at index k is the kth smallest element and is returned.
If k is less than pivotNewIndex, the function recursively calls quickSelect on the left subarray (from left to pivotNewIndex - 1).
If k is greater than pivotNewIndex, the function recursively calls quickSelect on the right subarray (from pivotNewIndex + 1 to right).
This recursive process continues until the kth smallest element is found and returned.
The provided test cases cover various scenarios for the Quick Select algorithm:
Worst Case Test: It verifies if the algorithm handles the worst case scenario and returns the correct kth smallest element.
An array with elements [10, 9, 8, 7, 6, 5, 4, 3, 2, 1] is used.
The target kth smallest element is set to 2.
The test expects the algorithm to return the value 3, indicating that the second smallest element in the array is correctly identified.
Best Case Test: It checks if the algorithm handles the best case scenario and returns the correct kth smallest element.
An array with elements [1, 4, 2, 9, 5, 7, 3, 8, 10, 6] is provided.
The target kth smallest element is set to 5.
The test expects the algorithm to return the value 6, indicating that the fifth smallest element in the array is correctly identified.