I have written the code for quick sort in python, but this code is throwing an error.
我已经在python中编写了快速排序的代码,但是这个代码正在抛出一个错误。
----------
k=0
def partition(arr,low_index,high_index):
key = arr[low_index]
i = low_index + 1;
j = high_index
while True:
while (i<high_index and key>=arr[i]):
i+=1
while (key<arr[j]):
j-=1
if i<j:
arr[i,j] = arr[j,i]
else:
arr[low_index,j]=arr[j,low_index]
return j
def quicksort(arr,low_index,high_index):
if low_index < high_index:
j = partition(low_index,high_index,arr)
print("Pivot element with index "+str(j)+" has thread "+str(k))
if left<j:
k=k+1
quicksort(arr,low_index, j - 1)
if i<right:
k=k+1
quicksort(arr,j+1,high_index)
return arr
n = input("Enter the value n ")
arr=input("Enter the "+str(n)+" no. of elements ")
brr=quicksort(arr,0,n-1)
print("Elements after sorting are "+str(brr))
----------
The error it is throwing is
它抛出的错误是
Enter the value n 4
输入值n4
Enter the 4 no. of elements [5,6,2,7] Traceback (most recent call last): File "C:\Users\devendrabhat\Documents\dev\dev\quick.py", line 38, in brr=quicksort(arr,0,n-1) TypeError: unsupported operand type(s) for -: 'str' and 'int'
进入4号[5,6,2,7] Traceback(最近一次调用):File "C:\Users devendrabhat\Documents\dev\dev\ dev\dev\ dev\dev\ dev\dev\ dev\dev\quick "py",第38行,在brr=quicksort(arr,0,n-1)类型错误:为-:'str'和'int'的不支持操作数类型(s)
3 个解决方案
#1
0
n is string. So you need to change it to int:
n是字符串。所以你需要把它改成int:
n = int(n)
If you input [5,6,2,7] on line 37, python interpret it as string like "[5,6,2,7]". So, you need to convert string to list.
如果您在第37行输入[5,6,2,7],python会将其解释为“[5,6,2,7]”之类的字符串。因此,需要将字符串转换为list。
arr = eval(arr)
#2
1
You need to change n to an integer, not a string. Your error is telling you, that you are trying to perform an operation (- in this case) on a string and an integer. Change str(n)
to int(n)
so you have the same type throughout.
你需要把n变成一个整数,而不是一个字符串。您的错误告诉您,您正在尝试对字符串和整数执行操作(在本例中)。将str(n)更改为int(n),以便始终具有相同的类型。
#3
1
you are declaring 'n' as string there in your code. And trying to perform arithmetic operation with string.
在代码中,你将n声明为字符串。并尝试用字符串进行算术运算。
So it is giving that error. Change this str(n)
to int(n)
.
它给出了这个误差。将这个str(n)改为int(n)
It will work !!!
它将工作! ! !
#1
0
n is string. So you need to change it to int:
n是字符串。所以你需要把它改成int:
n = int(n)
If you input [5,6,2,7] on line 37, python interpret it as string like "[5,6,2,7]". So, you need to convert string to list.
如果您在第37行输入[5,6,2,7],python会将其解释为“[5,6,2,7]”之类的字符串。因此,需要将字符串转换为list。
arr = eval(arr)
#2
1
You need to change n to an integer, not a string. Your error is telling you, that you are trying to perform an operation (- in this case) on a string and an integer. Change str(n)
to int(n)
so you have the same type throughout.
你需要把n变成一个整数,而不是一个字符串。您的错误告诉您,您正在尝试对字符串和整数执行操作(在本例中)。将str(n)更改为int(n),以便始终具有相同的类型。
#3
1
you are declaring 'n' as string there in your code. And trying to perform arithmetic operation with string.
在代码中,你将n声明为字符串。并尝试用字符串进行算术运算。
So it is giving that error. Change this str(n)
to int(n)
.
它给出了这个误差。将这个str(n)改为int(n)
It will work !!!
它将工作! ! !