0

i have a program with 5 sorting algorithms (bubble sort, selection sort etc) the user is asked to select which algorithm he wants to use and then is asked to write how many numbers he wants to sort and those numbers are generated randomly, my question is how to print the time each case takes to finish?

Thanks in advance

code below:

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include<string>
#include <ctime>
#include <cstdlib>

using namespace std;

void merge(int[],int,int[],int,int[]);
void bubblemethod(int*,int);
void insertionmethod(int[],int);
void merge_sort(int[],int,int);
void mergemethod(int[],int,int,int);
void selectionmethod(int[],int,int);
int populateArray();

   int size;
   int*array;




int main(int argc, char** argv) {
    int menuOption;





    //int c2, n2;

 // printf("Ten random numbers in [1,100]\n");

  //for (c2 = 1; c2 <= 10; c2++) 
  //{
 //   n2 = rand()%100 + 1;
  //  printf("%d\n", n2);
  //}


    do

    {
    cout << endl << endl << "Select an option from the MENU:" << endl  ;
    cout << "1. Bubble Sort" << endl ;
    cout << "2. Insertion Sort" << endl ;
    cout << "3. Merge Sort" << endl  ;
    cout << "4. Selection Sort" << endl ;
    cout << "5. Quick Sort" << endl ;
    cout << "6. Exit the Program" << endl ;
    cin >> menuOption ;

        switch (menuOption)
        {
            case 1:

                size=populateArray();

                bubblemethod(array,size);



             break;

            case 2:

             size=populateArray();

                insertionmethod(array,size);


            break;
            /*
            case 3:
            cout <<  endl << "Enter the size of the list with integers:" << endl ;
            cin >> size;

            randomnum=size;

             cout<< endl <<"The random numbers in this size are:" << endl;

               for (c1 = 1; c1 <= size; c1++) 
                  {
                      n1 = rand()%size + 1;
                      printf("%d\n", n1);
                      array[c1-1]=n1;
                  }

                  merge_sort(array,c1,randomnum);

                   cout<<"So, the sorted list (using MERGE SORT) will be :"<<endl;
                   cout<<endl<<endl;

                   for(c1=1;c1<=randomnum;c1++)
                    cout<<array[c1-1]<<"    ";



            break;
            case 4:
                cout <<  endl << "Enter the size of the list with integers:" << endl ;
                cin>> size;

                randomnum=size;

                cout<< endl <<"The random numbers in this size are:" << endl;

               for (c1 = 1; c1 <= randomnum; c1++) 
                  {
                      n1 = rand()%size + 1;
                      printf("%d\n", n1);
                      array[c1-1]=n1;
                  }


                selectionmethod(array,c1,randomnum);

                break;
                case 5:

                break;
                case 6:
                cout << endl << "End the program" << endl  ;
                break;
                    */
            default:
                cout << endl << "Error in menu input. Valid menu options are 1 to 6." << endl  ;
        }// end switch


    }   while (menuOption !=6); // end do-while



}


void bubblemethod(int array[],int size)
            {
                int d,swap,c1;

            for (c1 = 0 ; c1 <size; c1++)
             {

                 for (d = 0 ; d < size - 1; d++)
                 {
                     if (array[d] > array[d+1]) /* For decreasing order use < */
                      {
                         swap   = array[d];
                         array[d]   = array[d+1];
                         array[d+1] = swap;
                      }
                 }
             }

            cout<< endl << "Sorted list:\n" << endl;


            for ( c1 = 0 ; c1 <size; c1++ )
            printf("%d\n", array[c1]);



            }

void insertionmethod(int array[],int size)
            {
                int d,t,c1;
                 for (c1 = 1 ; c1 <= size - 1; c1++) 
                 {
                   d = c1;

                   while ( d > 0 && array[d] < array[d-1]) 
                   {
                     t = array[d];
                     array[d]  = array[d-1];
                     array[d-1] = t;

                     d--;
                   }
                 }

                printf("Sorted list:\n");

                for (c1 = 0; c1 <= size - 1; c1++) 
                    printf("%d\n", array[c1]);

            }

            void merge_sort(int array[],int c1,int size)
              {
                  int mid;
                  if(c1<size)
                  {
                      mid=(c1+size)/2;
                      merge_sort(array,c1,mid);
                      merge_sort(array,mid+1,size);
                      mergemethod(array,c1,mid,size);
                  }
              } 

            void mergemethod(int array[],int c1,int mid,int randomnum)
            {
                int h,i,j,b[50],k;
                h=c1;
                i=c1;
                j=mid+1;

                while((h<=mid)&&(j<=randomnum))
                {
                   if(array[h]<=array[j])
                   {
                      b[i]=array[h];
                      h++;
                   }
                   else
                   {
                      b[i]=array[j];
                      j++;
                   }
                   i++;
                }
                if(h>mid)
                {
                   for(k=j;k<=randomnum;k++)
                   {
                      b[i]=array[k];
                      i++;
                   }
                }
                else
                {
                   for(k=h;k<=mid;k++)
                   {
                      b[i]=array[k];
                      i++;
                   }
                }
                for(k=c1;k<=randomnum;k++) 
                     array[k]=b[k];
            }



            void selectionmethod(int array[],int c1,int randomnum)
            {
                int position,d,swap;
                for ( c1 = 0 ; c1 < ( randomnum - 1 ) ; c1++ )
                {
                    position = c1;

                    for ( d = c1 + 1 ; d < randomnum ; d++ )
                    {
                         if ( array[position] > array[d] )
                         position = d;
                    }
                   if ( position != c1 )
                   {
                        swap = array[c1];
                        array[c1] = array[position];
                        array[position] = swap;
                   }
                }

                printf("Sorted list:\n");

                for ( c1 = 0 ; c1 < randomnum ; c1++ )
                printf("%d\n", array[c1]);
            }


int populateArray()
{
    int size;
cout <<  endl << "Enter the size of the list with integers:" << endl ;
                cin >> size;


                  int randomnum, c1, n1;

                 randomnum=size;
                 array=new int[size];

                  cout<< endl <<"The random numbers in this size are:" << endl;

               for (c1 = 1; c1 <= size; c1++) 
                  {
                      n1 = rand()%size + 1;
                      printf("%d\n", n1);
                      array[c1-1]=n1;
                  }

return size;    

}
foobar
  • 2,887
  • 2
  • 30
  • 55
rOcKoN
  • 13
  • 1
  • 4
  • Use the iostreams library to write a formatted string representing your time to the standard output: `std::cout << algorithm_run_time << "\n";` – Kerrek SB Jul 19 '14 at 12:08
  • am sorry but am very new to c++ and i have no idea how to write that – rOcKoN Jul 19 '14 at 12:16
  • [This](http://stackoverflow.com/questions/24468397/a-timer-for-arbitrary-functions) might help you, but if you are very new to C++, it might be a little bit confusing. Change the code from the question like the accepted answer says, then you can just get the time with `get<0>(result_of_timer_function)`. – Baum mit Augen Jul 19 '14 at 12:29
  • you are right about the confusing part, i havent got any timers written on my program yet so am not sure what i am supposed to change? do you want me to copy my code here so you can get an idea? again thank you very much for trying to help me! – rOcKoN Jul 19 '14 at 12:35

2 Answers2

1
#include<time.h>
#include<iostream.h>

int main(){
    time_t t = clock();

    //Code goes here

    t = clock() - t;
    double time_taken = ((double)t)/CLOCKS_PER_SEC;
    cout<<time_taken;
return 0;
}

this should help.

uSeemSurprised
  • 1,826
  • 2
  • 15
  • 18
  • yeah it does help, i copy this code in my main as it is? cause i already have this int main(int argc, char** argv) { it looks like if i dont it will cause conflicts and what goes in the "//code goes here" comment? thank you! – rOcKoN Jul 19 '14 at 12:44
  • You can place your code here for which you want to calculate the time for. – uSeemSurprised Jul 19 '14 at 12:48
  • i cant post the code i try to edit my original post but half of it doesnt make it as a code and it wont let me post – rOcKoN Jul 19 '14 at 13:02
  • You can add the timer.h header file to your library. Then create variables: double start, end; call, start = get_time(); /.. your code ../ end = get_time(); printf("Time Taken", end - start); – Juniar Jul 20 '14 at 02:45
0

Edited your code to calculate time for first 2 cases in seconds:

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include<string>
#include <ctime>
#include <cstdlib>

using namespace std;

void merge(int[],int,int[],int,int[]);
void bubblemethod(int*,int);
void insertionmethod(int[],int);
void merge_sort(int[],int,int);
void mergemethod(int[],int,int,int);
void selectionmethod(int[],int,int);
int populateArray();

int size;
int*array;




int main(int argc, char** argv) {
    int menuOption;





    //int c2, n2;

    // printf("Ten random numbers in [1,100]\n");

    //for (c2 = 1; c2 <= 10; c2++) 
    //{
    //   n2 = rand()%100 + 1;
    //  printf("%d\n", n2);
    //}


    do

    {
        cout << endl << endl << "Select an option from the MENU:" << endl  ;
        cout << "1. Bubble Sort" << endl ;
        cout << "2. Insertion Sort" << endl ;
        cout << "3. Merge Sort" << endl  ;
        cout << "4. Selection Sort" << endl ;
        cout << "5. Quick Sort" << endl ;
        cout << "6. Exit the Program" << endl ;
        cin >> menuOption ;

        time_t t1 = 0;
        time_t t2 = 0;
        switch (menuOption)
        {
        case 1:

            size=populateArray();
            t1 = time(0);
            bubblemethod(array,size);
            t2 = time(0);

            break;

        case 2:

            size=populateArray();
             t1 = time(0);
            insertionmethod(array,size);
            t2 = time(0);


            break;
            /*
            case 3:
            cout <<  endl << "Enter the size of the list with integers:" << endl ;
            cin >> size;

            randomnum=size;

            cout<< endl <<"The random numbers in this size are:" << endl;

            for (c1 = 1; c1 <= size; c1++) 
            {
            n1 = rand()%size + 1;
            printf("%d\n", n1);
            array[c1-1]=n1;
            }

            merge_sort(array,c1,randomnum);

            cout<<"So, the sorted list (using MERGE SORT) will be :"<<endl;
            cout<<endl<<endl;

            for(c1=1;c1<=randomnum;c1++)
            cout<<array[c1-1]<<"    ";



            break;
            case 4:
            cout <<  endl << "Enter the size of the list with integers:" << endl ;
            cin>> size;

            randomnum=size;

            cout<< endl <<"The random numbers in this size are:" << endl;

            for (c1 = 1; c1 <= randomnum; c1++) 
            {
            n1 = rand()%size + 1;
            printf("%d\n", n1);
            array[c1-1]=n1;
            }


            selectionmethod(array,c1,randomnum);

            break;
            case 5:

            break;
            case 6:
            cout << endl << "End the program" << endl  ;
            break;
            */
        default:
            cout << endl << "Error in menu input. Valid menu options are 1 to 6." << endl  ;
        }// end switch

        cout <<"time taken = "<<t2-t1<<" seconds";
    }   while (menuOption !=6); // end do-while



}


void bubblemethod(int array[],int size)
{
    int d,swap,c1;

    for (c1 = 0 ; c1 <size; c1++)
    {

        for (d = 0 ; d < size - 1; d++)
        {
            if (array[d] > array[d+1]) /* For decreasing order use < */
            {
                swap   = array[d];
                array[d]   = array[d+1];
                array[d+1] = swap;
            }
        }
    }

    cout<< endl << "Sorted list:\n" << endl;


    for ( c1 = 0 ; c1 <size; c1++ )
        printf("%d\n", array[c1]);



}

void insertionmethod(int array[],int size)
{
    int d,t,c1;
    for (c1 = 1 ; c1 <= size - 1; c1++) 
    {
        d = c1;

        while ( d > 0 && array[d] < array[d-1]) 
        {
            t = array[d];
            array[d]  = array[d-1];
            array[d-1] = t;

            d--;
        }
    }

    printf("Sorted list:\n");

    for (c1 = 0; c1 <= size - 1; c1++) 
        printf("%d\n", array[c1]);

}

void merge_sort(int array[],int c1,int size)
{
    int mid;
    if(c1<size)
    {
        mid=(c1+size)/2;
        merge_sort(array,c1,mid);
        merge_sort(array,mid+1,size);
        mergemethod(array,c1,mid,size);
    }
} 

void mergemethod(int array[],int c1,int mid,int randomnum)
{
    int h,i,j,b[50],k;
    h=c1;
    i=c1;
    j=mid+1;

    while((h<=mid)&&(j<=randomnum))
    {
        if(array[h]<=array[j])
        {
            b[i]=array[h];
            h++;
        }
        else
        {
            b[i]=array[j];
            j++;
        }
        i++;
    }
    if(h>mid)
    {
        for(k=j;k<=randomnum;k++)
        {
            b[i]=array[k];
            i++;
        }
    }
    else
    {
        for(k=h;k<=mid;k++)
        {
            b[i]=array[k];
            i++;
        }
    }
    for(k=c1;k<=randomnum;k++) 
        array[k]=b[k];
}



void selectionmethod(int array[],int c1,int randomnum)
{
    int position,d,swap;
    for ( c1 = 0 ; c1 < ( randomnum - 1 ) ; c1++ )
    {
        position = c1;

        for ( d = c1 + 1 ; d < randomnum ; d++ )
        {
            if ( array[position] > array[d] )
                position = d;
        }
        if ( position != c1 )
        {
            swap = array[c1];
            array[c1] = array[position];
            array[position] = swap;
        }
    }

    printf("Sorted list:\n");

    for ( c1 = 0 ; c1 < randomnum ; c1++ )
        printf("%d\n", array[c1]);
}


int populateArray()
{
    int size;
    cout <<  endl << "Enter the size of the list with integers:" << endl ;
    cin >> size;


    int randomnum, c1, n1;

    randomnum=size;
    array=new int[size];

    cout<< endl <<"The random numbers in this size are:" << endl;

    for (c1 = 1; c1 <= size; c1++) 
    {
        n1 = rand()%size + 1;
        printf("%d\n", n1);
        array[c1-1]=n1;
    }

    return size;    

}
foobar
  • 2,887
  • 2
  • 30
  • 55
  • done! :) again thank you and have a very nice day! if there is anything i can do feel free to send me a pm! – rOcKoN Jul 19 '14 at 14:03