Question

In: Computer Science

Python Programming Question 1. Implement the median-of-three method for selecting a pivot value as a modification...

Python Programming Question

1. Implement the median-of-three method for selecting a pivot value as a modification to quickSort (name this function

mo3_quickSort). Prepare test cases for your mo3_quickSort .function

QuickSort function:

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

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

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

alist = [54,26,93,17,77,31,44,55,20]
quickSort(alist)
print(alist)

2. Prepare and run an experiment to verify the following hypothesis.

Canonic quickSort is as fast as mo3_quickSort when processing large lists of unsorted integers.

Solutions

Expert Solution

 

"""
Python Program
Quick sort with median of three numbers
"""

def quickSort(L):
quicksorthelp(L, 0, len(L))


def quicksorthelp(L, first, last):
if first < last:
pivot_location = Partition(L, first, last)
quicksorthelp(L, first, pivot_location)
quicksorthelp(L, pivot_location + 1, last)


def medianOfThree(L, first, last):
mid = (first+last-1)//2
a = L[first]
b = L[mid]
c = L[last-1]
if a <= b <= c:
return b, mid
if c <= b <= a:
return b, mid
if a <= c <= b:
return c, last-1
if b <= c <= a:
return c, last-1
return a, first

def Partition(L, first, last):
pivot, pidx = medianOfThree(L, first, last)
L[first], L[pidx] = L[pidx], L[first]
i = first + 1

for j in range(first+1, last, 1):
if (L[j] < pivot):
L[i], L[j] = L[j], L[i]
i += 1

L[first], L[i-1] = L[i-1], L[first]
return i - 1

alist = [54,26,93,17,77,31,44,55,20]
print("Before: ", alist)
quickSort(alist)
print("After: ", alist)


Related Solutions

implement c++ Quicksort using median of 3 as pivot. this also pass comparator as a function...
implement c++ Quicksort using median of 3 as pivot. this also pass comparator as a function param so it can sort vector in increasing or decreasing order based on the comparator passed. the test code uses std::less comparator. #ifndef QSORT_H #define QSORT_H #include #include using namespace std; template T median_of_three(T& a, T& b, T& c, TComparator comparator) { } template size_t partition(vector& vec, TComparator& comparator, size_t low, size_t high) { // TODO: implement. } template void QuickSort(vector& vec, TComparator comparator,size_t...
Question 1 Selecting the programming language is important Group of answer choices It allows a programmer...
Question 1 Selecting the programming language is important Group of answer choices It allows a programmer to pick their favorite language It identifies the best language as the best tool for solving a particular problem It enables only certain programmer to communicate None of these Question 2 High level languages include Group of answer choices Basic, Fortran, Cobol, Pascal, C HTML, XML Java, C++ All of these Question 3 A major ethical concern on the Internet Group of answer choices...
Choose a pivot using the median of 3 technique and show the result after 1 pass...
Choose a pivot using the median of 3 technique and show the result after 1 pass of quicksort (stop just before choosing a 2nd pivot) for the list [ 85, 40,60, 30, 55, 63, 70, 75 ,25]
Java Programming Part 1 (20%) Implement a class with a main method. Using an enhanced for...
Java Programming Part 1 (20%) Implement a class with a main method. Using an enhanced for loop, display each element of this array: String[] names = {"alice", "bob", "carla", "dennis", "earl", "felicia"}; Part 2 (30%) In a new class, implement two methods that will each calculate and return the average of an array of numeric values passed into it. Constraints: your two methods must have the same name one method should accept an array of ints; the other should accept...
C Programming Question: Write a C - program to implement the following three operations: a) Breadth-first...
C Programming Question: Write a C - program to implement the following three operations: a) Breadth-first search using Adjacency List b) Breadth-first search using Adjacency Matrix c) Check whether a given graph is Bipartite using Breadth-first search (adjacency list).​​​​​​ Please take your time, but do submit the correct and full code. Thank you very much.
Repl.it Python Ch 07 #5: Rock, Paper, Scissors Modification Programming Exercise 11 in Chapter 6 asked...
Repl.it Python Ch 07 #5: Rock, Paper, Scissors Modification Programming Exercise 11 in Chapter 6 asked you to design a program that plays the Rock, Paper, Scissors game. In the program, the user enters one of the three strings - “rock”, “paper”, or “scissors” - at the keyboard. Add input validation (with a case-insensitive comparison) to make sure the user enters one of those strings only. I demonstrated the implementation of the program here: https://repl.it/@profeldridge/week06assignment06part2of2#main.py Using the repl.it link above,...
Using Python Implement Recursive Selection Sort with the following recursive method. 1. Find the smallest number...
Using Python Implement Recursive Selection Sort with the following recursive method. 1. Find the smallest number in the list and swaps it with the first number. 2.Ignore the first number and sort the remaining smaller list recursively.
Write a program in python programming language to implement/simulate a finite automaton that accepts (only): odd...
Write a program in python programming language to implement/simulate a finite automaton that accepts (only): odd Binary numbers // 00000000, 0101, 111111, etc. Show: Finite Automaton Definition, Graph, Table
Write a program in python programming language to implement/simulate a finite automaton that accepts (only): unsigned...
Write a program in python programming language to implement/simulate a finite automaton that accepts (only): unsigned integer numbers // 123, 007, 4567890, etc. Show: Finite Automaton Definition, Graph, Table.
Implement Radix Sort using PYTHON programming language. Use one of the two options for the algorithm...
Implement Radix Sort using PYTHON programming language. Use one of the two options for the algorithm to sort the digits: Use Counting Sort or Bucket Sort. • Assume the numbers are maximum 4-digit numbers. • If using Counting Sort, you can see that your digit range is between 0 and 9 ([0…9]). • If using Bucket Sort, you will have ten buckets labeled from 0 to 9. Please add comments and explain every step carefully.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT