簡體   English   中英

在 Python3 中使用遞歸的 BubbleSort - 返回“無”

[英]BubbleSort with recursion in Python3 - Returning "None"

我在 Python 3 中創建了一個小函數來執行 BubbleSort(只是學習如何編碼),但我找不到問題。

這是代碼。 由於某種原因,它返回“無”。 有人可以看看嗎? 謝謝!

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))

您需要返回排序后的數組

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))

我真的很喜歡@user3065757 的方法! 無論如何,我的代碼是

def rec_bubble_sort(myList):
    flag = 0
    for i in range(len(myList)-1):
        if myList[i] > myList[i+1]:
            myList[i], myList[i+1] = myList[i+1], myList[i]
            flag = 1
    if flag:
        return rec_bubble_sort(myList[0:len(myList)-1])+myList[len(myList)-1:]
    else:
        return myList


myList = [2, 6, 4, 0, 5, 7, 2, 4, 1]
print(rec_bubble_sort(myList))

我的代碼類似於@RohithS98,但我使用過

return rec_bubble_sort(myList[0:len(myList)-1])+myList[len(myList)-1:]

這背后的原因是當函數被調用時,它不會遍歷整個列表。

使用遞歸而不使用任何循環的冒泡排序,

def bubble_sort_recur(a, i, j, n):
    if j == n:
        i = i+1
        j = 0
    if i == n:
        return 
    if a[i] > a[j]:
        temp = a[j]
        a[j] = a[i]
        a[i] = temp
        bubble_sort_recur(a, i, j+1, n);
    else:
        bubble_sort_recur(a, i, j + 1, n);
    return a

a = [1, 12, 3, 4]
a = bubble_sort_recur(a, 0, 0, len(a))
print(a)

代碼行更少的最簡單解決方案:

def bubblesort(l,n):
    for i in range(len(l)-2):
        if l[i] > l[i+1]:
            l[i], l[i+1] = l[i+1], l[i]
    if n>1:        
        bubblesort(l,n-1)   

l = [6,2,9,11,9,3,7,12]
n=len(l)    
bubblesort(l,n)
print(l)
public class RecursionBubbleSort {
    public static void main(String[] args) {
        int[] nums = {4, 3, 2, 1};
        int[] sorted = bubbleSort(nums, 1, nums.length);

        for(int i: sorted){
            System.out.print(i + " ");
        }
    }

    public static int[] bubbleSort(int[] nums, int start, int end){
        if(start >= end) return nums;

        nums = innerSort(nums, start, end);
        
        return bubbleSort(nums, start, end-1);
    }

    public static int[] innerSort(int[] nums, int start, int size){
        if(start >= size) return nums;

        if(nums[start-1] > nums[start]){
            int temp = nums[start];
            nums[start] = nums[start-1];
            nums[start-1] = temp;
        }

        return innerSort(nums, start+1, size);
    }
}
a=[3,6,66,10,12,3,40,5,0,]
k=0
j=len(a)
def sort(a,k,j):
    if a[k]==a[j-1]:
        return a
    for i in range(j):
        try:
            if a[i] > a[i+1]:
                a[i],a[i+1]=a[i+1],a[i] 
        except:
            pass
    j-=1
            
    return sort(a,k,j)
    
print(sort(a,k,j))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM