Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Quick- sort added
  • Loading branch information
junthbasnet committed Jun 1, 2019
commit f39cdef29951e779db13bfd51fee04cc8739adb2
35 changes: 35 additions & 0 deletions sorts/QuickSort_MiddlePivot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Author : Junth Basnet
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. This can be merged.


"""
Implementation of Quick Sort Algorithm with middle element as pivot element
Time Complexity : O(nlogn) - O(n^2)
"""

def QuickSortFirst(array):
return QuickSort(array, 0, len(array) - 1)

def QuickSort(array, left, right):
if left >= right:
return array
pivot = array[(left + right) // 2]
index = Partition(array, left, right, pivot)
QuickSort(array, left, index - 1)
QuickSort(array, index, right)
return array

def Partition(array, left, right, pivot):
while left <= right:
while array[left] < pivot:
left += 1
while array[right] > pivot:
right -= 1
if left <= right:
array[left], array[right] = array[right], array[left]
left += 1
right -= 1
return left

array = [1, 6, 4, 10, 7, 30, 25]
print(array)
sorted_array = QuickSortFirst(array)
print(sorted_array)