
# 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)

The code is in python. The file does not have to be the data set from...
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...
This question involves comparing the sorting algorithms that we studied so far such as selection sort, bubble sort, insertion sort, merge sort & quicksort. First briefly explain the idea behind each of them. And then compare them according to the criterion such as time (best, worst and average case) and space efficiency, stability, applicability (when performs best) etc. You may use following links as hints, however you will be able to find a significant number of links that compare sorting...
. 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 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 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 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 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...
Your mean employer has five sorting programs. But they are labeled by their descriptions - no names. Read each label then say which of the following it is: Heap Sort Insertion Sort Merge Sort Quick Sort Radix Sort Selection Sort 1) “I always use theta(n^2) comparisons no matter what kind of list I am sorting. I am ashamed.” *Answer: This is 2) "I am a good choice if you want to quickly sort a large number of K digit numeric...
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. 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...