From 1d77617b80a47e448fa64d96814bbc554f26e0f8 Mon Sep 17 00:00:00 2001 From: ashutoshbenni Date: Fri, 24 Mar 2017 17:34:13 +0530 Subject: [PATCH] Quick Sort --- Hoare_quicksort.py | 33 +++++++++++++++++++++++++++++ K_th_smallest_quickSelect.py | 40 ++++++++++++++++++++++++++++++++++++ quick_sort.py | 32 +++++++++++++++++++++++++++++ tail_recursion_QuickSort.py | 38 ++++++++++++++++++++++++++++++++++ 4 files changed, 143 insertions(+) create mode 100644 Hoare_quicksort.py create mode 100644 K_th_smallest_quickSelect.py create mode 100644 quick_sort.py create mode 100644 tail_recursion_QuickSort.py diff --git a/Hoare_quicksort.py b/Hoare_quicksort.py new file mode 100644 index 0000000..48720ff --- /dev/null +++ b/Hoare_quicksort.py @@ -0,0 +1,33 @@ +from random import randint +def partition(arr, p ,r): + pivot = arr[p] + i = p - 1 + j = r + 1 + while True: + j -= 1 + while j >= p and arr[j] > pivot : + j -= 1 + i += 1 + while i <= r and arr[i] < pivot: + i += 1 + if i < j: + arr[i], arr[j] = arr[j], arr[i] + else: + #arr[p], arr[j] = arr[j], arr[p] don't need to do this since we are calling quicksort [p,q] & (q,r]... + return j + +def quicksort(arr, p ,r): + if p < r: + q = partition(arr, p, r) + quicksort(arr, p, q) + quicksort(arr, q + 1, r) + +def main(): + arr = [randint(-1,10) for i in range(9)] + print arr + print "applying Hoare's partition Quick Sort..." + quicksort(arr,0,len(arr) - 1) + print arr + +if __name__ == "__main__": + main() diff --git a/K_th_smallest_quickSelect.py b/K_th_smallest_quickSelect.py new file mode 100644 index 0000000..0f5b308 --- /dev/null +++ b/K_th_smallest_quickSelect.py @@ -0,0 +1,40 @@ +from random import randint +from time import time + +def partition(arr,p,r): + pivot = arr[r] + i = p - 1 + for j in range(p,r):#using Lomuto's partition method... + if arr[j] <= pivot: + i += 1 + arr[i],arr[j] =arr[j],arr[i] + arr[i + 1],arr[r] = arr[r],arr[i + 1] + return i + 1 + +def quickselect(arr,p,r,key): + if p < r: + q = partition(arr,p,r) + if q == key: # If partition index is equal to kth number stop... + return + elif q > key - 1: + quickselect(arr,p,q-1,key) + else: + quickselect(arr,q+1,r,key) + +def main(): + arr = [randint(-1,1000000) for i in range(1000000)]#picking numbers randomly + print "Which smallest number you want to you wnat to find an above array?" + k = int(raw_input()) + if k > len(arr):# k should be less than the length of input... + start = time() + raise "Invalid input: Exceeds the array limit ERROR." + else: + start = time()#calculate the time to find kth smallest + print "finding %dth smallest element..." % k + quickselect(arr,0,len(arr)-1,k-1) + #print arr + print arr[k-1] + print time() - start + +if __name__ == "__main__":#execution will start from here... + main() diff --git a/quick_sort.py b/quick_sort.py new file mode 100644 index 0000000..a8b03d1 --- /dev/null +++ b/quick_sort.py @@ -0,0 +1,32 @@ +from random import randint +from time import time + +def partition(arr, p, r): + pivot = arr[r] + i = p - 1 + for j in range(p, r): + if arr[j] <= pivot: + i += 1 + arr[j], arr[i] = arr[i], arr[j] + else: + arr[i + 1],arr[r] = arr[r], arr[i + 1] + print "pivot = %d" % (i + 1) + print arr + return i + 1 + +def quicksort(arr, p ,r): + if p < r: + q = partition(arr, p, r) + quicksort(arr, p, q - 1) + quicksort(arr, q + 1, r) + +def main(): + arr = [randint(-1,10) for i in range(10)] + print arr + print "Applying quick sort..." + start = time() + quicksort(arr, 0, len(arr)- 1) + print time() - start + +if __name__ == "__main__": + main() diff --git a/tail_recursion_QuickSort.py b/tail_recursion_QuickSort.py new file mode 100644 index 0000000..a7ec923 --- /dev/null +++ b/tail_recursion_QuickSort.py @@ -0,0 +1,38 @@ +from random import randint +from time import time + + +def partition(arr, p, r): + pivot = arr[p] + i = p - 1 + j = r + 1 + while True: + j -= 1 + while j >= p and arr[j] > pivot: + j -= 1 + i += 1 + while i <= r and arr[i] < pivot: + i += 1 + if i < j: + arr[i], arr[j] = arr[j], arr[i] + else: + return j + +def tail_recursion_quick_sort(arr, p, r): + while p < r: + q = partition(arr, p, r) + tail_recursion_quick_sort(arr, p, q) + p = q + 1 + +def main(): + arr = [randint(-1,11) for i in range(9)] + print arr + start = time() + print "Performing Tail Recursion Quick Sort..." + tail_recursion_quick_sort(arr,0,len(arr) - 1) + print arr + print time() - start + +if __name__ == "__main__": + main() +