-
-
Notifications
You must be signed in to change notification settings - Fork 46.7k
solved the issue #12046 and #12020 #12057
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
Closed
Closed
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
d65aecf
Added code for quicksort.py and kruskal algorithm
iz-Manik 7eb6984
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 24bcc2e
Update kruskal.py
iz-Manik f0f2213
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] d3402ee
updated requirements to solve issue
iz-Manik d425012
updated requirements
iz-Manik 8e56553
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] b492d5f
isssue resolved
iz-Manik 9e8d831
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
# Function to implement Quick Sort | ||
def quick_sort(arr: list[int], low: int, high: int) -> None: | ||
""" | ||
Perform quick sort on the given array in-place. | ||
|
||
Parameters: | ||
arr (list[int]): The list of integers to sort. | ||
low (int): The starting index of the portion of the array to sort. | ||
high (int): The ending index of the portion of the array to sort. | ||
|
||
Returns: | ||
None: The function sorts the array in-place. | ||
|
||
Doctest: | ||
>>> arr = [10, 7, 8, 9, 1, 5] | ||
>>> quick_sort(arr, 0, len(arr) - 1) | ||
>>> arr | ||
[1, 5, 7, 8, 9, 10] | ||
|
||
>>> arr = [4, 3, 2, 1] | ||
>>> quick_sort(arr, 0, len(arr) - 1) | ||
>>> arr | ||
[1, 2, 3, 4] | ||
""" | ||
|
||
if low < high: | ||
# Partitioning index | ||
pi = partition(arr, low, high) | ||
|
||
# Recursively sort elements before and after partition | ||
quick_sort(arr, low, pi - 1) | ||
quick_sort(arr, pi + 1, high) | ||
|
||
def partition(arr: list[int], low: int, high: int) -> int: | ||
""" | ||
Partition function to place the pivot element at its correct position. | ||
|
||
Parameters: | ||
arr (list[int]): The list of integers to partition. | ||
low (int): The starting index for the partition. | ||
high (int): The ending index for the partition. | ||
|
||
Returns: | ||
int: The partitioning index. | ||
""" | ||
pivot = arr[high] # Pivot | ||
i = low - 1 # Index of smaller element | ||
|
||
for j in range(low, high): | ||
if arr[j] <= pivot: | ||
i += 1 | ||
arr[i], arr[j] = arr[j], arr[i] # Swap | ||
|
||
arr[i + 1], arr[high] = arr[high], arr[i + 1] # Swap pivot | ||
return i + 1 | ||
|
||
# Driver code to take user-defined input and sort | ||
if __name__ == "__main__": | ||
# Ask the user for input | ||
n = int(input("Enter the number of elements in the array: ")) | ||
|
||
# Input array elements from the user | ||
arr = list(map(int, input(f"Enter {n} elements separated by spaces: ").split())) | ||
|
||
print("Original array:", arr) | ||
|
||
# Call quick sort function | ||
quick_sort(arr, 0, len(arr) - 1) | ||
|
||
# Print sorted array | ||
print("Sorted array:", arr) |
Empty file.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As there is no test file in this pull request nor any test function or class in the file
divide_and_conquer/quicksort.py
, please provide doctest for the functionpartition