Fixed quick sort

This commit is contained in:
K 2023-10-16 19:07:36 +05:30
parent 0ad0dc13b9
commit c14ce1a405
Signed by: notkshitij
GPG Key ID: C5B8BC7530F8F43F

View File

@ -1,6 +1,6 @@
no = int(input("Enter number of students: ")) no = int(input("Enter number of students: "))
list2 = [] list2 = []
#list1 = []
for i in range(0,no): for i in range(0,no):
n = float(input("Enter % of student: ")) n = float(input("Enter % of student: "))
list2.append(n) list2.append(n)
@ -8,31 +8,32 @@ print(list2)
d = len(list2) d = len(list2)
def partition(list3,a,b): def partition(list3, low, high):
v = list3[a] i = low - 1
i = a pivot = list3[high]
j = b-1
temp = 0 for j in range(low, high):
while(i<j): if list3[j] < pivot:
while(list3[i]<=v and i<=j): i += 1
i = i + 1 list3[i], list3[j] = list3[j], list3[i]
while(list3[j] > v):
j = j - 1 list3[i + 1], list3[high] = list3[high], list3[i + 1]
if(i<j): return i + 1
temp = list3[i]
list3[i] = list3[j] def quicksort(list1, low, high):
list3[j] = temp if low < high:
list3[a] = list3[j] pi = partition(list1, low, high)
list3[j] = v quicksort(list1, low, pi - 1)
return j quicksort(list1, pi + 1, high)
no = int(input("Enter number of students: "))
list2 = []
for i in range(0, no):
n = float(input("Enter % of student: "))
list2.append(n)
print("Unsorted list: ", list2)
quicksort(list2, 0, len(list2) - 1)
print("Sorted list: ", list2)
def quicksort(list1, l, u):
j = 0
if(l<u):
j = partition(list1,l,u)
quicksort(list1,l,j - 1)
quicksort(list1,j+1,u)
list2 = list1
return list2
o = quicksort(list2,0,d)
print("Sorted list is: ",o)