Feb-17-2019, 12:57 AM
I'm working on a sorting algorithm as a curiosity, but it seems to be stuck in an infinite loop.
apple sdfgd
a s
apple sdfgd
a s
apple sdfgd
a s
apple sdfgd
a s
etc...
def simple_sorter(unsorted): print(unsorted) result = [] result.append(unsorted[0]) unsorted.remove(unsorted[0]) for item_a in unsorted: for item_b in result: print(item_a, item_b) #debug index = result.index(item_b) if compare(item_a, item_b) < 0: result.insert(index, item_a) if item_a not in result: result.append(item_a) unsorted.remove(item_a) print(result) def compare(item_a, item_b): # return 1, a is greater # return -1, b is greater # return 0 a&b are equal length = 0 if len(item_a) > len(item_b): length = len(item_b) else: length = len(item_a) for index in range(length): print(item_a[index].lower(), item_b[index].lower()) #debug if ord(item_a[index].lower()) > ord(item_b[index].lower()): return 1 elif ord(item_a[index].lower()) < ord(item_b[index].lower()): return -1 return 0 unsorted = ["sdfgd", "Swarg", "apple", "Swerg", "aaa", "Swfrg", "DDD"] simple_sorter(unsorted)With the debug lines it produces this output:
apple sdfgd
a s
apple sdfgd
a s
apple sdfgd
a s
apple sdfgd
a s
etc...