subject

Modify the quicksort function (found in the file testquicksort. py), so that it calls insertionSort (found in the file algorithms. py) to sort any sublist whose size is less than 50 items. Compare the performance of this version with that of the original one, using data sets of 50, 500, and 5000 items. Then, adjust the threshold for using the insertion sort to determine an optimal setting File: testquicksort. py
Tests the quicksort algorithm
def quicksort(lyst):
quicksortHelper(lyst, 0, len(lyst) - 1)
def quicksortHelper(lyst, left, right):
if left < right:
pivotLocation = partition(lyst, left, right)
quicksortHelper(lyst, left, pivotLocation - 1)
quicksortHelper(lyst, pivotLocation + 1, right)
def partition(lyst, left, right):
# Find the pivot and exchange it with the last item
middle = (left + right) // 2
pivot = lyst[middle]
lyst[middle] = lyst[right]
lyst[right] = pivot
# Set boundary point to first position
boundary = left
# Move items less than pivot to the left
for index in range(left, right):
if lyst[index] < pivot:
swap(lyst, index, boundary)
boundary += 1
# Exchange the pivot item and the boundary item
swap (lyst, right, boundary)
return boundary
def swap(lyst, i, j):
"""Exchanges the items at positions i and j."""
# You could say lyst[i], lyst[j] = lyst[j], lyst[i]
# but the following code shows what is really going on
temp = lyst[i]
lyst[i] = lyst[j]
lyst[j] = temp
import random
def main(size = 20, sort = quicksort):
lyst = []
for count in range(size):
lyst. append(random. randint(1, size + 1))
print(lyst)
sort(lyst)
print(lyst)
if __name__ == "__main__":
main()

ansver
Answers: 3

Another question on Computers and Technology

question
Computers and Technology, 22.06.2019 05:10
Suppose we have a byte addressable computer that has a 32-byte cache with 8 bytes per block. the memory address is 8 bits long. the system accesses memory addresses (in hex) in this exact order: 6e, b9, 17, e0, 4e, 4f, 50, 91, a8, ab, ad, 93, and 94. (a) assuming the cache is direct mapped, what memory addresses will be in cache block 2 after the last address has been accessed? (b) assuming the cache is direct mapped, what is the hit ratio for the entire memory reference sequence given, assuming the cache is initially empty? (c) assuming the cache is 2-way set associative with a lru replacement policy, what is the hit ratio?
Answers: 3
question
Computers and Technology, 23.06.2019 18:30
Janice recently received her college degree and is looking for a job. she is worried that since she just finished school, she will be required to repay her perkins and direct subsidized loans immediately. janice pulls out the paperwork she signed and reviews it again for repayment information. after reading all of the information, janice discovers that
Answers: 2
question
Computers and Technology, 25.06.2019 06:50
A1-megabit computer memory chip contains many 27 ff capacitors. each capacitor has a plate area of 3.09 × 10−11 m 2 . determine the plate separation of such a capacitor (assume an empty parallel-plate configuration). the characteristic atomic diameter is 10−10 m = 1 ˚a, and the permittivity of a vacuum is 8.8542 × 10−12 c 2 /n · m2 . answer in units of ˚a.
Answers: 3
question
Computers and Technology, 25.06.2019 08:00
The bus width and bus speed together determine the bus's or bandwidth; that is, the amount of data that can be transferred via the bus in a given period of time.
Answers: 1
You know the right answer?
Modify the quicksort function (found in the file testquicksort. py), so that it calls insertionSort...
Questions
question
History, 26.03.2021 05:00
Questions on the website: 13722367