Quicksort我需要能够打印快速排序

时间:2018-03-13 16:33:39

标签: python

我有快速排序的以下代码,但我无法编写快速排序和打印功能帮助!我已经多次尝试快速排序数组。如果您有任何建议,请更新评论或代码。

arr = []
arr2=[]
arr6=[]
arr7=[]
arr8=[]
num_lines = 0
Loop_time=(5)

inp = open ("1600.txt","r")
#read line into array
for line in inp.readlines():
    # loop over the elemets, split by whitespace
    for i in line.split():
        # convert to integer and append to the list
        arr.append(int(i))
        for i in range(Loop_time):
            for name in arr:
                arr2 = arr[0:2]
                arr6 = arr[0:2]
                arr7 = arr[0:1]
                arr8 = arr[0:1]

def partition(lo, hi,**kwargs):
    i = (lo-1)
    pivot = kwargs[hi]
    for j in range (lo,hi):
        if kwargs[j] <= pivot:
            i = i+1
            kwargs[i], kwargs[j] = kwargs[j], kwargs[i]
    kwargs[i + 1], kwargs[hi]= kwargs[hi], kwargs[i + 1]
    return (i+1),kwargs
kwargs = [arr2, arr6, arr7, arr8]

def quickSort(lo, hi,**kwargs):
    if lo < hi:
        pi=partition(kwargs, lo, hi)
        quickSort(kwargs, lo, pi - 1)
        quickSort(kwargs, pi + 1, hi)
        quickSort(arr2,arr6,arr7,arr8)

def main():    
    print(arr2)
    print(arr6)
    print(arr7)

if __name__== "__main__":
    main()

如果我有几个错误,你可以在代码中告诉我。我在print语句中使用了以下内容

1 个答案:

答案 0 :(得分:0)

Guys this works sorting one array I need to be able to sort all the other arrays Arr6 Arr7 and Arr8 I have tried *args and this does not work either I changed both the partition and Quicksort I get errors help
import sys,time

arr = []
arr2=[]
arr6=[]
arr7=[]
arr8=[]
num_lines = 0
Loop_time=(5)



inp = open ("1600.txt","r")
#read line into array
for line in inp.readlines():
    # loop over the elemets, split by whitespace
    for i in line.split():
        # convert to integer and append to the list
        arr.append(int(i))


        for name in arr:
            arr2 = arr[0:29]
            arr6 = arr[0:5]
            arr7 = arr[0:10]
            arr8 = arr[0:7]







def partition(arr2, lo, hi):
    i = (lo-1)
    pivot = arr2[hi]
    for j in range (lo,hi):
        if arr2[j] <= pivot:
            i = i+1
            arr2[i], arr2[j] = arr2[j], arr2[i]
    arr2[i + 1], arr2[hi]= arr2[hi], arr2[i + 1]
    return (i+1)

def quickSort(arr2, lo, hi):
    if lo < hi:
        pi=partition(arr2, lo, hi)
        quickSort(arr2, lo, pi - 1)
        quickSort(arr2, pi + 1, hi)
        return arr2



def main():

    SortedList =quickSort(arr2,0,len(arr2)-1)
    print(SortedList)
if __name__== "__main__":
    main()