BubbleSort с рекурсией в Python3 - возвращает «None» - PullRequest
0 голосов
/ 03 июля 2018

Я создал небольшую функцию для BubbleSort (просто учусь кодировать) в Python 3, и я не могу найти проблему.

Вот код. По какой-то причине он возвращает «None». Может кто-нибудь, пожалуйста, посмотрите? Спасибо!

arr = [1,5,2,7,3]

def bubbleSort(array):
    count = 0
    #print("array is currently",array)
    for idx in range(len(array)-1):
        if array[idx] > array[idx + 1]:
            array[idx],array[idx + 1] = array[idx + 1],array[idx]
            count += 1
            #print("swaped and count is currently",count)
            #print("array is currently",array)
    if count == 0:
        #print("Count is zero")
        #print("array is currently",array)
        return array
    else:
        #print("Count is not zero")
        bubbleSort(array)

print(bubbleSort(arr))

1 Ответ

0 голосов
/ 03 июля 2018

Вам нужно вернуть отсортированный массив

arr = [1,5,2,7,3]

def bubbleSort(array):
    count = 0
    #print("array is currently",array)
    for idx in range(len(array)-1):
        if array[idx] > array[idx + 1]:
            array[idx],array[idx + 1] = array[idx + 1],array[idx]
            count += 1
            #print("swaped and count is currently",count)
            #print("array is currently",array)
    if count == 0:
        #print("Count is zero")
        #print("array is currently",array)
        return array
    else:
        #print("Count is not zero")
        return bubbleSort(array)

print(bubbleSort(arr))
...