QuickSort用户输入十或八个整数Python

时间:2018-07-27 16:26:39

标签: python

尝试通过递归创建具有用户输入的双重枢纽,尽管可以省略递归,但是一个人如何将输入/整数放入列表中,然后根据所示的快速排序公式进行排序。

def swap(lst, i, j):
    if min(i,j) >= 0 and max(i,j) < len(lst) and i != j:
        lst[i], lst[j] = lst[j], lst[i]

def dpquicksort(lst, left=0, right=None):
    if right is None:
        right = len(lst) - 1     
    if right - left >= 1:
        p = min(lst[left], lst[right])
        q = max(lst[left], lst[right])
        l = left + 1
        g = right - 1
        k = l        
        while k <= g:
            if lst[k] < p:
                swap(lst, k, l)
                l += 1
            elif lst[k] >= q:
                while lst[g] > q and k < g:
                    g -= 1
                swap(lst, k, g)
                g -= 1
                if lst[k] < p:
                    swap(lst, k, l)
                    l += 1
            k += 1
        l -= 1
        g += 1
        swap(lst, left, l)
        swap(lst, right, g)
        dpquicksort(lst, left, l-1)
        dpquicksort(lst, l+1, g-1)
        dpquicksort(lst, g+1, right)
        return right

def quickSortHelper(alist, first, last):
    if first<last:
        splitpoint= partition(alist, first, last)
        quickSortHelper(alist, first, splitpoint-1)
        quickSortHelper(alist, splitpoint+1, last)

def quicksort(lst):
    dpquicksort(lst, 0, len(lst)-1)
    print(lst)
lst = [54,26,93,17,77,31,44,55,20]    
#lst = int(input("enter integers: ")) 
quicksort(lst)
lst = [54,6,93,17,7,1,44,55,20]
#lst = [2, 4, 6, 8, 10, 12, 14, 16, 18]
quicksort(lst)

1 个答案:

答案 0 :(得分:0)

您可以使用内置的map和int,

>>> lst = list(map(int,input("enter integers: ").split()))
enter integers: 2 3 8 1 3 4 1 8 9 2
>>> lst
[2, 3, 8, 1, 3, 4, 1, 8, 9, 2]

或列表理解,

[int(num) for num in input("enter integers: ").split()]

由您决定,放在顶部还是底部都可以。您的代码成为

def swap(lst, i, j):
    if min(i,j) >= 0 and max(i,j) < len(lst) and i != j:
        lst[i], lst[j] = lst[j], lst[i]

def dpquicksort(lst, left=0, right=None):
    if right is None:
        right = len(lst) - 1     
    if right - left >= 1:
        p = min(lst[left], lst[right])
        q = max(lst[left], lst[right])
        l = left + 1
        g = right - 1
        k = l        
        while k <= g:
            if lst[k] < p:
                swap(lst, k, l)
                l += 1
            elif lst[k] >= q:
                while lst[g] > q and k < g:
                    g -= 1
                swap(lst, k, g)
                g -= 1
                if lst[k] < p:
                    swap(lst, k, l)
                    l += 1
            k += 1
        l -= 1
        g += 1
        swap(lst, left, l)
        swap(lst, right, g)
        dpquicksort(lst, left, l-1)
        dpquicksort(lst, l+1, g-1)
        dpquicksort(lst, g+1, right)
        return right

def quickSortHelper(alist, first, last):
    if first<last:
        splitpoint= partition(alist, first, last)
        quickSortHelper(alist, first, splitpoint-1)
        quickSortHelper(alist, splitpoint+1, last)

def quicksort(lst):
    dpquicksort(lst, 0, len(lst)-1)
    print(lst)
lst = list(map(int,input("enter integers: ").split()))
quicksort(lst)