Skip to content

Update Normal Distribution QuickSort Readme #66

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

Merged
merged 6 commits into from
Feb 4, 2017
Merged
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
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ __Properties__

###### View the algorithm in [action][quick-toptal]




![Normal Distribution QuickSort](https://github.com/prateekiiest/Python/blob/master/sorts/normal_distribution_QuickSort_README.md)


### Selection
![alt text][selection-image]

Expand Down
80 changes: 80 additions & 0 deletions sorts/normal_distribution_QuickSort_README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#Normal Distribution QuickSort


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.


##Array Elements

The array elements are taken from a Standard Normal Distribution , having mean = 0 and standard deviation 1.

####The code

```python

>>> import numpy as np
>>> from tempfile import TemporaryFile
>>> outfile = TemporaryFile()
>>> p = 100 # 100 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)

```

------

#### The Distribution of the Array elements.

```python
>>> mu, sigma = 0, 1 # mean and standard deviation
>>> s = np.random.normal(mu, sigma, p)
>>> count, bins, ignored = plt.hist(s, 30, normed=True)
>>> plt.plot(bins , 1/(sigma * np.sqrt(2 * np.pi)) *np.exp( - (bins - mu)**2 / (2 * sigma**2) ),linewidth=2, color='r')
>>> plt.show()

```


![Array_Element_Distribution](https://github.com/prateekiiest/Algorithms/blob/master/normaldistributionforarrayelements.png)




---

---------------------

--

##Plotting the function for Checking 'The Number of Comparisons' taking place between Normal Distribution QuickSort and Ordinary QuickSort

```python
>>>import matplotlib.pyplot as plt


# Normal Disrtibution QuickSort is red
>>> plt.plot([1,2,4,16,32,64,128,256,512,1024,2048],[1,1,6,15,43,136,340,800,2156,6821,16325],linewidth=2, color='r')

#Ordinary QuickSort is green
>>> plt.plot([1,2,4,16,32,64,128,256,512,1024,2048],[1,1,4,16,67,122,362,949,2131,5086,12866],linewidth=2, color='g')

>>> plt.show()

```


----

###The Plot

* X axis denotes the number of elements to be sorted.
* Y axis denotes the number of comparisons taking place

![Plot](https://github.com/prateekiiest/Algorithms/blob/master/normaldist.png)


------------------
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)