It's a wasteful idea that I came up with and implemented while implementing various sorting algorithms while practicing python to kill time.
When implementing bubble sort, sorting randomly generated data, and finding the average number of times, everyone naturally thinks that the number of sorts is based on a uniform standard normal distribution. Using it, I made a program to generate random numbers based on a standard normal distribution from uniform random numbers.
Is the program useful? ~~ python vector operation numpy
R = np.random.rand()
Random numbers based on the standard normal distribution can be generated without using. ~~
Even random
#### **`random.normalvariate(mu, sigma)`**
```normalvariate(mu, sigma)
Random numbers can be generated based on the normal distribution by using.
~~ In other words, it is a useless idea that can generate random numbers based on the standard normal distribution without putting numpy in python. ~~
Not really useful.
## The following programs
```python
# -*- coding: utf-8 -*-
import random
import matplotlib.pyplot as plt
def bubble_sort(data,data_num):
count = 0
for i in range(0,data_num):
for j in range(data_num-1,i,-1):
if data[j] <= data[j-1]:
count += 1
data[j] , data[j-1] = data[j-1] , data[j]
return count
if __name__ == '__main__':
t = 10 #Number of random numbers to perform each bubble sort
n = t*(t-1)/2 #A standard normal distribution is created in the range of n
y = [0]*n #Stores the number of times it took to sort when each bubble sort was performed.
data = [] #T to sort data each time
for j in range(0,100000):
count = 0
for i in range(0,t):
data.append(random.randint(1,100))
count = bubble_sort(data,t)
y[count] = y[count] + 1
del data[:] #Data initialization
plt.plot(y,marker="o")
plt.show()
This time, we sorted a list of 10 random numbers 100,000 times, and captured the number of times each sort was performed as a newly generated random number, and counted and graphed it. As you can see from the graph, it feels like a standard normal distribution. The maximum number of sorts when n pieces of data are sorted by bubble sort is n (n-1) / 2, so this time there are 10 sorts of data, so 10 * (10-1) / 2 = The calculation is 45. The reason why it looks like a standard normal distribution is thought to be the pattern of random numbers that belong to the number of sorts.
It's just a self-satisfaction program.
Recommended Posts