-
-
Notifications
You must be signed in to change notification settings - Fork 46.9k
Added Quicksort #5295
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
Added Quicksort #5295
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# Implementation of Quick sort algo-> https://en.wikipedia.org/wiki/Quicksort | ||
class QuickSort(): | ||
def __init__(self, array,asc=True)->None: | ||
self.asc=asc | ||
self.array = array | ||
|
||
def partition(self, start, end)->integer: # Will sort in ascending order by default | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 Please provide type hint for the parameter: Please provide type hint for the parameter: |
||
'''Normal partition routine read more about it here: google Lomuto partition algorithm. ''' | ||
i = start | ||
j = start+1 | ||
while(j < end): | ||
if(self.asc): | ||
if(self.array[j] < self.array[start]): | ||
i += 1 | ||
self.array[j], self.array[i] = self.array[i], self.array[j] | ||
j += 1 | ||
else: | ||
if(self.array[j] > self.array[start]): | ||
i += 1 | ||
self.array[j], self.array[i] = self.array[i], self.array[j] | ||
j += 1 | ||
self.array[start], self.array[i] = self.array[i], self.array[start] | ||
return i | ||
def sort(self,start,end)->None: #recursive implementation of quick sort | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 Please provide type hint for the parameter: Please provide type hint for the parameter: |
||
if(start>=end): | ||
return | ||
i=self.partition(start,end) | ||
self.sort(start,i) | ||
self.sort(i+1,end) | ||
|
||
|
||
ls = [3, 2, 1, 4, 3, 0, 5, 9,0,99,88,77,66,55,1] | ||
#sort in ascending order | ||
q_s=QuickSort(ls) | ||
q_s.sort(start=0,end=len(ls)) | ||
print(ls) | ||
#descending | ||
q_s=QuickSort(ls,False) | ||
q_s.sort(start=0,end=len(ls)) | ||
print(ls) |
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.
Please provide type hint for the parameter:
array
Please provide type hint for the parameter:
asc