Question

USING C++ write a program that creates a modified version of Merge sort in which insertion...

USING C++ write a program that creates a modified version of Merge sort in which insertion sort will be used for merging array elements.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>
using namespace std;

void insertion_sort(int array[],int n){
int t,j;
for(int i=0; i<n; i++){
t = array[i];
j = i-1;
while((t<array[j]) && j>=0){
array[j+1] = array[j];
j--;
}
array[j+1] = t;
}
}
//function to merge two sub-arrays
void merge(int array[], int l, int m, int r){
int i, j, k;
int n1 = m - l + 1;
int n2 = r - m;
//create two temporary arrays
int L[n1], R[n2];
//copy the contents in temporary arrays
for (i = 0; i < n1; i++)
L[i] = array[l + i];
for (j = 0; j < n2; j++)
R[j] = array[m + 1+ j];

i = 0;
j = 0;
k = l;
while (i < n1 && j < n2){
if (L[i] <= R[j]){
array[k] = L[i];
i++;
}
else{
array[k] = R[j];
j++;
}
k++;
}
//if there are any remaining elements of L[], copy then
while (i < n1){
array[k] = L[i];
i++;
k++;
}
//if there are any remaining elements of R[], copy then
while (j < n2){
array[k] = R[j];
j++;
k++;
}
}
//function mergesort
void Merge_Sort(int array[], int l, int r){
if (l < r){

int m = l+(r-l)/2;
// Sort first and second halves
Merge_Sort(array, l, m);
Merge_Sort(array, m+1, r);
  
merge(array, l, m, r);
}
}
//function display to print the contents of the array
void display(int array[], int n){
int i;
for (i=0; i < n; i++)
cout << array[i] <<" ";
cout << endl;
}

int main(){
int n, array[30];
cout<<"Enter the number of elements";
cin>>n;
cout<<"\nEnter elements of array:";
for(int i=0;i<n;i++)
{
cin>>array[i];
}
cout << "\nOriginal array : \n";
display(array, n);
insertion_sort(array, n);
cout << "Sorted array: \n";
display(array, n);
cout << "\n";
}

Output:-

Enter the number of elements 10

Enter elements of array: 10
 23
 21
 12
 34
 16
 2
 7
 1
 19

Original array : 
10 23 21 12 34 16 2 7 1 19 
Sorted array: 
1 2 7 10 12 16 19 21 23 34 
Add a comment
Know the answer?
Add Answer to:
USING C++ write a program that creates a modified version of Merge sort in which insertion...
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
  • C++ Write a program that can be used to compare Insertion Sort, Merge Sort and Quick...

    C++ Write a program that can be used to compare Insertion Sort, Merge Sort and Quick Sort. Program must: Read an array size from the user, dynamically an array of that size, and fill the array with random numbers Sort the array with the Insertion Sort, MergeSort and QuickSort algorithms studied in class, doing a time-stamp on each sort. Use your program to measure and record the time needed to sort random arrays of size 5000, 50000, and 500000. For...

  • 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...

  • Data Structure using C++ only Write 4 different sorting functions: Selection Sort, Insertion Sort, Merge Sort...

    Data Structure using C++ only Write 4 different sorting functions: Selection Sort, Insertion Sort, Merge Sort and Quick sort. For each sort you may store the numbers in an array or a linked list (this may be different for each sort). Write your program so that it accepts arguments from the command line using argc and argv in the main function call.

  • Java Merge sort algorithm

    Implement MERGE-SORT() algorithm that reads from a file named “inputHW02.txt” a list of double numbers (max = 3,000,000 numbers), sorts those numbers and indicates time consumption. This programming question will address the advantage of using iteration loops over recursive calls as well as using INSERTION-SORT() as a procedure in MERGESORT(). Your program must perform the following actions: 1. Opens the given file name and reads all double numbers. For simplicity, we assume this file only contains numbers and nothing else....

  • Python Merge sort algorithm...

    Implement MERGE-SORT() algorithm that reads from a file named “inputHW02.txt” a list of double numbers (max = 3,000,000 numbers), sorts those numbers and indicates time consumption. This programming question will address the advantage of using iteration loops over recursive calls as well as using INSERTION-SORT() as a procedure in MERGESORT(). Your program must perform the following actions: 1. Opens the given file name and reads all double numbers. For simplicity, we assume this file only contains numbers and nothing else....

  • Program with generic merge sort and binary search method help. The programming language I'm using is...

    Program with generic merge sort and binary search method help. The programming language I'm using is Java. This program should show understanding generic merge sort methods and generic binary search methods in java. The execution should include at least 5 found items including one from the first three items in the sorted array and one from the last three items in the sorted array as well as at least two items not found Create a generic merge sort method that...

  • Write a program to sort an array of characters using INSERTION SORT. Note: You CAN NOT...

    Write a program to sort an array of characters using INSERTION SORT. Note: You CAN NOT use any built-in sorting function. IN JAVA

  • need help!! java eclipse Write a program to implement bubble sort, insertion sort, selection sort, merge...

    need help!! java eclipse Write a program to implement bubble sort, insertion sort, selection sort, merge sort and quick sort (pivot - first index) algorithms. a) Compute the CPU processing time for all the algorithms for varying input sizes as follows: N-10, 103, 10, 10, and 106 b) Use a random number generator to generate the inputs. Obtain the inputs from the following input ranges: 1- 10, 1 -10, 1 - 10, 1 12 10 c) Write down your results...

  • Implement MERGE-SORT() algorithm that reads from a file named “inputHW02.txt” a list of double numbers (max...

    Implement MERGE-SORT() algorithm that reads from a file named “inputHW02.txt” a list of double numbers (max = 3,000,000 numbers), sorts those numbers and indicates time consumption. This programming question will address the advantage of using iteration loops over recursive calls as well as using INSERTION-SORT() as a procedure in MERGESORT(). Your program must perform the following actions: 1. Opens the given file name and reads all double numbers. For simplicity, we assume this file only contains numbers and nothing else....

  • 1. Algorithm write recurrence relation Help? Consider a version of merge sort in which an array...

    1. Algorithm write recurrence relation Help? Consider a version of merge sort in which an array of size n is divided into 5 segments of sizes n/5. Write the recurrence relation for the time complexity and solve it. (Show all your work.)

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