I am new to python so just to get familiar with the syntax, I am making the programs I have already made in C++ and Java.
def swap(p , q):
temp = array[p]
array[p] = array[q]
array[q] = temp
def partition(beg , end):
l = beg
x = array[beg]
for j in range(l+1,end) :
if(array[j] <= x):
l += 1
swap(l , j)
swap(l, beg)
return l
def quick(beg , end):
if(beg <= end):
mid = partition(beg , end)
quick(beg , mid - 1)
quick(mid + 1 , end)
array = []
n=int(input("\nEnter the number of terms: "))
print("\nEnter the terms")
for i in range(0,n):
val = int(input())
array.append(val)
print("\nBefore Sorting: ")
print(array)
quick(0 , n)
print("\nAfter Sorting: ")
print(array)
This is the code that I made in Python for quick sort. It worked in c++ with the same range but it shows the following errors
**
Traceback (most recent call last):
- File "python", line 28, in
- File "python", line 17, in quick
- File "python", line 17, in quick
- File "python", line 17, in quick
- [Previous line repeated 990 more times]
- File "python", line 16, in quick
- File "python", line 8, in partition
- RecursionError: maximum recursion depth exceeded in comparison
Please help. Thank you.
array[a], array[b] = array[b], array[a]partitionfunction always returns(n - 1), since that is the value ofiinreturn irefers to the globali, which is last set in the for loop,for i in range(0,n):, did you meanreturn l? But you really really shouldn't be relying on global state.