Question
The code is in python. The file does not have to be the data set from HW2. What I need is a code combing selection sort and merge sort. The 2-1 is what the problem in refers to as Q1.
d) [10 points] Write a code combining both selection sort and mergesort algorithms (NOT insertion and merge sorts) as describ
2-1 Insertion sort on small arrays in merge sort Although merge sort runs in O(n Ign) worst-case time and insertion sort runs
0 0
Add a comment Improve this question Transcribed image text
Answer #1

# Code combining merge sort and selection sort. Here, I am using merge sort on the whole array and using selection sort for arrays with length smaller than a threshold.

def selectionSort(X, l, r):

   for i in range(l,r+1):

     min_index = i

     for j in range(i+1, r+1):

       if X[min_index] > X[j]:

         min_index = j

     X[i], X[min_index] = X[min_index], X[i]

   return X


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

    n1 = m - l + 1

    n2 = r- m

  

    # creating temporary arrays and copying data in them

    temp1 = [0] * (n1)

    temp2 = [0] * (n2)

  

    for i in range(0 , n1):

        temp1[i] = arr[l + i]

  

    for j in range(0 , n2):

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

  

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

    i = 0     # for first subarray

    j = 0     # for second subarray

    k = l     # for merged subarray

  

    while i < n1 and j < n2 :

        if temp1[i] <= temp2[j]:

            arr[k] = temp1[i]

            i += 1

        else:

            arr[k] = temp2[j]

            j += 1

        k += 1

  

    # Copy the remaining elements of L

    while i < n1:

        arr[k] = temp1[i]

        i += 1

        k += 1

  

    # Copy the remaining elements of R

    while j < n2:

        arr[k] = temp2[j]

        j += 1

        k += 1


k = 4 # threshold length, arrays smaller than this will be sorted using selection sort

def mergeSelectionSort(X, l, r):

    if (r - l <= k):

        selectionSort(X, l, r);

    else:

        mid = int((l + r)/2);

        mergeSelectionSort(X, l, mid);

        mergeSelectionSort(X, mid+1, r);

        merge(X, l, mid, r);


arr = [9,8,7,6,5,4,3,2,1]# array to be sorted

mergeSelectionSort(arr, 0, len(arr)-1)

print(arr)


Add a comment
Know the answer?
Add Answer to:
The code is in python. The file does not have to be the data set from...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Insertion sort on small arrays in merge sort Although merge-sort runs in Θ(n log n) worst-case...

    Insertion sort on small arrays in merge sort Although merge-sort runs in Θ(n log n) worst-case time and insertion sort runs in Θ(n 2 ) worst-case time, the constant factors in insertion sort can make it faster in practice for small problem sizes on many machines. Thus, it makes sense to coarsen the leaves of the recursion by using insertion sort within merge sort when subproblems become sufficiently small. Consider a modification to merge sort in which n/k sublists of...

  • . Shell sort is a sorting algorithm similar to insertion sort. Research shell sort and apply...

    . Shell sort is a sorting algorithm similar to insertion sort. Research shell sort and apply that to the following array. Show your work in Detail. [15 points] 45 20 50 10 80 30 60 70 40 90 2. Is Shell sort a stable sorting algorithm? Answer this with an example. [10 points] 3. Apply Merge Sort to sort the following list. Show your work in Detail. [15 Points] 45 20 50 10 80 30 60 70 40 90 4....

  • Consider a variation of Merge sort called 4-way Merge sort. Instead of splitting the array into...

    Consider a variation of Merge sort called 4-way Merge sort. Instead of splitting the array into two parts like Merge sort, 4-way Merge sort splits the array into four parts. 4-way Merge divides the input array into fourths, calls itself for each fourth and then merges the four sorted fourths. a)Implement 4-way Merge sort from Problem 4 to sort an array/vector of integers and name it merge4. Implement the algorithm in the same language you used for the sorting algorithms...

  • Practical 5: Write a program that implements several sorting algorithms, and use it to demonstrate the comparative perfo...

    Practical 5: Write a program that implements several sorting algorithms, and use it to demonstrate the comparative performance of the algorithms for a variety of data sets. Need Help With this Sorting Algorithm task for C++ Base Code for sorting.cpp is given. The header file is not included in this. Help would be much appreciated as I have not started on this due to personal reasons #include <cstdlib> #include <iostream> #include <getopt.h> using namespace std; long compares; // for counting...

  • This program should test the running time of these algorithms: Selection Sort Insertion Sort Bubble Sort...

    This program should test the running time of these algorithms: Selection Sort Insertion Sort Bubble Sort Merge Sort Quick Sort Heap Sort You have access to the implementation of all of these sorting algorithms, and you may use what is provided in your text directly. Be sure to cite the source of these implementations in the header of your program. Please maintain the property that these sorting algorithms sort arrays in ascending order. For this homework, you will write a...

  • Hello, I want to check if my C++ code is correct and follows the requeriments described...

    Hello, I want to check if my C++ code is correct and follows the requeriments described thanks. Requeriments: Assignment Sorting Benchmark each of the sorting methods listed below. Insertion Sort Bubble Sort Selection Sort Heap Sort. Quick Sort. Merge Sort. Benchmark each of the above sorting methods for data sizes of 10000, 20000, 30000, 40000 and 50000. Display the results in a table as shown below. The table should have rows and columns. However, the rows and columns need not...

  • the question from the course COMP 4040 that Analysis of Algorithms if you want to answer it by code please use C or C++...

    the question from the course COMP 4040 that Analysis of Algorithms if you want to answer it by code please use C or C++ 5. Algorithm Design (20 points) Input: array A contains n distinct numbers from 1 to n, in arbitrary order. Output: number of inversions (defined as the number of pair(i, j) of array indices with i < j and A[i] > Aj]) (a) (5 points) What array with elements from the set {1, 2, ..., n) has...

  • Modify the sorts (selection sort, insertion sort, bubble sort, quick sort, and merge sort) by adding code to each to tally the total number of comparisons and total execution time of each algorithm. E...

    Modify the sorts (selection sort, insertion sort, bubble sort, quick sort, and merge sort) by adding code to each to tally the total number of comparisons and total execution time of each algorithm. Execute the sort algorithms against the same list, recording information for the total number of comparisons and total execution time for each algorithm. Try several different lists, including at least one that is already in sorted order. ---------------------------------------------------------------------------------------------------------------- /** * Sorting demonstrates sorting and searching on an...

  • Do the following project: Following is the file to be programmed in Linux kernel. Run this...

    Do the following project: Following is the file to be programmed in Linux kernel. Run this program. Include the screenshot of the results. Multi threaded Sorting Application Write a multithreaded sorting program that works as follows: A list of integers is divided into two smaller lists of equal size. Two separate threads (which we will term sorting threads) sort each sub list using a sorting algorithm of your choice. The two sub lists are then merged by a third thread—a...

  • Objective: GUI Layout manager Download one of the sample GUI layout program. Use any GUI layout...

    Objective: GUI Layout manager Download one of the sample GUI layout program. Use any GUI layout to add buttons to start each sort and display the System.nanoTime in common TextArea panel. The question is a bit confusing so i will try to simplify it. Using the GUI ( I made a unclick able one so you have to make it clickable), allow a user to sort the text file based on what they click on. example: if i click merge...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT