From 3040022f0b426b2b7fb896177af419a34fbcf843 Mon Sep 17 00:00:00 2001 From: Prateek Chanda Date: Sat, 4 Feb 2017 12:20:55 +0530 Subject: [PATCH 1/6] Create randomquicksort.py --- sorts/randomquicksort.py | 66 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 sorts/randomquicksort.py diff --git a/sorts/randomquicksort.py b/sorts/randomquicksort.py new file mode 100644 index 000000000..19b180578 --- /dev/null +++ b/sorts/randomquicksort.py @@ -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 Date: Sat, 4 Feb 2017 13:59:48 +0530 Subject: [PATCH 2/6] Rename randomquicksort.py to random_normaldistribution_quicksort.py --- ...{randomquicksort.py => random_normaldistribution_quicksort.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename sorts/{randomquicksort.py => random_normaldistribution_quicksort.py} (100%) diff --git a/sorts/randomquicksort.py b/sorts/random_normaldistribution_quicksort.py similarity index 100% rename from sorts/randomquicksort.py rename to sorts/random_normaldistribution_quicksort.py From ea52ca8ee00e696492f5f16ff3f1660f4248c05b Mon Sep 17 00:00:00 2001 From: Prateek Chanda Date: Sat, 4 Feb 2017 14:12:16 +0530 Subject: [PATCH 3/6] Create normaldistribution_quicksort_README.md --- normaldistribution_quicksort_README.md | 38 ++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 normaldistribution_quicksort_README.md diff --git a/normaldistribution_quicksort_README.md b/normaldistribution_quicksort_README.md new file mode 100644 index 000000000..f90ae1d49 --- /dev/null +++ b/normaldistribution_quicksort_README.md @@ -0,0 +1,38 @@ +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) + +``` + +--------------------- + +#Plotting the function for Checking 'The Number of Swappings' taking place between Normal Distribution QuickSort and Ordinary QuickSort + +```python +>>>import matplotlib.pyplot as plt + + # Normal Disrtibution 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') + #Simple 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() +``` From c2b7ba3614725011ab5dfd7a74f65b49fb32ecce Mon Sep 17 00:00:00 2001 From: Prateek Chanda Date: Sat, 4 Feb 2017 14:35:30 +0530 Subject: [PATCH 4/6] Delete normaldistribution_quicksort_README.md --- normaldistribution_quicksort_README.md | 38 -------------------------- 1 file changed, 38 deletions(-) delete mode 100644 normaldistribution_quicksort_README.md diff --git a/normaldistribution_quicksort_README.md b/normaldistribution_quicksort_README.md deleted file mode 100644 index f90ae1d49..000000000 --- a/normaldistribution_quicksort_README.md +++ /dev/null @@ -1,38 +0,0 @@ -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) - -``` - ---------------------- - -#Plotting the function for Checking 'The Number of Swappings' taking place between Normal Distribution QuickSort and Ordinary QuickSort - -```python ->>>import matplotlib.pyplot as plt - - # Normal Disrtibution 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') - #Simple 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() -``` From d4e35fb43038ee161ae913ba74ba21972744d815 Mon Sep 17 00:00:00 2001 From: Prateek Chanda Date: Sat, 4 Feb 2017 15:02:23 +0530 Subject: [PATCH 5/6] Create normal_distribution_QuickSort_README.md --- sorts/normal_distribution_QuickSort_README.md | 80 +++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 sorts/normal_distribution_QuickSort_README.md diff --git a/sorts/normal_distribution_QuickSort_README.md b/sorts/normal_distribution_QuickSort_README.md new file mode 100644 index 000000000..fc6d55ed5 --- /dev/null +++ b/sorts/normal_distribution_QuickSort_README.md @@ -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) + + +------------------ From a484b47cdf868debc6118f15d3d057a41c73c56f Mon Sep 17 00:00:00 2001 From: Prateek Chanda Date: Sat, 4 Feb 2017 15:06:49 +0530 Subject: [PATCH 6/6] Update README.md --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.md b/README.md index 1d8d3386e..fd3de1465 100644 --- a/README.md +++ b/README.md @@ -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]