Advertisement
MdSadmanSiraj

quicksort_jordan

Sep 28th, 2022
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.25 KB | None | 0 0
  1. import random
  2.  
  3. #Define comparisons counter
  4. class counters:
  5.     def __init__(self):
  6.         self.comparisonsCount = 0
  7.     def incrementComparisonsCount(self):
  8.         self.comparisonsCount += 1
  9.     def getComparisonsCount(self):
  10.         return self.comparisonsCount
  11.  
  12. #Define Partition
  13. def Partition(array, low, high):
  14.     pivot = high                                                                    #set the pivot
  15.     i = low - 1
  16.     for j in range(low, high):                                                      #Process every element other than the pivot
  17.         test.incrementComparisonsCount()                                            #increase comparisons counter
  18.         if array[j] <= array[pivot]:                                                #Check to see if this element belongs on the low side of the array
  19.             i = i + 1                                                               #Create a new slot on the low side
  20.             array[i], array[j] = array[j], array[i]                                 #Add this element to new slot on the low side
  21.     array[i+1], array[high] = array[high], array[i+1]                               #place pivot just to the right of the low side
  22.     return i + 1                                                                    #return the new index of the pivot
  23.  
  24. #Define RandomizedPartition
  25. def RandomizedPartition(array, low, high):
  26.     randPivot = random.randrange(low, high)                                         #the random.randrange function chooses a random item from a specific range. In this case between p and q.
  27.     array[high], array[randPivot] = array[randPivot], array[high]                   #Swap A[high] with with the random pivot
  28.     return Partition(array, low, high)
  29.  
  30. #Define RandomizedQuickSort
  31. def RandomizedQuickSort(array, low, high):
  32.     if low < high:
  33.         pivot = RandomizedPartition(array, low, high)                               #Create a random pivot within the array
  34.         RandomizedQuickSort(array, low, pivot-1)                                    #Sort the left half of the array
  35.         RandomizedQuickSort(array, pivot+1, high)                                   #Sort the right half of the array
  36.  
  37. #Call RandomizedQuickSort
  38. test = counters()
  39. array = [5, 8, 17, 3, 14, 20, 18, 19, 10, 9, 7, 6, 15, 11, 4, 1, 16, 2, 13, 12]
  40. RandomizedQuickSort(array, 0, len(array)-1)
  41.  
  42. #upperbound calculation
  43. n = 20
  44. sum = 0
  45. for i in range(0, n-1):
  46.     for k in range(0, n):                                                           #calculate the value of k for each iteration of n (n = 20)
  47.         if k > 0:
  48.             sum += (2.0/k)
  49. print("Total number of Comparisons:", test.getComparisonsCount())
  50. print("Upperbound:", sum)
  51.  
  52. #The upper bound given in the textbook is defined as the summation of i = 1 through n -1 and the summation of k = 1 through n for 2/k.
  53. #When we compute this bound for n = 20 (because the array has 20 elements) we get an upperbound of 134.81
  54. #The total number of comparisons varies between each run of the program due the randomization of the pivot.
  55. #But the total number of comparisons is typically between 60 and 80 comparisons and it is always less than the upperbound of 134.81.
  56. #So, we can say that the number of comparisons is in agreement with the bound.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement