Question

Looking for python code. Perform a series of benchmarking tests on merge-sort and quick-sort to determine...

Looking for python code. Perform a series of benchmarking tests on merge-sort and quick-sort to determine which one is faster. Your tests should include sequences that are “random” as well as “almost” sorted

Homework Answers

Answer #1

Quick Sort:-

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

Merge Sort:-

def merge(arr, l, m, r):

    n1 = m - l + 1

    n2 = r- m

  

    # create temp arrays

    L = [0] * (n1)

    R = [0] * (n2)

  

    # Copy data to temp arrays L[] and R[]

    for i in range(0 , n1):

        L[i] = arr[l + i]

  

    for j in range(0 , n2):

        R[j] = arr[m + 1 + j]

  

    # Merge the temp arrays back into arr[l..r]

    i = 0     # Initial index of first subarray

    j = 0     # Initial index of second subarray

    k = l     # Initial index of merged subarray

  

    while i < n1 and j < n2 :

        if L[i] <= R[j]:

            arr[k] = L[i]

            i += 1

        else:

            arr[k] = R[j]

            j += 1

        k += 1

    # Copy the remaining elements of L[], if there

    # are any

    while i < n1:

        arr[k] = L[i]

        i += 1

        k += 1

  

    # Copy the remaining elements of R[], if there

    # are any

    while j < n2:

        arr[k] = R[j]

        j += 1

        k += 1

# l is for left index and r is right index of the

# sub-array of arr to be sorted

def mergeSort(arr,l,r):

    if l < r:

        # Same as (l+r)//2, but avoids overflow for

        # large l and h

        m = (l+(r-1))//2

        # Sort first and second halves

        mergeSort(arr, l, m)

        mergeSort(arr, m+1, r)

        merge(arr, l, m, r)

# Driver code to test above

arr = [12, 11, 13, 5, 6, 7]

n = len(arr)

print ("Given array is")

for i in range(n):

    print ("%d" %arr[i]),

mergeSort(arr,0,n-1)

print ("\n\nSorted array is")

for i in range(n):

    print ("%d" %arr[i]),

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
IN JAVA PLEASE This problem requires you to code the Merge part of the Merge Sort...
IN JAVA PLEASE This problem requires you to code the Merge part of the Merge Sort algorithm. The Merge step merges n elements which takes O(n) time. Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Any code that is found to exceed linear time will fail the tests. Example 1: Input: nums1 = [1,2,3,0,0,0], m = 3 nums2 = [2,5,6], n = 3 Output: [1,2,2,3,5,6]
IN JAVA PLEASE This problem requires you to code the Merge part of the Merge Sort...
IN JAVA PLEASE This problem requires you to code the Merge part of the Merge Sort algorithm. The Merge step merges n elements which takes O(n) time. Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Any code that is found to exceed linear time will fail the tests. Example 1: Input: nums1 = [1,2,3,0,0,0], m = 3 nums2 = [2,5,6], n = 3 Output: [1,2,2,3,5,6] import java.util.Scanner; import java.util.ArrayList; public class Solution...
Implement functions for insertion sort, quicksort, heapsort and merge sort that input an array of integers...
Implement functions for insertion sort, quicksort, heapsort and merge sort that input an array of integers and sort it in-place. Write a program that generates random integer arrays (hint: use seed appropriately to avoid generating same sequences) of lengths 10, 100, 1000, 10,000, 100,000, 1000,000, and then sorts each using each of the sorting functions from (a), and measures the time in nanoseconds. The program will repeat this process 30 times and will compute the average execution time for each...
DIRECTIONS: IMPLEMENT QuickSort and MergeSort. You have been provided: 1. Functions to perform Merge (for MergeSort)...
DIRECTIONS: IMPLEMENT QuickSort and MergeSort. You have been provided: 1. Functions to perform Merge (for MergeSort) and Partition (for QuickSort). 2. In class we discussed the body of the recursive functions, the "brakes" needed to stop the recursion. You are expected to develop 2 RECURSIVE programs: 1. Complete the bodies of the sort functions, MergeSort and QuickSort. 2. Complate the main that tests each function. - program should not read inputs - stock the program with multiple arrays (test cases)...
The following code to run as the described program on python. The extra test file isn't...
The following code to run as the described program on python. The extra test file isn't included here, assume it is a text file named "TXT" with a series of numbers for this purpose. In this lab you will need to take a test file Your program must include: Write a python program to open the test file provided. You will read in the numbers contained in the file and provide a total for the numbers as well as the...
Part 1: Benchmarking Sorting Algorithms The same task can take vastly different amounts of time, depending...
Part 1: Benchmarking Sorting Algorithms The same task can take vastly different amounts of time, depending on the algorithm that is used to perform the task. You are familiar with simple sorting algorithms such as insertion sort and selection sort. (See Section 7.4 in the textbook.) While these methods work fine for small arrays, for larger arrays they can take an unreasonable amount of time. The question is whether we can do any better. Java has some built-in sorting methods....
Using C++, Python, or Java, write a program that: In this programming exercise you will perform...
Using C++, Python, or Java, write a program that: In this programming exercise you will perform an empirical analysis of the QuickSort algorithm to study the actual average case behavior and compare it to the mathematically predicted behavior. That is, you will write a program that counts the number of comparisons performed by QuickSort on an array of a given size. You will run the program on a large number of arrays of a certain size and determine the average...
CAN YOU PLEASE WRITE THIS CODE IN A DIFFERENT WAY 'EASIER AND BETTER' QUESTION Using C++...
CAN YOU PLEASE WRITE THIS CODE IN A DIFFERENT WAY 'EASIER AND BETTER' QUESTION Using C++ 11. Write a function that will merge the contents of two sorted (ascending order) arrays of type double values, storing the result in an array out- put parameter (still in ascending order). The function shouldn’t assume that both its input parameter arrays are the same length but can assume First array 04 Second array Result array that one array doesn’t contain two copies of...
(a) Write an algorithm (use pseudo-code) to determine whether a function f ∶ Z100 → Z100...
(a) Write an algorithm (use pseudo-code) to determine whether a function f ∶ Z100 → Z100 is surjective. That is, supply a “Method” to go with Input: A function (array) f with f(i) ∈ Z100 for i = 0, 1, . . . , 99. Output: Boolean B. B=‘true’ if f is surjective, ‘false’ otherwise. Try to make your algorithm as efficient as possible. Do NOT include an implementation of your algorithm in a programming language. (b) How many comparisons...
Use the list functions learned in class to perform a series of operations and display the...
Use the list functions learned in class to perform a series of operations and display the output using the following list – [5, 15, 84, 3, 14, 2, 8, 10, 14, 25]. Specifications are as follows: (a) Print a count of the number of times 14 is in the list. (worth 2 pts) (b) Print the maximum number in the list. (worth 2 pts) (c) Print the minimum number in the list. (worth 2 pts) (d) Print the numbers in...