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

2024-04-25 05:59:26 发布

您现在位置: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()

如果我有几个错误,你可以让我知道在代码。我在打印报表时使用了以下内容


Tags: 代码inlofor排序deflinehi
1条回答
网友
1楼 · 发布于 2024-04-25 05:59:26
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()

相关问题 更多 >