Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6808549
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:58:10+00:00 2026-05-26T19:58:10+00:00

I want to take this merge_sort I wrote and put it into a class.

  • 0

I want to take this merge_sort I wrote and put it into a class. Because their is some coupling of the variables, particularly – left, right_begin, right…I want to make these class members. The variables that are local to each function I will leave as local function variables.

Also this function will be used for another class I have called dynamic_array which is a bit more efficient than vector. However I want to make it available to the STL vector as well.

How do I do this. Do I use inheritance? Do I make the class a composite class of my dynamic_array…If I do this it will not be available to vector though.

The Question:
How do I make my merge_sort class interface with the STL container Vector and to my own container dynamic_array.

This is for an interview preparation.

Thank you. Dynamic Array code below(remove old merge_sort cold)

Merge Sort Code – to be converted to a class

#include <iostream>
using namespace std;
const int size=8;
int scratch[size],array[size]={6,5,3,1,8,7,2,4};
void print_array(int arr[]){ for (int i = 0; i < size; i++) cout << arr[i] << endl;}
void flip_if_unordered(int &x, int &y)
  {
  if(array[x]>array[y])
    {
    int tmp = array[x];
    array[x] = array[y];
    array[y] = tmp;
    }
  }
void merge_algo(int &left, int &right_begin, int &right)
  {
  int iter,iter_left=left,iter_right=right_begin;  
  for(iter=left;iter<=right;++iter)
    {
    if( (iter_right>right) || ((iter_left < right_begin) && (array[iter_left]<=array[iter_right])))
      {
      scratch[iter]=array[iter_left];
      ++iter_left;
      }
    else
      {
      scratch[iter]=array[iter_right];
      ++iter_right;
      }
    }
  for(iter=left;iter<=right;++iter){array[iter]=scratch[iter];}
  }
void merge_recurse(int left,int right)
  {
  int left_end=(left+((right-left)/2)); 
  int right_begin=left_end+1;  
  if(((left+1)==right)){flip_if_unordered(left,right);return;}
  else if ((left==right)){return;}
  else
    { 
    merge_recurse(left,left_end);               
    merge_recurse(right_begin,right);  
    merge_algo(left,right_begin,right);
    }   
  }
int main()
  {
  merge_recurse(0,(size-1)); 
  print_array(array);
  return 0;
  }

Dynamic Array

#include "c_arclib.cpp"
using namespace std;
template <class T> class dynamic_array
  {
  private:
    T* array;
    T* scratch;
    void merge_recurse(int left, int right)
      {
      if(right == left + 1)
        {
        return;
        }
      else
        {
        int i = 0;
        int length = right-left;
        int midpoint_distance = length/2;
        int l = left, r = left + midpoint_distance;
        merge_recurse(left, left + midpoint_distance);
        merge_recurse(left + midpoint_distance, right);
        for(i = 0; i < length; i++)
          {
          if((l < (left + midpoint_distance)) && (r == right || array[l] > array[r]))
            {
            scratch[i] = array[l];
            l++;
            }
          else
            {
            scratch[i] = array[r];
            r++;
            }
          }
        for(i = left; i < right; i++)
          {
          array[i] = scratch[i - left];
          }
        }
      }
    void quick_recurse(int left, int right) 
      {  
      int l = left, r = right, tmp;
      int pivot = array[(left + right) / 2];
      while (l <= r)
        {
        while (array[l] < pivot)l++;
        while (array[r] > pivot)r--;
        if (l <= r) 
          {
          tmp = array[l];
          array[l] = array[r];
          array[r] = tmp;
          l++;
          r--;
          }
        }
      if (left < r)quick_recurse(left, r);
      if (l < right)quick_recurse(l, right);
      }  
  public:
    int size;
    void rorder();
    void order();
    void randorder();
    dynamic_array(int sizein)
      {
      size=sizein;
      array=new T[size]();
      }
    void print_operator(std::ostream &os = cout) const
      { 
      for (int i = 0; i < size; i++) os << array[i] << endl; 
      } 
    int merge_sort()
      {
      scratch = new T[size]();
      if(scratch != NULL)
        {
        merge_recurse(0, size);return 1;
        }
      else{return 0;}
      }
    void quick_sort()
      {
      quick_recurse(0,size);
      }
  };
template <class T> void dynamic_array<T>::randorder()
  {
  srand(time(NULL));
  int *ap;
  for(ap=array;ap!=array+size;++ap){*ap=rand();} 
  }
template <class T> void dynamic_array<T>::order()
  {
  int *ap,i=0;
  for(ap=array;ap!=array+size;++ap)                                             
    { 
    *ap=i;
    ++i;        
    } 
  }
template <class T> void dynamic_array<T>::rorder()
  {
  int *ap,i=size;
  for(ap=array;ap!=array+size;++ap)                                             
    { 
    *ap=i;
    --i;        
    } 
  }
template<class T> ostream& operator<<(ostream& stream, dynamic_array<T> const& data) 
  { 
  data.print_operator(stream);
  return stream; 
  }   
int main()
  {
  dynamic_array<int> d1(10);
  d1.order();
  cout << d1;
  /*
  clock_t time_start=clock();
  d1.merge_sort(); 
  clock_t time_end=clock();
  double result = (double)(time_end - time_start) / CLOCKS_PER_SEC; 
  cout << result;
  cout << d1;
  */
  }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T19:58:11+00:00Added an answer on May 26, 2026 at 7:58 pm

    Just make it a template. You need 2 template parameter, the type of the elements, and the type of the container.

    Tipp: if you want to use the class “like a function” define the operator ()

    template<class _Item, class _Container> MergeSort
    {
    
    ...
    
    operator () (_Container & C);
    
    ...
    
    }
    

    now you can use C[] and to access elemets for any class that defines operator [], and you can use the size() function with any class that has it. You only have to add them to your dynamic_array class, and you can use MergeSort<int, vector<int> > with vectors, and MergeSort<int, dynamic_array<int> > with dynamic arrays.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know it is quite subjective, but I also want to take this opportunity
Anyone want to take a stab at why this bookmarklet fails in IE8? It
This should be an absurdly easy task: I want to take each line of
This is probably crazy. I want to take the idea of Dependency Injection to
This is strange. In the news details page, I want to take a few
i want to use stripes in our new project. This will take care of
For this project I'm working on, I want to take multiple excel sheets and
So this might sounds like a n00b question, but I want to take 2
Basically I want to take an image that the user chooses from their photo
I have two array like this and want to merge like Take 1st index

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.