Skip to content
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

Quick Sort #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
33 changes: 33 additions & 0 deletions Hoare_quicksort.py
Original file line number Diff line number Diff line change
@@ -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()
40 changes: 40 additions & 0 deletions K_th_smallest_quickSelect.py
Original file line number Diff line number Diff line change
@@ -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()
32 changes: 32 additions & 0 deletions quick_sort.py
Original file line number Diff line number Diff line change
@@ -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()
38 changes: 38 additions & 0 deletions tail_recursion_QuickSort.py
Original file line number Diff line number Diff line change
@@ -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()