The function quicksort(aList) takes a Python list as an input and sorts the items in ascending order. Assume this implem
Posted: Mon Jun 06, 2022 11:01 am
The function quicksort(aList) takes a Python list as an input
and sorts the items in ascending order. Assume this implementation
is saved in the quickSort.py file.
Please respond to the following questions.
(a) Create a primary program that generates a sorted list of ten
random integers ranging from 0 to 1,000,000. (both inclusive). Sort
using the quicksort function. Variable numlist should be used to
store the sorted list. Assume the main program is in the file
qmain.py, which is located in the same folder as quickSort.py.
Don't forget to import the necessary modules.
(b) Line 13 specifies the pivot to be used for partitioning. The
first number is chosen as the pivot in this implementation. Explain
a flaw in this method of selecting a pivot.
(c) Describe the function of the if statement in Line 7. Is it
possible to remove the if statement?
(d) Calculate the worst-case Big-O time complexity of the two
functions partition and quickSortHelper, given N as the amount of
data in the list.
(e) Tony, your buddy, claims to have discovered a technique
to expedite the process. He recommends the following updated
version of the quickSort function as below.
The other features haven't altered. Study the above-mentioned
modified function. Discuss the benefits and drawbacks of Tony's
adjustment in less than 100 words.
1 import random 2 3 def quickSort (aList): 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 12 ~♡ZHUNBASE 22 23 24 25 26 27 28 29 30 31 quickSortHelper (aList, 0, len(aList)-1) def quickSortHelper (aList, first, last): if first < last: pivot_pos= partition (aList, first, last) quickSortHelper (aList, first, pivot_pos - 1) quickSortHelper (aList, pivot_pos + 1, last) def partition (aList, first, last): pivot_val= aList[first] left = first+1 right = last while left <= right: while left <= right and aList[left]<= pivot_val: left += 1 while right >= left and aList[right] >= pivot_val: right -= 1 #Swap the two elements to complete the partitioning if left right: aList [left], aList[right] = aList[right], aList [left] #put the pivot in the proper position aList[first], aList[right] = aList[right], aList[first] #return the index position of the pivot value return right
def quickSort(aList): list_len len(aList) if list_len <= 1: return True for index in range (len (aList) - 1): if aList[index] > aList[index+1]: break if index == list_len - 2: return True quickSortHelper (aList, 0, len(aList)-1) return False
and sorts the items in ascending order. Assume this implementation
is saved in the quickSort.py file.
Please respond to the following questions.
(a) Create a primary program that generates a sorted list of ten
random integers ranging from 0 to 1,000,000. (both inclusive). Sort
using the quicksort function. Variable numlist should be used to
store the sorted list. Assume the main program is in the file
qmain.py, which is located in the same folder as quickSort.py.
Don't forget to import the necessary modules.
(b) Line 13 specifies the pivot to be used for partitioning. The
first number is chosen as the pivot in this implementation. Explain
a flaw in this method of selecting a pivot.
(c) Describe the function of the if statement in Line 7. Is it
possible to remove the if statement?
(d) Calculate the worst-case Big-O time complexity of the two
functions partition and quickSortHelper, given N as the amount of
data in the list.
(e) Tony, your buddy, claims to have discovered a technique
to expedite the process. He recommends the following updated
version of the quickSort function as below.
The other features haven't altered. Study the above-mentioned
modified function. Discuss the benefits and drawbacks of Tony's
adjustment in less than 100 words.
1 import random 2 3 def quickSort (aList): 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 12 ~♡ZHUNBASE 22 23 24 25 26 27 28 29 30 31 quickSortHelper (aList, 0, len(aList)-1) def quickSortHelper (aList, first, last): if first < last: pivot_pos= partition (aList, first, last) quickSortHelper (aList, first, pivot_pos - 1) quickSortHelper (aList, pivot_pos + 1, last) def partition (aList, first, last): pivot_val= aList[first] left = first+1 right = last while left <= right: while left <= right and aList[left]<= pivot_val: left += 1 while right >= left and aList[right] >= pivot_val: right -= 1 #Swap the two elements to complete the partitioning if left right: aList [left], aList[right] = aList[right], aList [left] #put the pivot in the proper position aList[first], aList[right] = aList[right], aList[first] #return the index position of the pivot value return right
def quickSort(aList): list_len len(aList) if list_len <= 1: return True for index in range (len (aList) - 1): if aList[index] > aList[index+1]: break if index == list_len - 2: return True quickSortHelper (aList, 0, len(aList)-1) return False