Question

Write a mergesort template function that returns a sorted vector <T> c++ merge sort vector

Write a mergesort template function that returns a sorted vector <T>

c++ merge sort vector

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

Hopefully this will clear all your doubts. If you still face any query, let me know in the comment section.Thank You.

Below is the code of the program and the snapshot of the output.

Program code :-

/* Merge Sorting using template function in c++ */
#include<iostream>
using namespace std;

/* Preprocessor processing the array*/
#define get_size(array) (sizeof((array))/sizeof((array[0])))

template<class t>
class sort {
   t *list;
   public:
       sort(t *data, int size) {
           list = data;

           /* Sort data using merge sort */
           merge(0,size-1);
       }

       void merge(int low, int high) {
           int mid;
           if(low < high) {
               mid = (low + high) /2;
               merge(low, mid);
               merge(mid + 1, high);
               merge_sort(low, high, mid);
           }
       }

       void merge_sort(int low, int high, int mid) {
           t temp[10];
           int i = low, j, l = low, m = mid + 1;
           while(l <= mid && m <= high) {
               if(list[l] <= list[m]) {
                   temp[i] = list[l];
                   l++;
               }
               else {
                   temp[i] = list[m];
                   m++;
               }
               i++;
           }
           if(l > mid) {
               for(j = m; j <= high; j++) {
                   temp[i] = list[j];
                   i++;
               }
           }
           else {
               for(j = l; j <= mid; j++) {
                   temp[i] = list[j];
                   i++;
               }
           }
           for(j = low; j <= high; j++)
               list[j] = temp[j];
       }
};

int main() {

   /* Sorting integers */
   int idata[] = {9,5,1,4,0,3,7,6,2,8};
   sort<int> isrt(idata, get_size(idata));
   cout << "\nAfter sort -:" << endl;
   for(int i = 0; i < get_size(idata); i++)
       cout << idata[i] << endl;
}

Snapshot :-

Add a comment
Know the answer?
Add Answer to:
Write a mergesort template function that returns a sorted vector <T> c++ merge sort vector
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
  • Find the space complexity of Merge Sort below as a function of n (the length of...

    Find the space complexity of Merge Sort below as a function of n (the length of A). Assume: • The elements of A require (1) space. • Merge takes 2 sorted arrays as input and merges them into one sorted array containing both inputs' elements in (n) space. A (there is no index trickery allowing Al and A2 Note that Al and A2 are independent arrays from to be "in" A; A is not being sorted in place). Merge Sort...

  • Write a function that takes a numerical vector and returns the sorted vector. To detect whether...

    Write a function that takes a numerical vector and returns the sorted vector. To detect whether a vector is sorted, use diff() and all() (or any()). So long as the vector is not sorted, pick two random positions from the vector and swap them. (in matlab program) You must use a while loop to solve this problem. Do not use for loops. Do not use sort(), min(), max(), sortrows(), unique() functions. >> v = sortbyrandomswaps( [39 25 91 71] )...

  • use python Write a function that will sort a given list using merge sort. You must...

    use python Write a function that will sort a given list using merge sort. You must use the merge sort algorithm (but may be recursive or iterative). The function will take a list as an input and return a sorted version of the list (you may assume it will be a list of integers). The method signature must be merge_sort(lst).

  • 4. (15 points) Recall that in merge sort, we partitioned the array into two halves then...

    4. (15 points) Recall that in merge sort, we partitioned the array into two halves then recursively apply merge sort on those two halves. Suppose instead we partitioned the array into three parts. Write a threewayMergeSort(int list) that returns sorted version of list via apply merge sort on three partitions instead of two // ThreeWayMergeSort.java public class ThreeWayMergeSort f public int threewayMergeSort (int [] list) f (a) (10 points) Fill threewayMergeSort methood (b) (2 points) Write the runtime of threeway...

  • HW58.1. Array Merge Sort You've done merge (on Lists), so now it's time to do merge...

    HW58.1. Array Merge Sort You've done merge (on Lists), so now it's time to do merge sort (on arrays). Create a public non-final class named Mergesort that extends Merge. Implement a public static method int[] mergesort(int[] values) that returns the input array of ints sorted in ascending order. You will want this method to be recursive, with the base case being an array with zero or one value. If the passed array is null you should return null. If the...

  • Implement Merge Sort and test it on a small list of 10 random integer values. c++...

    Implement Merge Sort and test it on a small list of 10 random integer values. c++ please template            // merge-sort S void mergeSort(list& S, const C& less) { typedef typename list::iterator Itor;       // sequence of elements int n = S.size(); if (n <= 1) return;                   // already sorted list S1, S2; Itor p = S.begin(); for (int i = 0; i < n / 2; i++) S1.push_back(*p++);   // copy first half to...

  • 1.) Complete the merge method below, which is the merge step of MergeSort. That is, it...

    1.) Complete the merge method below, which is the merge step of MergeSort. That is, it takes two sorted arrays as input and returns a new sorted array that contains all the elements from the two input arrays. Furthermore, it should keep all duplicated values, and it should run in O(n) time, where n is the size of the output array. public static int[] main(int[] a, int[] b) { }

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

  • In this assignment, you sort a list of strings using mergesort and the compareToIgnoreCase method of...

    In this assignment, you sort a list of strings using mergesort and the compareToIgnoreCase method of the String class. The strings are provided as arguments to your main method. In case you haven't yet learned to configure command-line arguments to main in your IDE, now is a good time to learn. Your program should sort the array of strings using mergesort, then print the strings one per line, in order from smallest ("a") to largest ("z"). The name of your...

  • Show that mergesort is a stable algorithm by modifiying the algorithm to sort a list of tuples (first and last names) by...

    Show that mergesort is a stable algorithm by modifiying the algorithm to sort a list of tuples (first and last names) by their second position. (Modify the merge section to break ties) Modify in python def mergesort(mlist): if len(mlist) < 2: return mlist else: mid = len(mlist)//2 return merge( mergesort(mlist[:mid]), mergesort(mlist[mid:])) # merge two sorted lists def merge(left, right): if left == []: return right elif right == []: return left elif left[0] < right[0]: return [left[0]] + merge(left[1:], right)...

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