简体   繁体   中英

Python Bubble sort Words

I'm new to python im looking for a code to bubble sort a list of words.

mylist = [12, 5, 13, 8, 9, 65]

def bubble(badList):
    length = len(badList) - 1
    unsorted = True

    while unsorted:
        for element in range(0,length):
            unsorted = False
            if badList[element] > badList[element + 1]:
                hold = badList[element + 1]
                badList[element + 1] = badList[element]
                badList[element] = hold
                print badList
            else:
                unsorted = True

print bubble(mylist)

this code does it with numbers i want one that does it with words. Thanks

One of the many cool things about Python is that equality and comparison operators work for strings just as they do with numbers. For example, how to compare that two numbers are the same?

7 == 7 # true!

How about two strings?

"Hello world".equals("Hello world") // Java...

"Hello world" == "Hello world" # Python!

Now to comparators. Python uses lexicographic ordering to compare strings. Basically, it looks at the first character from each string and says, "which is bigger?". If they are the same, it continues on down the string. So a few examples:

"ABC" > "BAC" # false, because the character B is greater than A
"AAAB" < "AAAC" # true, because the character B is less than C

So, your code will work no matter if mylist is comprised of ints or of strings.

A small work without builtin python, if condition for string bubble sort

alpha = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'j', 'K', 'L',
            'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'X', 
            'Y','Z']
array1 = ['BACA', 'BABB', 'AB', 'AB', 'B', 'FA', 'AD', 'A']

for j in range(0, len(array1)):
    for i in range(0, len(array1)):
        _sorted = False
        if i != len(array1)-1:
            for k in range(0,len(array1[i])):
                if not _sorted:
                    if k != (len(array1[i]) and len(array1[i+1])):
                        if alpha.index(array1[i][k]) > alpha.index(array1[i+1][k]):
                            array1[i], array1[i+1] = array1[i+1], array1[i]
                            _sorted = True
                        elif alpha.index(array1[i][k]) < alpha.index(array1[i+1][k]):
                            _sorted = True
                        else:
                            if len(array1[i+1]) < len(array1[i]):
                                array1[i], array1[i+1] = array1[i+1], array1[i]
                                _sorted = True
    print(array1)

Output of each iteration:

['BABB', 'AB', 'AB', 'B', 'BACA', 'AD', 'A', 'FA']
['AB', 'AB', 'B', 'BABB', 'AD', 'A', 'BACA', 'FA']
['AB', 'AB', 'B', 'AD', 'A', 'BABB', 'BACA', 'FA']
['AB', 'AB', 'AD', 'A', 'B', 'BABB', 'BACA', 'FA']
['AB', 'AB', 'A', 'AD', 'B', 'BABB', 'BACA', 'FA']
['AB', 'A', 'AB', 'AD', 'B', 'BABB', 'BACA', 'FA']
['A', 'AB', 'AB', 'AD', 'B', 'BABB', 'BACA', 'FA']
['A', 'AB', 'AB', 'AD', 'B', 'BABB', 'BACA', 'FA']

There's a bug in your bubble sort code that means it won't sort some (maybe most) lists correctly. This doesn't have anything to do with the data type of the values in the list though (it will have the same issues with lists of numbers or lists of strings).

Here's fixed code:

def bubble(badList):
    length = len(badList) - 1
    unsorted = True

    while unsorted:
        unsorted = False             # this was moved out of the for loop
        for element in range(0,length):
            if badList[element] > badList[element + 1]:
                hold = badList[element + 1]
                badList[element + 1] = badList[element]
                badList[element] = hold
                print badList        # comment this out when you're done testing
                unsorted = True      # this was moved up from the else block

It works for both numbers and strings, as shown here:

lst = [12, 5, 13, 8, 9, 65]
>>> bubble(lst)
[5, 12, 13, 8, 9, 65]
[5, 12, 8, 13, 9, 65]
[5, 12, 8, 9, 13, 65]
[5, 8, 12, 9, 13, 65]
[5, 8, 9, 12, 13, 65]
>>> lst
[5, 8, 9, 12, 13, 65]
>>> lst = ['a', 'list', 'of', 'words', 'foo', 'bar', 'baz']
>>> bubble(lst)
['a', 'list', 'of', 'foo', 'words', 'bar', 'baz']
['a', 'list', 'of', 'foo', 'bar', 'words', 'baz']
['a', 'list', 'of', 'foo', 'bar', 'baz', 'words']
['a', 'list', 'foo', 'of', 'bar', 'baz', 'words']
['a', 'list', 'foo', 'bar', 'of', 'baz', 'words']
['a', 'list', 'foo', 'bar', 'baz', 'of', 'words']
['a', 'foo', 'list', 'bar', 'baz', 'of', 'words']
['a', 'foo', 'bar', 'list', 'baz', 'of', 'words']
['a', 'foo', 'bar', 'baz', 'list', 'of', 'words']
['a', 'bar', 'foo', 'baz', 'list', 'of', 'words']
['a', 'bar', 'baz', 'foo', 'list', 'of', 'words']
>>> lst
['a', 'bar', 'baz', 'foo', 'list', 'of', 'words']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM