Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Rewrite the Quicksort routine so that it now calculates the Pivot value a couple

ID: 3809988 • Letter: R

Question

Rewrite the Quicksort routine so that it now calculates the Pivot value a couple of different ways. First, make sure you keep the original version. Second, create another version by using the median of three method as we described it in class. Third method that chooses the middle value to use as the pivot. You are going to compare these three methods (for the second two, once you’ve determined the pivot value, switch it to the first position in the list so that you can then proceed with the algorithm as it is outlined in your text.) Once again, use the timing routine, create a driver program which will:

• Generate a random list of 100 numbers from 1000 possible values.

• Generate a random list of 1000 numbers from 10000 possible values.

• Generate a random list of 10000 numbers from 100000 possible values.

You need to show the time outputs in the work that you submit. In other words, once you have the programs running, capture your results and include the results in what you submit. You can use screen shots or you can write the results to a file from your python code. You are to show the results for FIVE sets of data – FIVE lists of 100 random numbers, FIVE lists of 1000 random numbers, etc. Make sure that you save the original list for each iteration so that you are passing the same list to each routines. You do not need to show the list of numbers. Make sure you comment on your conclusions regarding the pivot value selection (first position versus median of three versus middle position.)

QuickSort routine here:

def quickSort(alist):
quickSortHelper(alist,0,len(alist)-1)

def quickSortHelper(alist,first,last):
if first<last:

splitpoint = partition(alist,first,last)

quickSortHelper(alist,first,splitpoint-1)
quickSortHelper(alist,splitpoint+1,last)


def partition(alist,first,last):
pivotvalue = alist[first]

leftmark = first+1
rightmark = last

done = False
while not done:

while leftmark <= rightmark and
alist[leftmark] <= pivotvalue:
leftmark = leftmark + 1

while alist[rightmark] >= pivotvalue and
rightmark >= leftmark:
rightmark = rightmark -1

if rightmark < leftmark:
done = True
else:
temp = alist[leftmark]
alist[leftmark] = alist[rightmark]
alist[rightmark] = temp

temp = alist[first]
alist[first] = alist[rightmark]
alist[rightmark] = temp


return rightmark

Explanation / Answer

def partition(alist, first, last, pivot): alist[pivot], alist[last] = alist[last], alist[pivot] store_index = first for i in xrange(first, last): if alist[i] = last: return alist pivot = randrange(first, last + 1) new_pivot = partition(alist, first, last, pivot) quick_sort(alist, first, new_pivot - 1) quick_sort(alist, new_pivot + 1, last) def sort(alist): quick_sort(alist, 0, len(alist) - 1) return alist
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote