Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C++ and Visual C++ > Visual C++
|
Visual C++ Questions specific to Microsoft's Visual C++. For questions not specific to this Microsoft version, use the C++ Programming forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Visual C++ section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 13th, 2004, 11:38 AM
Authorized User
 
Join Date: Jun 2004
Posts: 99
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to hlchuah77
Default Pls help me (mergesort problem)

I have one problem which I really can't solved after two weeks try. The questions are as below:

1. Write a program in C++ using any computer system. The program is required to implement recursion version of mergesort algorithm on an array of integers(numeric data). Add a counter in appropriate location of the implementation to count the number of times major operations (comparison, data moves)are performed in the algorithm. Run the program using various experimental dataset and try to find and run the worst case, average case, and best case (if any).

Using the coding below for this question, I just don't know where to put the counter so to get the result.

#include<iostream>

using namespace std;

const int size=6;

void mergesort(int [],int,int);
void merge(int [],int,int,int);


void main()
{
    int number[size];
    int i;
    for(i=0;i<size;i++)
    {
        cout<<"enter the number "<<i<<":";
        cin>>number[i];
    }

    mergesort(number,0,size-1);
    cout<<"\n\nList of sorted array : ";
    for(i=0;i<size;i++)
        cout<<number[i]<<"\t";

}

void mergesort (int Array[size], int first, int last)
{
    int mid;


        if(first<last)
        {
            mid=(first+last)/2;
            mergesort(Array,first,mid);
            mergesort(Array,mid+1,last);
            merge(Array,first,mid,last);
        }
}

void merge(int Array[size], int first, int mid, int last)
{
    int temp[size];
    int first1=first;
    int last1=mid;
    int first2=mid+1;
    int last2=last;
    int index=first1;

    for(; (first1<=last1) && (first2<=last2); ++index)
    {
        if (Array[first1]<Array[first2])
        {
            temp[index]=Array[first1];
            ++first1;
        }
        else
        {
            temp[index]=Array[first2];
            ++first2;
        }
    }

    for (; first1<=last1;++first1,++index)
        temp[index]=Array[first1];

    for (; first2<=last2; ++first2, ++index)
        temp[index]=Array[first2];

    for(index=first; index<=last;++index)
        Array[index]=temp[index];

}


2 Write a program in C++ using any computer system. The program is required to implement iterative version of mergesort algorithm on an array of integers(numeric data). Add a counter in appropriate location of the implementation to count the number of times major operations (comparison, data moves)are performed in the algorithm. Run the program using various experimental dataset and try to find and run the worst case, average case, and best case (if any).

The same, consider the coding below:

// file msort.h
// merge sort

#ifndef MergeSort_
#define MergeSort_

template<class T>
void Merge(T c[], T d[], int l, int m, int r)
{// Merge c[l:m]] and c[m:r] to d[l:r].
   int i = l, // cursor for first segment
       j = m+1, // cursor for second
       k = l; // cursor for result

   // merge until i or j exits its segment
   while ((i <= m) && (j <= r))
      if (c[i] <= c[j]) d[k++] = c[i++];
      else d[k++] = c[j++];

   // take care of left overs
   if (i > m) for (int q = j; q <= r; q++)
                 d[k++] = c[q];
   else for (int q = i; q <= m; q++)
           d[k++] = c[q];
}

template<class T>
void MergePass(T x[], T y[], int s, int n)
{// Merge adjacent segments of size s.
   int i = 0;
   while (i <= n - 2 * s) {
      // merge two adjacent segments of size s
      Merge(x, y, i, i+s-1, i+2*s-1);
      i = i + 2 * s;
      }

   // fewer than 2s elements remain
   if (i + s < n) Merge(x, y, i, i+s-1, n-1);
   else for (int j = i; j <= n-1; j++)
           // copy last segment to y
           y[j] = x[j];
}

template<class T>
void MergeSort(T a[], int n)
{// Sort a[0:n-1] using merge sort.
   T *b = new T [n];
   int s = 1; // segment size
   while (s < n) {
      MergePass(a, b, s, n); // merge from a to b
      s += s;
      MergePass(b, a, s, n); // merge from b to a
      s += s;
      }
}

#endif


//filename : msort.cpp
// test iterative version of merge sort

#include <iostream.h>
#include "msort.h"

void main(void)
{
   int y[10] = {10,7,8,9,4, 2, 3, 6, 5,1};
   MergeSort(y,10);
   for (int i=0; i< 10; i++) cout << y[i] << ' ';
   cout << endl;
}


For the above coding, there are four different types of dataset to test for the performance. The four different dataset are as examples below:

first set : random array (e.g. 32, 52, 35, 89, 12, 15)
second set : sorted array (e.g. 1, 2, 3, 4, 9, 10)
third set : inverse array (e.g. 101, 98, 50, 32, 10, 5)
fourth set : partial sorted array (e.g. 1, 20, 22, 47, 45, 46)


So, please advise me where to put the counter so to obtain the result, and for the conclusion, I want to know which method (recursion version or iterative version) is getting the better performance. Also, I want to know how many steps are taken (counters' value) for recursion version and iterative version of the algorithm, for example :

(recursion version)
data moves : 18
comparison : 25

(iterative version)
data moves : 20
comparison : 21

The values above are an example value only.

I know that this is a very long question and is not a quite an easy questions, but I really hope that whoever have knowledge in this thing would reply me, thank you.






Similar Threads
Thread Thread Starter Forum Replies Last Post
DateTime Problem!!! Pls help cmualo SQL Server 2000 4 August 17th, 2007 03:08 AM
I myself solved my problem(now u all pls help me i rashmipant BOOK: Beginning PHP5, Apache, and MySQL Web Development ISBN: 978-0-7645-7966-0 0 October 12th, 2006 04:34 AM
upload problem.... pls help life_s Ng ASP.NET 1.0 and 1.1 Basics 7 September 16th, 2003 11:20 PM
update problem again... pls help... life_s Ng ASP.NET 1.0 and 1.1 Basics 1 September 4th, 2003 02:02 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.