I am trying to create an original sorting algorithm for my home work for school but i can't get it to work and I don't understand why.
def sa(x):
print(x)
i = 0
s_array = []
while x:
for x[i] in x:
if x[i] == min(x):
s_array.append(x.pop(i))
i = 0
elif x[i] == max(x):
s_array.append(x.pop(i))
i = 0
else:
i += 1
print(s_array)
I know I shouldn't have print functions in the program but i wanted it to be concise and packaged. so this is the output i get:
>>> sa([89, 23, 33, 45, 10, 12, 45, 45, 45])
[89, 23, 33, 45, 10, 12, 45, 45, 45]
[89, 45, 45, 45, 45, 45, 45, 45, 12]
I had the code like this:
def sa(x):
print(x)
i = 0
s_array = []
while x:
for x[i] in x:
if x[i] == min(x):
s_array.append(x.pop(i))
i = 0
else:
i += 1
print(s_array)
but was getting this as the output and thought i had to add the max() elif:
>>> sa([89, 23, 33, 45, 10, 12, 45, 45, 45])
[89, 23, 33, 45, 10, 12, 45, 45, 45]
[10, 45, 45, 45, 45, 45, 45, 45, 45]
please help.
for x[i] in x:
is assigningx[i]
to each element ofx
as it is iterated. Absolutely certain that isn't what you are intending.while x: s_array.append(x.pop(x.index(min(x))))
.