|
| 1 | +#Normal Distribution QuickSort |
| 2 | + |
| 3 | + |
| 4 | +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. |
| 5 | +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. |
| 6 | + |
| 7 | + |
| 8 | +##Array Elements |
| 9 | + |
| 10 | +The array elements are taken from a Standard Normal Distribution , having mean = 0 and standard deviation 1. |
| 11 | + |
| 12 | +####The code |
| 13 | + |
| 14 | +```python |
| 15 | + |
| 16 | +>>> import numpy as np |
| 17 | +>>> from tempfile import TemporaryFile |
| 18 | +>>> outfile = TemporaryFile() |
| 19 | +>>> p = 100 # 100 elements are to be sorted |
| 20 | +>>> mu, sigma = 0, 1 # mean and standard deviation |
| 21 | +>>> X = np.random.normal(mu, sigma, p) |
| 22 | +>>> np.save(outfile, X) |
| 23 | +>>> print('The array is') |
| 24 | +>>> print(X) |
| 25 | + |
| 26 | +``` |
| 27 | + |
| 28 | +------ |
| 29 | + |
| 30 | +#### The Distribution of the Array elements. |
| 31 | + |
| 32 | +```python |
| 33 | +>>> mu, sigma = 0, 1 # mean and standard deviation |
| 34 | +>>> s = np.random.normal(mu, sigma, p) |
| 35 | +>>> count, bins, ignored = plt.hist(s, 30, normed=True) |
| 36 | +>>> plt.plot(bins , 1/(sigma * np.sqrt(2 * np.pi)) *np.exp( - (bins - mu)**2 / (2 * sigma**2) ),linewidth=2, color='r') |
| 37 | +>>> plt.show() |
| 38 | + |
| 39 | +``` |
| 40 | + |
| 41 | + |
| 42 | +----- |
| 43 | + |
| 44 | + |
| 45 | + |
| 46 | + |
| 47 | + |
| 48 | + |
| 49 | +--- |
| 50 | + |
| 51 | +--------------------- |
| 52 | + |
| 53 | +-- |
| 54 | + |
| 55 | +##Plotting the function for Checking 'The Number of Comparisons' taking place between Normal Distribution QuickSort and Ordinary QuickSort |
| 56 | + |
| 57 | +```python |
| 58 | +>>>import matplotlib.pyplot as plt |
| 59 | + |
| 60 | + |
| 61 | + # Normal Disrtibution QuickSort is red |
| 62 | +>>> 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') |
| 63 | + |
| 64 | + #Ordinary QuickSort is green |
| 65 | +>>> 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') |
| 66 | + |
| 67 | +>>> plt.show() |
| 68 | + |
| 69 | +``` |
| 70 | + |
| 71 | + |
| 72 | +---- |
| 73 | + |
| 74 | + |
| 75 | +------------------ |
| 76 | + |
0 commit comments