Question

Lab Overview: In this lab, you will work with merge sort. Objectives: Identify a problem that...

Lab Overview:

In this lab, you will work with merge sort.

Objectives:

  • Identify a problem that can efficiently be solved with merge sort.
  • Implement the merge sort algorithm
  • Determine and summarize the algorithmic run-time of the merge sort algorithm.

Specifications:

As you saw from your readings and exercises, Merge Sort is based upon the idea of divide-and-conquer. If implemented correctly, your merge sort algorithm should have a time complexity of O(n*log(n)).

Your task for this lab is to write a program that utilizes the merge sort algorithm. You can identify any problem that you wish (or make up a hypothetical problem), but the sorting algorithm that you must use to sort the data is merge sort. It is up to you ( your choice ) as to how you want to utilize merge sort within your program.

Once you have finished, write a small summary to describe the algorithmic time complexity of merge sort.

Example:

(If you're having a hard time coming up with your own idea, then you may use my example problem below.)

Mrs. Wh has 31 students take an exam. She is presented with 31 decimal values which represent exam scores. The scores are stored in an array. She would like you to write a program which will sort those scores into ascending order (using merge sort).

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

ANSWER:-

#include <iostream>
using namespace std;
//merge the two half into a sorted data.
void merge(int *arr, int l, int h, int mid){
   int i, j, k, temp_arr[h-l+1];
   i = l;
   k = 0;
   j = mid + 1;

   // Merge the two parts into temp[].
   while (i <= mid && j <= h)
   {
       if (arr[i] < arr[j])
       {
           temp_arr[k] = arr[i];
           k++;i++;
       }
       else
       {
           temp_arr[k] = arr[j];
           k++;j++;
       }
   }
   while (i <= mid)
   {
       temp_arr[k] = arr[i];
       k++;i++;
   }
   while (j <= h)
   {
       temp_arr[k] = arr[j];
       k++;j++;
   }
   for (i = l; i <= h; i++)
   {
       arr[i] = temp_arr[i-l];
   }
}

// divide array into two part
void mergeSort(int *arr, int l, int h)
{
   int mid;
   if (l < h){
       mid=(l+h)/2;
       mergeSort(arr,l,mid);
       mergeSort(arr,mid+1,h);
       merge(arr,l,h,mid);
   }
}

int main()
{
   int len,i;
   cout<<"\nEnter no of scores: ";
   cin>>len;

   int a[len];
   for(i=0;i<len;i++)
   {
       cout<<"Enter element: ";
       cin>>a[i];
   }

   mergeSort(a, 0, len-1);
   cout<<"Sorted Data: "<<endl;
   for (i = 0; i < len; i++)
cout<<a[i]<<endl;

   return 0;
}

SUMMARY OF TIME COMPLEXITY: - Time complexity of merge sort is O(n* logn)

NOTE: - I have given random numbers. You can test with your values. It works well. If you have any doubts please comment below. THANK YOU!!! Please like my answer.

OUTPUT:-

Add a comment
Know the answer?
Add Answer to:
Lab Overview: In this lab, you will work with merge sort. Objectives: Identify a problem that...
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
  • 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...

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

  • A. What is the time complexity of Merge Sort? B. Explain why Merge Sort has the...

    A. What is the time complexity of Merge Sort? B. Explain why Merge Sort has the time complexity you listed above in Part A. Algorithm Quicksort (A, left, right) if (left < right) pivot Point = [(left+right)/2] 11 note central pivot i left - 1 ja right + 1 do do it i +1 while (i < A.size) and (A[i] SA[pivot Point]) do jj-1 while (j > i) and (A[il > A[pivot Point]) if (i <j) then swap (A[i], Aljl)...

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

  • Write a MIPS assembly language for sorting an array of integers using non-recursive bottom-up merge sort...

    Write a MIPS assembly language for sorting an array of integers using non-recursive bottom-up merge sort algorithm. Your program should print the processed array after each step of the merge sort. For example, if the input array is 14 27 13 11 49 63 17 9, your program should print each sort process: Input Arra;y 14 27 13 11 49 63 17 9 Print After first Iteration 14 27 11 13 49 639 17 Print After second iteration 11 13...

  • In this assignment you will implement merge-sort algorithm by creating 2 threads instead of 2 pro...

    In this assignment you will implement merge-sort algorithm by creating 2 threads instead of 2 processes. First half of the array will be sorted by thread 1 and the second half by thread 2. When the threads complete their tasks, the main program will merge the half arrays. You should not waste your time on merge-sort algorithm. Use the merge sort algorithm given below Languaga is C platform Ubuntu

  • Create MIPS program that sorts positive numbers. Inputs the numbers until a zero is inputted and the program displays the sorted numbers. Can use any sorting algorithm except bubble sort.

    Write a MIPS assembly program that sorts a sequence of positive integersentered from the console (one number in one line). The end of thesequence is indicated by a 0 (zero). Verify that the program is correct bysimulation. You can use any sorting algorithm in your code except bubblesort, such as selection sort, merge sort, quick sort, etc. There must be aprocedure call as well as looping in your code. use recursive procedurecalls instead of looping to solve the same problem....

  • Sorting Sort the following array using the quick sort algorithm: (4 Marks) a. 12 26 8...

    Sorting Sort the following array using the quick sort algorithm: (4 Marks) a. 12 26 8 9 7 0 4 Pivot selection is defined to be the first element of each sub-list. Show the array before and after each quicksort round (when the array is partitioned after placing the pivot at its correct position). Also, clearly highlight the pivot in each partition b. Consider an unsorted array of integers of size n. Write a Java program to arrange the array...

  • Problem: Discuss in detail a data structure that on the radix sort algorithm. You will do this in the form of a 4-6 page paper. Your paper should discuss the underlying principle driving the data stru...

    Problem: Discuss in detail a data structure that on the radix sort algorithm. You will do this in the form of a 4-6 page paper. Your paper should discuss the underlying principle driving the data structure, how it works (i.e. how elementary operations are performed/implemented on it), special features if any, and most importantly it's algorithmic analysis (Big-Oh cost) of all the key operations. For the time complexity you must discuss why the cost is O(?) instead of simply reporting...

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

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