-2

Good evening. I have managed to bubble sort listOne. ListTwo will also need sorting. Is there a way to add listTwo into the bubble sort that I already have so that it gets sorted as well. Or do I need to write another loop?

    listOne = [3, 9, 2, 6, 1]
    listTwo = [4, 8, 5, 7, 0]

    def bubbleSort (inList):

    moreSwaps = True
while (moreSwaps):
    moreSwaps = False
    for element in range(len(listOne)-1):
        if listOne[element]> listOne[element+1]:
            moreSwaps = True
            temp = listOne[element]
            listOne[element]=listOne[element+1]
            listOne[element+1]= temp
return (inList)

      print ("List One = ", listOne)
      print ("List One Sorted = ", bubbleSort (listOne))
      print ("List Two = ", listTwo)
      print ("List Two Sorted = ", bubbleSort (listTwo))
user662973
  • 37
  • 1
  • 7
  • Have you heard about a paradigm called 'methods'? If not you can read about it here: [Method](https://en.wikipedia.org/wiki/Method_(computer_programming)) And for python: [What is a “method” in Python?](http://stackoverflow.com/q/3786881/4907452) – Fabian Damken Jun 11 '16 at 18:09

1 Answers1

1

I think you just need one method and then call the call it on the two list you can try this: That is one method to do two jobs for you.

listOne = [3, 9, 2, 6, 1]
listTwo = [4, 8, 5, 7, 0]

def bubblesort(array):
    for i in range(len(array)):
        for j in range(len(array) - 1):
            if array[j] > array[j + 1]:
                swap = array[j]
                array[j] = array[j + 1]
                array[j + 1] = swap
    print(array)


bubblesort(listOne)
bubblesort(listTwo)

[1, 2, 3, 6, 9]

[0, 4, 5, 7, 8]

Seek Addo
  • 1,871
  • 2
  • 18
  • 30