分享
三行代码  ›  专栏  ›  技术社区  ›  Drey Mandani

在哪里插入重试循环语句?

  •  0
  • Drey Mandani  · 技术社区  · 2 天前

    所以我在这里几乎完成了我的代码,用户在他/她未排序的数组列表中输入元素的数量,但留给我的唯一事情就是在这段代码中添加“Doyouwanttoretry”循环语句。

    def bubblesort(array):
    loop1 = 1
    for i in range(len(array)):
        for j in range(0, len(array) - i - 1):
            print('Loop', loop1 ,':')
            if array[j] > array[j+1]:
                (array[j], array[j+1]) = (array[j+1], array[j])
            print(array)
            loop1 = loop1 + 1
    def selectionsort(array, size):
    loop1 = 1;
    for step in range(size):
        min_idx = step
        print('Loop1', loop1, ':')
        for i in range(step + 1, size):
            if array[i] < array[min_idx]:
                min_idx = 1
            (array[step], array[min_idx]) = (array[min_idx], array[step])
            loop1 = loop1 + 1
            print(array)
    def insertionsort(arr):
    for i in range(1, len(arr)):
        key = arr[i]
        j = i-1
        while j >= 0 and key < arr[j]:
            arr[j+1] = arr[j]
            j -= 1
            arr[j+1] = key
    def merge(arr, l, m, r): 
    n1 = m - l + 1
    n2 = r- m 
    
    L = [0] * (n1) 
    R = [0] * (n2) 
    
    for i in range(0 , n1): 
        L[i] = arr[l + i] 
    
    for j in range(0 , n2): 
        R[j] = arr[m + 1 + j]
        
    i = 0     
    j = 0     
    k = l
    while i < n1 and j < n2 : 
        if L[i] <= R[j]: 
            arr[k] = L[i] 
            i += 1
        else: 
            arr[k] = R[j] 
            j += 1
        k += 1
    
    while i < n1: 
        arr[k] = L[i] 
        i += 1
        k += 1
    
    while j < n2: 
        arr[k] = R[j] 
        j += 1
        k += 1
    
    def mergeSort(arr,l,r): 
    if l < r: 
        m = (l+(r-1))//2
    
        mergeSort(arr, l, m) 
        mergeSort(arr, m+1, r) 
        merge(arr, l, m, r) 
    def quicksort(array1):
    if len(array1) <= 1:
        return array1
    else:
        pivot = array1.pop()
    greater_items = []
    lesser_items = []
    for item in array1:
        if item > pivot:
            greater_items.append(item)
        else:
            lesser_items.append(item)
    return quicksort(lesser_items) + [pivot] + quicksort(greater_items)
    def shellsort(array,n):
    loop1 = 1
    for i in range(len(array)):
        print('Loop', loop1)
        interval = n // 2
        while interval > 0:
            print(array)
            for i in range(interval, n):
                temp = array[i]
                j = i
                while j >= interval and array[j - interval] > temp:
                    array[j] = array[j - interval]
                    j -= interval
                array[j] = temp
                interval //= 2
                loop1 += 1
    lst = []
    num_elem = int(input('Enter the number of elements:'))
    for i in range(0, num_elem):
        elem = int(input('Enter your element/s:'))
        lst.append(elem)
    print('Array contains:', lst)
    print('Sorting Menus:')
    print('[A] Bubble Sort')
    print('[B] Insertion Sort')
    print('[C] Merge Sort')
    print('[D] Quick Sort')
    print('[E] Selection Sort')
    print('[F] Shell Sort')
    print('[Q] Quit Program')
    choice = str(input('Enter input:'))
    
    if choice == 'a':
        print('Original unsorted array:', lst)
        bubblesort(lst)
        print('Bubble Sorted Array in Ascending Order:')
        print(lst)
    elif choice == 'b':
        insertionsort(lst)
        print('Insertion Sort in Ascending Order:')
        for i in range(len(lst)):
            print(lst[i], end=',')
    elif choice == 'c':
        n = len(lst)
        print ("Given array is")
        for i in range(n):
            print ("%d" %lst[i]),
        mergeSort(lst,0,n-1)
        print ("\n\nSorted array is")
        for i in range(n):
            print ("%d" %lst[i]),
    elif choice == 'd':
        print('Quick Sorting in Ascending Order:')
        print(quicksort(lst))
    elif choice == 'e':
        print('Original Unsorted Array:')
        print(lst)
        size = len(lst)
        selectionsort(lst, size)
        print('Selection Sorted Array in Ascending Order:')
        print(lst)
    elif choice == 'f':
        print('Unsorted Array:', lst)
        size = len(lst)
        shellsort(lst, size)
        print('Shell Sorted Array in Ascending Order', lst)
    elif choice == 'q':
        print('Exiting Program...')
        exit()
    else:
        print('Invalid Choice! Please retype a proper listing menu.')
    
    
         
    

    我可以在哪里插入retry loop语句,它将使我从用户输入的未排序数组列表的开头开始?

    2 回复  |  直到 2 天前
        1
  •  1
  •   decorator Dust    2 天前

    试试这个

    def bubblesort(array):
        loop1 = 1
        for i in range(len(array)):
            for j in range(0, len(array) - i - 1):
                print('Loop', loop1 ,':')
                if array[j] > array[j+1]:
                    (array[j], array[j+1]) = (array[j+1], array[j])
                print(array)
                loop1 = loop1 + 1
    def selectionsort(array, size):
        loop1 = 1
        for step in range(size):
            min_idx = step
            print('Loop1', loop1, ':')
            for i in range(step + 1, size):
                if array[i] < array[min_idx]:
                    min_idx = 1
                (array[step], array[min_idx]) = (array[min_idx], array[step])
                loop1 = loop1 + 1
                print(array)
    def insertionsort(arr):
        for i in range(1, len(arr)):
            key = arr[i]
            j = i-1
            while j >= 0 and key < arr[j]:
                arr[j+1] = arr[j]
                j -= 1
                arr[j+1] = key
    def merge(arr, l, m, r): 
        n1 = m - l + 1
        n2 = r- m 
    
        L = [0] * (n1) 
        R = [0] * (n2) 
    
        for i in range(0 , n1): 
            L[i] = arr[l + i] 
    
        for j in range(0 , n2): 
            R[j] = arr[m + 1 + j]
            
        i = 0     
        j = 0     
        k = l
        while i < n1 and j < n2 : 
            if L[i] <= R[j]: 
                arr[k] = L[i] 
                i += 1
            else: 
                arr[k] = R[j] 
                j += 1
            k += 1
    
        while i < n1: 
            arr[k] = L[i] 
            i += 1
            k += 1
    
        while j < n2: 
            arr[k] = R[j] 
            j += 1
            k += 1
    
    def mergeSort(arr,l,r): 
        if l < r: 
            m = (l+(r-1))//2
    
            mergeSort(arr, l, m) 
            mergeSort(arr, m+1, r) 
            merge(arr, l, m, r) 
    
    def quicksort(array1):
        if len(array1) <= 1:
            return array1
        else:
            pivot = array1.pop()
        greater_items = []
        lesser_items = []
        for item in array1:
            if item > pivot:
                greater_items.append(item)
            else:
                lesser_items.append(item)
        return quicksort(lesser_items) + [pivot] + quicksort(greater_items)
    
    def shellsort(array,n):
        loop1 = 1
        for i in range(len(array)):
            print('Loop', loop1)
            interval = n // 2
            while interval > 0:
                print(array)
                for i in range(interval, n):
                    temp = array[i]
                    j = i
                    while j >= interval and array[j - interval] > temp:
                        array[j] = array[j - interval]
                        j -= interval
                    array[j] = temp
                    interval //= 2
                    loop1 += 1
    
    def host():                
        lst = []
        num_elem = int(input('Enter the number of elements:'))
        for i in range(0, num_elem):
            elem = int(input('Enter your element/s:'))
            lst.append(elem)
        print('Array contains:', lst)
        print('Sorting Menus:')
        print('[A] Bubble Sort')
        print('[B] Insertion Sort')
        print('[C] Merge Sort')
        print('[D] Quick Sort')
        print('[E] Selection Sort')
        print('[F] Shell Sort')
        print('[Q] Quit Program')
        choice = str(input('Enter input:'))
    
        if choice == 'a':
            print('Original unsorted array:', lst)
            bubblesort(lst)
            print('Bubble Sorted Array in Ascending Order:')
            print(lst)
        elif choice == 'b':
            insertionsort(lst)
            print('Insertion Sort in Ascending Order:')
            for i in range(len(lst)):
                print(lst[i], end=',')
        elif choice == 'c':
            n = len(lst)
            print ("Given array is")
            for i in range(n):
                print ("%d" %lst[i]),
            mergeSort(lst,0,n-1)
            print ("\n\nSorted array is")
            for i in range(n):
                print ("%d" %lst[i]),
        elif choice == 'd':
            print('Quick Sorting in Ascending Order:')
            print(quicksort(lst))
        elif choice == 'e':
            print('Original Unsorted Array:')
            print(lst)
            size = len(lst)
            selectionsort(lst, size)
            print('Selection Sorted Array in Ascending Order:')
            print(lst)
        elif choice == 'f':
            print('Unsorted Array:', lst)
            size = len(lst)
            shellsort(lst, size)
            print('Shell Sorted Array in Ascending Order', lst)
        elif choice == 'q':
            print('Exiting Program...')
            exit()
        else:
            print('Invalid Choice! Please retype a proper listing menu.')
        
        choice = input('Would you like to retry [Y]/[N]')
        if choice.lower()=='y':
            host()
        else:
            print('Exiting Program...')
            exit()
    
    host()
    
    
    
        2
  •  1
  •   Shadowcodder    2 天前

    在赋值之前设置一个while循环 lst = [] :