I.laboy I.laboy - 9 days ago 7
Python Question

Python sorting algorithm

This is a coding algorithm i found in python which does not seem to work i get a name error element line 7 and a trace back line 15

the program is supposed to find the lowest number in the array and bring it into the new sorted_copy list but it seems not to work

def copy_sort(array):
copy=array[:]
sorted_copy=[]
while len(copy)>0:
minimum=0
for element in range(0,len(copy)):
if copy[elememt] < copy[minimum]:
minimum=element
print('\nRemoving value',copy[minimum],\
'from',copy)
sorted_copy.append(copy.pop(minimum))
return sorted_copy
array=[5,3,1,2,6,4]
print('Copy sort...\nArray:',array)
print('copy :', copy_sort(array))
print('array',array)

Answer

You had some error about your indentation and element word, it was element

def copy_sort(array):
    copy=array[:]
    sorted_copy=[]
    while len(copy)>0:
        minimum=0
        for element in range(0,len(copy)):
           if copy[element] < copy[minimum]:
              minimum=element
        print('\nRemoving value',copy[minimum], 'from',copy)
        sorted_copy.append(copy.pop(minimum))
    return sorted_copy

array=[5,3,1,2,6,4]
print('Copy sort...\nArray:',array)
print('copy :', copy_sort(array))
print('array',array)`