Bubble sort


#1



Traceback (most recent call last):
File "/Volumes/ELEMENTS/bubble search.py", line 7, in
if lists[a] < lists[a-1]:
IndexError: list index out of range


i thought that it would return a sorted list


lists = [4,3,2,1]
a = len(lists)
b = 0
c = 0
for count in lists:
    for count in lists:
        if lists[a] < lists[a-1]:
            a -= 1
            c = lists[a]
            b = lists[a-1]
            lists[a] = b
            lists[a-1] = c
        elif lists[a] >= lists[a-1]:
            a -= 1
print(lists)


#2

Is this your personal code?


#3

yes :grin:. it is my own code.


#5

Okay if you want to sort items in a list you can shorten your code by a fairly large margin by using the python .sort() method :slight_smile:


#6

it is a school project so therefore i can't use inbuilt functions. thank you for trying though :+1:


#7

Oh okay then I suggest iterating through your list then each char,

n = 0
for z in lists:
    n+=1
for i in range(n):
        for j in range(1, n-i):
            if lists[j-1] > lists[j]:
                 (lists[j-1], lists[j]) = (lists[j], lists[j-1])
print(lists)

Btw you only need one variable containing 0,
n = 0

not two,


#8

This is a brow raiser. The inner loop should not have the same iterator.

>>> def bubble_sort(x):
    n = len(x)
    for i in range(0, n-1):
        for j in range(i+1, n):
            if x[i] > x[j]:
                x[i], x[j] = x[j], x[i]
    return x

>>> print(bubble_sort([4,5,6,7,8,2,3,4]))
[2, 3, 4, 4, 5, 6, 7, 8]
>>>

#9

i don't understand as the thing i need to do is swap the numbers around.


#10

i also don't understand what this piece of code does.


#11

A normal swap between two elements can be written like so,

temp = a
a = b
b = temp

Python lets us swap them in one line without the temporary variable (it has one internally)

a, b = b, a

Eg.

      0  1  2  3  4  5  6  7
      i
    [ 4, 5, 6, 7, 8, 2, 3, 4]   no swap
         j
    [ 4, 5, 6, 7, 8, 2, 3, 4]   no swap
            j
    [ 4, 5, 6, 7, 8, 2, 3, 4]   no swap
               j
    [ 4, 5, 6, 7, 8, 2, 3, 4]   no swap
                  j
    [ 4, 5, 6, 7, 8, 2, 3, 4]   swap  2 & 4
                     j
    [ 2, 5, 6, 7, 8, 4, 3, 4]   no swap
                        j
    [ 2, 5, 6, 7, 8, 4, 3, 4]   no swap
                           j

         i
    [ 2, 5, 6, 7, 8, 4, 3, 4]   no swap
            j
    [ 2, 5, 6, 7, 8, 4, 3, 4]   no swap
               j
    [ 2, 5, 6, 7, 8, 4, 3, 4]   no swap
                  j
    [ 2, 5, 6, 7, 8, 4, 3, 4]   swap  5 & 4
                     j
    [ 2, 4, 6, 7, 8, 5, 3, 4]   swap  4 & 3
                        j
    [ 2, 3, 6, 7, 8, 5, 4, 4]   no swap
                           j

            i
    [ 2, 3, 6, 7, 8, 5, 4, 4]   no swap
               j
    [ 2, 3, 6, 7, 8, 5, 4, 4]   no swap
                  j
    [ 2, 3, 6, 7, 8, 5, 4, 4]   swap  6 & 5
                     j
    [ 2, 3, 5, 7, 8, 6, 4, 4]   swap  5 & 4
                        j
    [ 2, 3, 4, 7, 8, 6, 5, 4]   no swap
                           j

               i
    [ 2, 3, 4, 7, 8, 6, 5, 4]   no swap
                  j
    [ 2, 3, 4, 7, 8, 6, 5, 4]   swap 7 & 6
                     j
    [ 2, 3, 4, 6, 8, 7, 5, 4]   swap  6 & 5
                        j
    [ 2, 3, 4, 5, 8, 7, 6, 4]   swap  5 & 4
                           j

                  i
    [ 2, 3, 4, 4, 8, 7, 6, 5]   swap 8 & 7
                     j
    [ 2, 3, 4, 4, 7, 8, 6, 5]   swap 7 & 6
                        j
    [ 2, 3, 4, 4, 6, 8, 7, 5]   swap 6 & 5
                           j

                     i
    [ 2, 3, 4, 4, 5, 7, 8, 6]   no swap
                        j
    [ 2, 3, 4, 4, 5, 7, 8, 6]   swap 7 & 6
                           j

                        i
    [ 2, 3, 4, 4, 5, 6, 8, 7]   swap 8 & 7
                           j

28 comparisons, 13 swaps.


#12

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.