Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Sorting algorithm issues
#1
I'm working on a sorting algorithm as a curiosity, but it seems to be stuck in an infinite loop.

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...
Reply
#2
I'm not entirely sure what the problem is, but I do notice two things. First, you are modifying the lists as you are iterating over them. That causes problems with the iteration, and should be avoided unless you know exactly what you are doing. Second, the insert on line 11 could happen multiple times. I would put a break after that line, and then change line 12 to an else (an else triggers after a loop if the loop finished without triggering a break statement).
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
(Feb-17-2019, 02:08 AM)ichabod801 Wrote: I'm not entirely sure what the problem is, but I do notice two things. First, you are modifying the lists as you are iterating over them. That causes problems with the iteration, and should be avoided unless you know exactly what you are doing. Second, the insert on line 11 could happen multiple times. I would put a break after that line, and then change line 12 to an else (an else triggers after a loop if the loop finished without triggering a break statement).

I removed line 14 since the result is the important list and unsorted is discarded after the function concludes. The big one that you were correct on is the multiple insert, adding a break on line 12 fixed the issue.

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)
            index = result.index(item_b)
            if compare(item_a, item_b) < 0:
                result.insert(index, item_a)
                break
        if item_a not in result:
            result.append(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())
        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)
['sdfgd', 'Swarg', 'apple', 'Swerg', 'aaa', 'Swfrg', 'DDD']
Swarg sdfgd
s s
w d
apple sdfgd
a s
Swerg apple
s a
Swerg sdfgd
s s
w d
Swerg Swarg
s s
w w
e a
aaa apple
a a
a p
Swfrg aaa
s a
Swfrg apple
s a
Swfrg sdfgd
s s
w d
Swfrg Swarg
s s
w w
f a
Swfrg Swerg
s s
w w
f e
DDD aaa
d a
DDD apple
d a
DDD sdfgd
d s
['aaa', 'apple', 'DDD', 'sdfgd', 'Swarg', 'Swerg', 'Swfrg']
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Sorting a copied list is also sorting the original list ? SN_YAZER 3 3,046 Apr-11-2019, 05:10 PM
Last Post: SN_YAZER

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020