~~ I don't think I'll forget ~~
(Addition: 2020/06 11:13: 00) I received advice from @shiracamus, so I will correct it.
Bubble sort is a sort algorithm that is also called the basic exchange method, and ** it will be this if you try to make it properly **
bubble.py
'''1'''def bubble(T):
'''2''' i = len(T)-1
'''3''' while i:
'''4''' for j in range(i-1):
'''5''' if T[j] > T[i]:
'''6''' T[j],T[i] = T[i],T[j]
'''7''' i -= 1
'''8''' return T
Wow, it's a very short algorithm
The side who wrote it was a little surprised. Since it is so short, I will explain it with line numbers.
len (T)
is "length", so if you insert it as it is in the array number, an error will occur that refers to the outside of the array.while
statement only needs to contain characters that are true
, just like ʻif, so I added ʻi
alone, which has the same meaning as ʻi! = 0. --If you really want to use
for, delete the first line and write
for i in range (len (T) -1,0, -1): `.for
. range (i)
is fine, but the loop is wasted once.
--T [i]> T [i] Compare the same thing with false
[Wiki](https://ja.wikipedia.org/wiki/%E3%83%90%E3%83%96%E3%83%AB%E3%82%BD%E3%83%BC%E3%83 As you can see by referring to% 88), it is characterized by being heavy because it is an algorithm that has a double loop when sorting. Also, swapping everything one by one is also a cause of heavy weight, so the improvement algorithm saves the data.
bubble2.py
def bubble2(T):
for i in range(len(T)-1,0,-1):
tmp = T[0]
for j in range(1,i+1):
if tmp < T[j]:
tmp , T[j] = T[j], tmp
T[j-1]=T[j]
T[i] = tmp
print(T)
return T
I tried to make it on the spot, but is there any improvement in this? It should be.
I have created a program that creates a random array with no duplicate numbers, so I will give an example.
(Corrected part)
Avoid using lists as default arguments. Reference: https://docs.python.org/ja/3/faq/programming.html#why-are-default-values-shared-between-objects
I have corrected the part that was xMake (i, T = [-1]) and the text of the program.
makeLis.py
import random as r
def xMake(i,T = None ):
if T is None:
T = [r.randint(1,i)]*i
return xMake( 1 , T )
if i == len(T):
return T
T[i] = r.randint(1,len(T))
for j in range(i):
if T[i] == T[j]:
return xMake( i , T )
return xMake( i+1 , T )
print(xMake(10))
>>[2, 1, 3, 6, 4, 10, 9, 5, 7, 8]
print(xMake(20))
>>[13, 20, 1, 12, 7, 8, 6, 4, 17, 11, 14, 9, 18, 3, 5, 10, 15, 2, 19, 16]
~~ People who use the recurrence function even if they are willing ~~ Please tell me if there is a better or cleaner way to write.
I learned from @shiracamus. You can write clearly by using> range and sample.
By_shiracamus.py
import random as r
def xMake(i):
return r.sample(range(1, i + 1), k=i)
print(xMake(10))
print(xMake(20))
Using random.sample
makes it so much easier ...!
Thank you! I am going to use it as an example!
When declaring a function, I often name it x〇〇 ()
(usually a number).
If you write or copy a program for no purpose and change the writing style, you will make a mistake when programming if there is no rule in the function name.
Then, when you actually use it, rewrite it with the replace function of the text editor to make it a name that you can understand.
I don't know if this is a good thing, but since I'm not good at English, I sometimes make embarrassing things such as spelling mistakes.