Search code examples
pythonbubble-sort

Bubble Sort using a while loop in Python


I've been set a bubble sort for homework, and I've been trying to use a while loop. I know that it's possible with a for loop, but I don't really understand them and I'd like to write something that I understand.

unsorted = True
numbers = []
unsortchecker = 0
start = 0
maxlist = int(input("How many numbers should be sorted?"))
if len(numbers) == 1:
   print(1)
while len(numbers) < maxlist:
    num = input("Please enter a number: ")
    numbers.append(num)
while unsorted:
    if unsortchecker == 0:
        unsorted = False
    while start != maxlist:
        if numbers[start] > numbers[start+1]:
            replacement = numbers[start]
            replaced = numbers[start+1]
            del numbers[start]
            del numbers[start+1]
            numbers.insert(start, replaced)
            numbers.insert(start+1, replacement)
            unsortchecker = 1
            start = start + 1
            print(numbers)
      else:
          start = start + 1
          print(numbers)
print(numbers)

When I run this, it will work for the first few, and then replace different numbers to what I want, and then give back an error IndexError: list index out of range Any ideas?

Edited Code

unsorted = True
numbers = []
unsortchecker = 0
start = 0
maxlist = int(input("How many numbers should be sorted?"))
end = maxlist
if len(numbers) == 1:
    print(1)
while len(numbers) < maxlist:
    num = input("Please enter a number: ")
    numbers.append(num)
while unsorted:
    if unsortchecker == 0:
        unsorted = False
    start = 0
     while start < maxlist-1:
        if numbers[start] > numbers[start+1]:
            replacement = numbers[start]
            numbers[start] = numbers[start + 1]
            numbers[start + 1] = replacement
            unsortchecker = unsortchecker + 1
            start = start + 1
            print(numbers)
        else:
             maxlist = maxlist - 1
             print(numbers)
print(numbers)

Solution

  • For starters:

    replacement = numbers[start]
    replaced = numbers[start+1]
    del numbers[start]
    del numbers[start+1]
    numbers.insert(start, replaced)
    numbers.insert(start+1, replacement)
    

    This looks like a very cumbersome way to swap the two numbers. Try this way:

    replacement = numbers[start]
    numbers[start] = numbers[start + 1]
    numbers[start + 1] = replacement
    

    And no need for del and insert. Understand what these three lines do: I put the value that's at position start into the variable replacement. Then I overwrite the value at position start with the value at position start + 1. Then I overwrite the value at position start + 1 with the value in replacement, which is the old value of numbers[start].

    There's an even more efficient way (in python, anyway) to swap numbers, but it could be a bit confusing for starters.

    That's not the only problem though.

    The way you have implemented BubbleSort is that you "bubble up" instead of "bubble down". That means that after the very first pass, you now know that the largest element will be at the end of the list.

    This means that instead of increasing start by 1 after the first pass, you'd have to reduce the upper end by 1.