Skip to content

Commit

Permalink
random_normaldistribution_quicksort
Browse files Browse the repository at this point in the history
This is for creating an algorithm implementing QuickSort Algorithm where the pivot element is chosen randomly between first and last elements of the array and the array elements are taken from a Standard Normal Distribution.

This is different from the ordinary quicksort in the sense, that it applies more to real life problems , where elements usually follow a normal distribution. Also the pivot is randomized to make it a more generic one.
  • Loading branch information
prateekiiest authored Feb 3, 2017
1 parent d68666d commit a3ab980
Showing 1 changed file with 66 additions and 0 deletions.
66 changes: 66 additions & 0 deletions sorts/random_normaldistribution_quicksort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
from random import randint
from tempfile import TemporaryFile
import numpy as np
import math



def _inPlaceQuickSort(A,start,end):
count = 0
if start<end:
pivot=randint(start,end)
temp=A[end]
A[end]=A[pivot]
A[pivot]=temp

p,count= _inPlacePartition(A,start,end)
count += _inPlaceQuickSort(A,start,p-1)
count += _inPlaceQuickSort(A,p+1,end)
return count

def _inPlacePartition(A,start,end):

count = 0
pivot= randint(start,end)
temp=A[end]
A[end]=A[pivot]
A[pivot]=temp
newPivotIndex=start-1
for index in range(start,end):

count += 1
if A[index]<A[end]:#check if current val is less than pivot value
newPivotIndex=newPivotIndex+1
temp=A[newPivotIndex]
A[newPivotIndex]=A[index]
A[index]=temp

temp=A[newPivotIndex+1]
A[newPivotIndex+1]=A[end]
A[end]=temp
return newPivotIndex+1,count

outfile = TemporaryFile()
p = 100 # 1000 elements are to be sorted




mu, sigma = 0, 1 # mean and standard deviation
X = np.random.normal(mu, sigma, p)
np.save(outfile, X)
print('The array is')
print(X)






outfile.seek(0) # using the same array
M = np.load(outfile)
r = (len(M)-1)
z = _inPlaceQuickSort(M,0,r)

print("No of Comparisons for 100 elements selected from a standard normal distribution is :")
print(z)

0 comments on commit a3ab980

Please sign in to comment.