From 16dddda780f56f41643d72390592b5fce1c0620a Mon Sep 17 00:00:00 2001 From: NobinPegaus <56520367+NobinPegaus@users.noreply.github.com> Date: Thu, 2 Jul 2020 00:43:03 +0600 Subject: [PATCH] It's changed for python3 and comments are added --- Programming Assignment 3/QuickSort.py | 31 +++++++++++++++++---------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/Programming Assignment 3/QuickSort.py b/Programming Assignment 3/QuickSort.py index 9746b0b..e287981 100644 --- a/Programming Assignment 3/QuickSort.py +++ b/Programming Assignment 3/QuickSort.py @@ -1,26 +1,31 @@ +#It's written in python3 -with open('QuickSort.txt') as f: +with open('c:/Users/Nabin/Desktop/output.txt') as f: a = [int(x) for x in f] -def FindMedian(A): + +#It's for the 3rd part of the assignment where we are told to use the median of three +def FindMedian(A): minvalue = min(A) maxvalue = max(A) for i in range(3): if A[i] != minvalue and A[i] != maxvalue: return A[i] + +#It's a function which chooses the pivot either the first elem for flag1, the last for flag2 and the median of three for flag3 def ChoosePivot(A,flag): n = len(A) first = A[0] final = A[n-1] - if n/2*2==n: - k = n/2 - 1 + if(n%2==0): + k = n//2 - 1 middle = A[k] - elif n/2*21: p = ChoosePivot(A,flag) - A = Swap(A,0,p) + A = Swap(A,0,p) #places the pivot element in the first position as our partition function works assuming the first element is the pivot. A,pivot_position = Partition(A) A[:pivot_position],left = QuickSort(A[:pivot_position],flag) A[pivot_position+1:],right = QuickSort(A[pivot_position+1:],flag) - return A,left+right+n-1 + return A,left+right+n-1 #for each recursive call atleast n-1 comparison is made and then it works for the left and right sublist. else: return A,0