[英]QuickSort by reading a numerical txt file using Python
I have updated my program, it did not give me an error.我已经更新了我的程序,它没有给我一个错误。 However it gave me the same txt file without sorting
但是它给了我相同的txt文件而没有排序
txt_array.txt 54044 14108 79294 29649 25260 60660 2995 53777 49689 9083 txt_array.txt 54044 14108 79294 29649 25260 60660 2995 53777 49689 9083
#QuickSort with first element to be a pivot element
def choosePivot(arr, l, r):
i = (l - 1) #first index
pivot = arr[l] #first element to be a pivot
for j in range(l+1, r+1):
if arr[j] < pivot:
arr[i], arr[j] = arr[j], arr[i] #swapping
i=i+1 #increment i
position = i-1 #checking position
arr[l], arr[r] = arr[position], arr[l]
return position
def quickSort(arr, l, r):
if len(arr) == 1:
return arr
if l < r:
p = choosePivot(arr, l, r)
quickSort(arr, l, p-1)
quickSort(arr, p+1, r)
def readFile(filename, l):
# f = open(filename, "r")
with open(filename, "r") as f:
for line in f:
l.append(int(line))
if f.closed == False:
f.close()
def main ():
txt_array = []
size = len(txt_array)
readFile("array.txt", txt_array)
print (txt_array[:]) #Appear all numbers of the array
print ("Numbers of given: " + str(len(txt_array)))
quickSort(txt_array, 0,size-1)
print("Sorted array: " + str(txt_array))
return 0
main()
my outpout is我的输出是
[54044, 14108, 79294, 29649, 25260, 60660, 2995, 53777, 49689, 9083] [54044、14108、79294、29649、25260、60660、2995、53777、49689、9083]
Numbers of given: 10给定数量:10
Sorted array: [54044, 14108, 79294, 29649, 25260, 60660, 2995, 53777, 49689, 9083]排序数组:[54044, 14108, 79294, 29649, 25260, 60660, 2995, 53777, 49689, 9083]
Error should specify line number, but I suspect it's:错误应该指定行号,但我怀疑它是:
C,txt_array = quickSort(txt_array, 0,size-1)
In this line you unpack whatever quickSort returned, which might be None in which case it would fail with that error.在这一行中,您解压缩返回的任何 quickSort,这可能是 None 在这种情况下,它将因该错误而失败。
I suppose you get it on the line C, txt_array = quickSort(txt_array, 0, size-1)
, am i right?我想你在
C, txt_array = quickSort(txt_array, 0, size-1)
线上得到它,对吗?
In the line C, txt_array = quickSort(txt_array, 0, size-1)
you are trying to unpack the returned value into to variables C
and txt_array
which throws the error you mention since the function doesn't return any value, meaning it is None
. In the line
C, txt_array = quickSort(txt_array, 0, size-1)
you are trying to unpack the returned value into to variables C
and txt_array
which throws the error you mention since the function doesn't return any value, meaning it is None
。
To solve it : Since the function operates on an array that is in the same scope, it should be enough to replace C, txt_array = quickSort(txt_array, 0, size-1)
with quickSort(txt_array, 0, size-1)
and txt_array
should still be sorted. To solve it : Since the function operates on an array that is in the same scope, it should be enough to replace
C, txt_array = quickSort(txt_array, 0, size-1)
with quickSort(txt_array, 0, size-1)
and txt_array
仍应排序。 (However, i can't answer on what C is expected to be so that you would need to fix separately) (但是,我无法回答 C 的预期值,因此您需要单独修复)
EDIT: in the example below i substituted the code in your quicksort and choosePivot functions against functions from this tutorial and then the principle i explain in my answer below is illustrated.编辑:在下面的示例中,我将您的 quicksort 和 choosePivot 函数中的代码替换为本教程中的函数,然后说明了我在下面的答案中解释的原理。
# ------- START code borrowed from tutorial
def partition(arr, low, high):
i = (low-1)
pivot = arr[high]
for j in range(low, high):
if arr[j] <= pivot:
i = i+1
arr[i], arr[j] = arr[j], arr[i]
arr[i+1], arr[high] = arr[high], arr[i+1]
return (i+1)
def quickSort(arr, low, high):
if len(arr) == 1:
return arr
if low < high:
pi = partition(arr, low, high)
quickSort(arr, low, pi-1)
quickSort(arr, pi+1, high)
# ------- END code borrowed from tutorial
def readFile(filename, l):
with open(filename, "r") as f:
for line in f:
l.append(int(line))
if f.closed == False:
f.close()
def main():
txt_array = []
readFile("array.txt", txt_array)
size = len(txt_array)
print (txt_array[:]) #Appear all numbers of the array
print ("Numbers of given: " + str(len(txt_array)))
quickSort(txt_array, 0, size-1)
print("Sorted array: " + str(txt_array))
main()
This would output这将是 output
[54044, 14108, 79294, 29649, 25260, 60660, 2995, 53777, 49689, 9083]
Numbers of given: 10
Sorted array: [2995, 9083, 14108, 25260, 29649, 49689, 53777, 54044, 60660, 79294]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.