1

I am writing a Python 3 Bubble Sort program for a homework assignment and I cannot figure out how to get the original list to be reprinted (aka the unsorted list) after the list has already been sorted.

The following already posted question gets the answer almost all the way but falls short of giving a solution for the second printed original list:

Bubble Sort in Python 3

Similar but does not address the print issue: Bubble Sort Homework

I am hoping in reposting that I can get a complete answer

import sys

def bubblesort(mylist):
    changes = passes = 0
    last = len(mylist)
    swapped = True
    # This original list (below) correctly prints as unsorted:
    print("Original List: ", ','.join(map(str, mylist)) )
    while swapped:
        swapped = False

        for j in range(1, last):
            if mylist[j - 1] > mylist[j]:
                mylist[j], mylist[j - 1] = mylist[j - 1], mylist[j]  # Swap
                changes += 1
                swapped = True
                last = j

        # Only prints and increases number of passes if there was a swap
        # Remove if statement for the correct number of passes
        if(swapped):
          passes += 1
          print('Pass', passes, ':' , ','.join(map(str, mylist)))

    # This original list (below) prints sorted:
    print("\nOriginal List: ", ','.join(map(str, mylist)) )
    print("Sorted List: ", ','.join(map(str, mylist)) )
    print("Number of passes =",passes)
    return mylist

print("Welcome to a Bubble Sort Algorithm in Python!")

mylist = " "
while True:
    print("\nBubble sort in Python 3 Program")
    mylist = input("Enter a the value or type Exit to exit: ")
    if (mylist == "exit" or mylist == "Exit" or mylist == "EXIT"):
        print("Goodbye")
        sys.exit()
    else:
        mylist = [int(v) for v in mylist.split(',')]
        bubblesort(mylist)

The program should produced the following printed results:

Original List: 4, 9, 74, 0, 9, 8, 28, 1

Pass 1: 4, 9, 0, 9, 8, 28, 1, 74

Pass 2: 4, 0, 9, 8, 9, 1, 28, 74

Pass 3 : 0, 4, 8, 9, 1, 9, 28, 74

Pass 4 : 0, 4, 8, 1, 9, 9, 28, 74

Pass 5 : 0, 4, 1, 8, 9, 9, 28, 74

Pass 6 : 0, 1, 4, 8, 9, 9, 28, 74

Original List: 4, 9, 74, 0, 9, 8, 28, 1

Sorted List: 0, 1, 4, 8, 9, 9, 28, 74

Number of Passes: 6

Actual printed results:

Original List: 4, 9, 74, 0, 9, 8, 28, 1

Pass 1: 4, 9, 0, 9, 8, 28, 1, 74

Pass 2: 4, 0, 9, 8, 9, 1, 28, 74

Pass 3 : 0, 4, 8, 9, 1, 9, 28, 74

Pass 4 : 0, 4, 8, 1, 9, 9, 28, 74

Pass 5 : 0, 4, 1, 8, 9, 9, 28, 74

Pass 6 : 0, 1, 4, 8, 9, 9, 28, 74

Original List: 0, 1, 4, 8, 9, 9, 28, 74

Sorted List: 0, 1, 4, 8, 9, 9, 28, 74

Original List appears sorted

KNHunter
  • 13
  • 4

2 Answers2

0

I'd make an additional list with the same contents and sort THAT. Be aware that if you just make a new name, it will point to the original list and will modify that:

new_list = original_list

will present you problems.

new_list = original_list[:]

will work.

mauve
  • 2,707
  • 1
  • 20
  • 34
0

You can create a deep copy of your original list as reference for printing after your sorting algorithm is performed on your list of numbers. Below code works.

import sys
from copy import deepcopy

def bubblesort(mylist):
    changes = passes = 0
    last = len(mylist)
    swapped = True
    originalList = deepcopy(mylist)
    # This original list (below) correctly prints as unsorted:
    print("Original List: ", ','.join(map(str, mylist)) )
    while swapped:
        swapped = False

        for j in range(1, last):
            if mylist[j - 1] > mylist[j]:
                mylist[j], mylist[j - 1] = mylist[j - 1], mylist[j]  # Swap
                changes += 1
                swapped = True
                last = j

        # Only prints and increases number of passes if there was a swap
        # Remove if statement for the correct number of passes
        if(swapped):
          passes += 1
          print('Pass', passes, ':' , ','.join(map(str, mylist)))

    # This original list (below) prints sorted:
    print("\nOriginal List: ", ','.join(map(str, originalList)) )
    print("Sorted List: ", ','.join(map(str, mylist)) )
    print("Number of passes =",passes)
    return mylist
Andrew St P
  • 524
  • 1
  • 5
  • 13