About how to print every time after using bubble sort.

Asked 1 years ago, Updated 1 years ago, 245 views

I have a question about Python.In order to execute bubble sort, I have programmed as follows, but I would like to print every time after operation.What should we do to do so?
For example, if [3,5,1,7] is given,
[3,5,1,7]
[3,1,5,7]
[1,3,5,7]
I would like to extract it

def BubbleSort(num):
for i in range (len(num)):
for jin range (len(num)-1, i, -1):
if num[j]<num[j-1]:
num[j], num[j-1] = num[j-1], num[j]
return num

python

2023-01-21 03:47

1 Answers

def BubbleSort(num):
    print(num)
    for i in range (len(num)) :
        for jin range (len(num)-1, i, -1):
            if num[j]<num[j-1]:
                num[j], num[j-1] = num[j-1], num[j]
                print(num)
    return num

if__name__=='__main__':
    BubbleSort ([3,5,1,7])

# [3, 5, 1, 7]
# [3, 1, 5, 7]
# [1, 3, 5, 7]


2023-01-21 20:49

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.