0

I am trying to sort a list of numbers through algorithm instead of using .sort() function. The logic works fine when no integer is repeated inside the list but would not work properly if the list has two or more same integer.

for eg. it algorithm works for

number = [13, 6, 9, 2, 1, 10, 3, 8, 12]

but doesnot work for

number = [13, 6, 9, 2, 1, 10, 3, 8, 8, 12]

number = [13, 6, 9, 2, 1, 10, 3, 8, 12]

for j in range(len(number)):
    min_number = number[j]
    for i in range(j, len(number)):
        if number[i] < min_number:
            min_number = number[i]
    number.remove(min_number)
    number.insert(j, min_number)
print(number)

hurat
  • 19
  • 1
  • 4

1 Answers1

0

The remove can work with two same Values it will select one but your doesn't knew who is deleted

It can't works with doublons

Or implements the insertion Sort insertion sort

lupaulus
  • 358
  • 5
  • 22
  • according to the definition of .remove() function, it removes the first occurrence of the number. – hurat Apr 16 '19 at 20:22
  • For me the strict inferior and the remove are the cause of the problem – lupaulus Apr 16 '19 at 20:32
  • alright. Thanks. Here I am implementing sequential sort method. May be the reason is that sequential sort do not work properly with repeated values – hurat Apr 16 '19 at 23:01