Skip to content

feat: add quick select algorithm and test #6 #139

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

prisojacques
Copy link
Contributor

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.

@prisojacques
Copy link
Contributor Author

please review the PR

@prisojacques prisojacques reopened this Jun 26, 2023
@appgurueu
Copy link
Contributor

Closing this in favor of #140

@appgurueu appgurueu closed this Jun 27, 2023
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.

2 participants