Is there any other way to make a Bubble Sort without defining it?

3

In Python3, when you search how to make a bubble sort, find something like this

def bubbleSort(alist):

    for passnum in range(len(alist)-1,0,-1):
        for i in range(passnum):
            if alist[i]>alist[i+1]:
                temp = alist[i]
                alist[i] = alist[i+1]
                alist[i+1] = temp

alist = [54,26,93,17,77,31,44,55,20]

bubbleSort(alist)

print(alist)

I was wondering if there is any way to do the bubble sort without having to define it as a function, without the "def bubbleSort (alist):"

    
asked by Iam Gallano 31.07.2018 в 19:26
source

1 answer

1

If what you want is to make a simple program without having to define a function, simply replace the call to said function with the same code. Something like this:

alist = [54,26,93,17,77,31,44,55,20]

for passnum in range(len(alist)-1,0,-1):
        for i in range(passnum):
            if alist[i]>alist[i+1]:
                temp = alist[i]
                alist[i] = alist[i+1]
                alist[i+1] = temp

print(alist)
    
answered by 31.07.2018 / 19:29
source