- Notifications
You must be signed in to change notification settings - Fork360
Create Quick_Sort.py#154
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
63 changes: 63 additions & 0 deletionspython program/Quick_Sort.py
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,63 @@ | ||
| # Python program for implementation of Quicksort Sort | ||
| # This implementation utilizes pivot as the last element in the nums list | ||
| # It has a pointer to keep track of the elements smaller than the pivot | ||
| # At the very end of partition() function, the pointer is swapped with the pivot | ||
| # to come up with a "sorted" nums relative to the pivot | ||
| # Function to find the partition position | ||
| def partition(array, low, high): | ||
| # choose the rightmost element as pivot | ||
| pivot = array[high] | ||
| # pointer for greater element | ||
| i = low - 1 | ||
| # traverse through all elements | ||
| # compare each element with pivot | ||
| for j in range(low, high): | ||
| if array[j] <= pivot: | ||
| # If element smaller than pivot is found | ||
| # swap it with the greater element pointed by i | ||
| i = i + 1 | ||
| # Swapping element at i with element at j | ||
| (array[i], array[j]) = (array[j], array[i]) | ||
| # Swap the pivot element with the greater element specified by i | ||
| (array[i + 1], array[high]) = (array[high], array[i + 1]) | ||
| # Return the position from where partition is done | ||
| return i + 1 | ||
| # function to perform quicksort | ||
| def quickSort(array, low, high): | ||
| if low < high: | ||
| # Find pivot element such that | ||
| # element smaller than pivot are on the left | ||
| # element greater than pivot are on the right | ||
| pi = partition(array, low, high) | ||
| # Recursive call on the left of pivot | ||
| quickSort(array, low, pi - 1) | ||
| # Recursive call on the right of pivot | ||
| quickSort(array, pi + 1, high) | ||
| data = [1, 7, 4, 1, 10, 9, -2] | ||
| print("Unsorted Array") | ||
| print(data) | ||
| size = len(data) | ||
| quickSort(data, 0, size - 1) | ||
| print('Sorted Array in Ascending Order:') | ||
| print(data) |
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.