这是我写的一个简单的排序算法:
import random as r
a = []
for i in range (100): # to add random numbers
a.append(r.randint(0,100))
print(f"Original: {a}") # Orginal numbers
i = 0
j = 0
b = a[i] # to get old value
"""sorting alogrithm """
# greatest to least
for n in range (len(a)-1):
i = 0
j = 0
for x in range (len(a)-1):
b = a[i]
if a[i] <= a[j+1]:
a[i] = a[j+1]
a[j+1] = b
i +=1
j +=1
print("")
print(f" Final_first {a}") # Numbers greatest to least
# least to greatest
for n in range (len(a)-1):
i = 0
j = 0
for x in range (len(a)-1):
b = a[i]
if a[i] >= a[j+1]:
a[i] = a[j+1]
a[j+1] = b
i +=1
j +=1
print("")
print(f" Final_second {a}") # results for least to greatest
网友评论