GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 03-Sep-2007, 08:49
xlonehats xlonehats is offline
New Member
 
Join Date: Sep 2007
Posts: 15
xlonehats is on a distinguished road
Question

sorting algorithms


pliz can anyone help me....
im writing a program which analyses some sorting algorithms....
the output should show the different algorithms used and the average processing time each one of them took.....
i) im a bit confused in using the clock() methods...as most of the time it returns mostly 0sec....im implementing as
start - end.......so i also looked upon Clocks_Per_Sec....i think it shows no. of ticks per second.....umm is it always 1000 ticks per second


ii) the average time taken for each algorithm for the same data set is to be output on a txt file and a graph drawn from it by importing it on MS Excel....?????? i hav no idea how to do that
any ideas would be most welcome
  #2  
Old 03-Sep-2007, 10:21
davis
 
Posts: n/a

Re: sorting algorithms


There is no standard C or C++ way to do what you're trying to do. Each platform offers their own, platform-specific way of using timers to give you the granularity needed to profile code.

If you want to specify the platform, then it may be possible to offer something that will suit your needs and at the level of precision that results in something more meaningful that returning zero for each clock_t.

Considering that you mentioned MS Excel, then we should assume that you are using Windoze? If that is the case, perhaps you'll want to explore:

msdn.microsoft.com


:davis:
  #3  
Old 03-Sep-2007, 21:21
xlonehats xlonehats is offline
New Member
 
Join Date: Sep 2007
Posts: 15
xlonehats is on a distinguished road

Re: sorting algorithms


ok the clock timing is ok now....any suggestion on using the output to estimate the order of the growth of processing times for each sorting algorithms by doing some graphical analysis......
hint -> You can import the text file in Microsoft Excel and plot on graph(s) to estimate the order.

any sample code would be most welcomed
  #4  
Old 04-Sep-2007, 06:31
davis
 
Posts: n/a

Re: sorting algorithms


Quote:
Originally Posted by xlonehats
ok the clock timing is ok now....any suggestion on using the output to estimate the order of the growth of processing times for each sorting algorithms by doing some graphical analysis......
hint -> You can import the text file in Microsoft Excel and plot on graph(s) to estimate the order.

any sample code would be most welcomed


Format your output so that it is a comma separated value file or a tab delimited file. e.g.:

sort type, elements, time
bubble, 100, 0.5489
heap, 100, 0.4210
insertion, 100, 0.4118
merge, 100, 0.4421
quick, 100, 0.4837
selection, 100, 0.4719
shell, 100, 0.4222


...then do it again with 100000 (or whatever) number of elements.


Also, this forum is supposed to be a two-way street. That is, you can tell others what you did to make your "clock" work properly--even provide some code--and others here will find it of benefit, too. Don't be just a "taker."


:davis:
  #5  
Old 04-Sep-2007, 11:07
davis
 
Posts: n/a

Re: sorting algorithms


Here is an example that I ran on Linux.

Output:

Code:
Enter the number of elements: 100 bubble,100,0.000153 heap,100,0.000038 insertion,100,0.000046 merge,100,0.000058 quick,100,0.000034 selection,100,0.000124 shell,100,0.000019 Enter the number of elements: 1000 bubble,1000,0.014926 heap,1000,0.000530 insertion,1000,0.003983 merge,1000,0.000762 quick,1000,0.000455 selection,1000,0.009033 shell,1000,0.000938 Enter the number of elements: 10000 bubble,10000,1.504395 heap,10000,0.006242 insertion,10000,0.381983 merge,10000,0.004501 quick,10000,0.003289 selection,10000,0.330045 shell,10000,0.025075

...imagine if we had opened a file (rather than spewing to stdout) to write each line of output. Then sucking it in with Excel/Calc would be reasonable and could produce something like:




:davis:
  #6  
Old 04-Sep-2007, 19:52
Peter_APIIT Peter_APIIT is offline
Regular Member
 
Join Date: May 2007
Location: Malaysia
Posts: 434
Peter_APIIT is an unknown quantity at this point

Re: sorting algorithms


Please post your program here. Don't become selfish person.
  #7  
Old 06-Sep-2007, 08:15
xlonehats xlonehats is offline
New Member
 
Join Date: Sep 2007
Posts: 15
xlonehats is on a distinguished road

Re: sorting algorithms


thanx very much...the importing works now
jst another thing.....i want to find the average time taken for each set of data input.....a friend has suggested that i should loop that particular sorting code and get the average....

start clock
for(int i=0;i<5;i++)
merge_sort(............)
end clock
then divide

im not sure whether it would give me an accurate average because after the
first loop the data in my array is already sorted...!!!!! any ideas

in my array i use random no. till 10000 or higher to get a decent time
i can get average simply by generating another set of same no. of data in another array and sorting it...and then calculating average but this would mean a lot of repeating codes.....any ideas
  #8  
Old 06-Sep-2007, 15:17
davis
 
Posts: n/a

Re: sorting algorithms


Quote:
Originally Posted by xlonehats
thanx very much...the importing works now
jst another thing.....i want to find the average time taken for each set of data input.....a friend has suggested that i should loop that particular sorting code and get the average....

start clock
for(int i=0;i<5;i++)
merge_sort(............)
end clock
then divide

im not sure whether it would give me an accurate average because after the
first loop the data in my array is already sorted...!!!!! any ideas

in my array i use random no. till 10000 or higher to get a decent time
i can get average simply by generating another set of same no. of data in another array and sorting it...and then calculating average but this would mean a lot of repeating codes.....any ideas


I used the same array for each test in my own coded effort...and made a copy before invoking each sorting algorithm. I did not include the cost of copying in my timing. This allowed me to use the "same unsorted" array over and over again.

My code, which is nothing more than a quick hack, initializes an array of some user-specified size to the counting numbers of 1 to array elements + 1. Then I used std::random_shuffle to "mix up" the content of the array. I created interfaces to the sorting algorithms that required each of the arrays to be const. This was to ensure that my "original, mixed-up" array wasn't mutated by the individual sorting algorithms.

In order for these sorting algorithms to work properly, since they each mutate the contents of the array, I made a copy of the array and sorted it rather than the original array. I only timed the time that it takes to make the call (and return from it) to the particular sorting algo.

Here is the code that I used. You'll note that the "PerfTimer" class is yet another quickly hacked together example of producing some performance timing. Note that you can easily reimplement its interfaces using Win32-specific functionality and test on Windoze.

perf.cpp

CPP / C++ / C Code:
#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>

#include <cstring>

using namespace std;

#include <sorting_algorithms.h>
#include <PerfTimer.h>

void spew_array( int const* array, const int array_size )
{
    for( int i = 0; i < array_size; i++ )
    {
        cout << array[i] << endl;
    }
}

float test_bubble_sort( int const* array, int array_size )
{
    int* copy_of_array = new int[array_size];
    memcpy( copy_of_array, array, array_size * sizeof( int ) );
    PerfTimer* pt = PerfTimer::instance();
    pt->start();
    bubble_sort( copy_of_array, array_size );
    float f = pt->stop();
    delete [] copy_of_array;
    return f;
}

float test_heap_sort( int const* array, int array_size )
{
    int* copy_of_array = new int[array_size];
    memcpy( copy_of_array, array, array_size * sizeof( int ) );
    PerfTimer* pt = PerfTimer::instance();
    pt->start();
    heap_sort( copy_of_array, array_size );
    float f = pt->stop();
    delete [] copy_of_array;
    return f;
}

float test_insertion_sort( int const* array, int array_size )
{
    int* copy_of_array = new int[array_size];
    memcpy( copy_of_array, array, array_size * sizeof( int ) );
    PerfTimer* pt = PerfTimer::instance();
    pt->start();
    insertion_sort( copy_of_array, array_size );
    float f = pt->stop();
    delete [] copy_of_array;
    return f;
}

float test_merge_sort( int const* array, int array_size )
{
    int* copy_of_array = new int[array_size];
    memcpy( copy_of_array, array, array_size * sizeof( int ) );
    int* temp_array = new int[array_size];
    PerfTimer* pt = PerfTimer::instance();
    pt->start();
    merge_sort( copy_of_array, temp_array, array_size );
    float f = pt->stop();
    delete [] copy_of_array;
    delete [] temp_array;
    return f;
}

float test_quick_sort( int const* array, int array_size )
{
    int* copy_of_array = new int[array_size];
    memcpy( copy_of_array, array, array_size * sizeof( int ) );
    PerfTimer* pt = PerfTimer::instance();
    pt->start();
    quick_sort( copy_of_array, array_size );
    float f = pt->stop();
    delete [] copy_of_array;
    return f;
}

float test_selection_sort( int const* array, int array_size )
{
    int* copy_of_array = new int[array_size];
    memcpy( copy_of_array, array, array_size * sizeof( int ) );
    PerfTimer* pt = PerfTimer::instance();
    pt->start();
    selection_sort( copy_of_array, array_size );
    float f = pt->stop();
    delete [] copy_of_array;
    return f;
}

float test_shell_sort( int const* array, int array_size )
{
    int* copy_of_array = new int[array_size];
    memcpy( copy_of_array, array, array_size * sizeof( int ) );
    PerfTimer* pt = PerfTimer::instance();
    pt->start();
    shell_sort( copy_of_array, array_size );
    float f = pt->stop();
    delete [] copy_of_array;
    return f;
}

int main()
{
    cout << "Enter the number of elements: ";
    int elements = 0;
    cin >> elements;

    int* array = (int*)new int[elements];
    for( int i = 0; i < elements; i++ )
    {
        array[i] = i + 1;
    }
    srand( time(0) );
    random_shuffle( array, array + elements );

    // uncomment the next line to view result of randomization of the array
    //spew_array( array, elements );

    cout << setiosflags( ios::fixed | ios::showpoint ) << setprecision( 6 );

    cout << "bubble," << elements << "," << test_bubble_sort( array, elements ) << endl;
    cout << "heap," << elements << "," << test_heap_sort( array, elements ) << endl;
    cout << "insertion," << elements << "," << test_insertion_sort( array, elements ) << endl;
    cout << "merge," << elements << "," << test_merge_sort( array, elements ) << endl;
    cout << "quick," << elements << "," << test_quick_sort( array, elements ) << endl;
    cout << "selection," << elements << "," << test_selection_sort( array, elements ) << endl;
    cout << "shell," << elements << "," << test_shell_sort( array, elements ) << endl;

    delete [] array;

    return 0;
}    

PerfTimer.h

CPP / C++ / C Code:
#ifndef _PerfTimer_h_
#define _PerfTimer_h_

#include <sys/time.h>

class PerfTimer* g_instance;

class PerfTimer
{
public:
    static PerfTimer* instance()
    {
        if( !g_instance )
        {
            g_instance = new PerfTimer();
        }
        return g_instance;
    }
    void start()
    {
        gettimeofday( &m_last, NULL );
    }
    float elapsed()
    {
        timeval result;
        gettimeofday( &m_now, NULL );
        timersub( &m_now, &m_last, &result );
        float elapsed_time = result.tv_sec + (1.0e-6 * result.tv_usec); 
        return elapsed_time;
    }
    float stop()
    {
        return elapsed();
    }
protected:
    private:
    struct timeval  m_now; 
    struct timeval  m_last; 
    PerfTimer()
    {
    }
};

#endif // ! _PerfTimer_h_

sorting_algorithms.h

CPP / C++ / C Code:
#ifndef _sorting_algorithms_h_
#define _sorting_algorithms_h_

#ifdef __cplusplus
extern "C"
{
#endif

void bubble_sort( int numbers[], int array_size );
void heap_sort( int numbers[], int array_size );
void insertion_sort( int numbers[], int array_size );
void merge_sort( int numbers[], int temp[], int array_size );
void quick_sort( int numbers[], int array_size );
void selection_sort( int numbers[], int array_size );
void shell_sort( int numbers[], int array_size );

#ifdef __cplusplus
}
#endif

#endif /* _sorting_algorithms_h_ */

sorting_algorithms.c

CPP / C++ / C Code:
#include <sorting_algorithms.h>

void bubble_sort( int numbers[], int array_size )
{
    int i, j, temp;

    for( i = (array_size - 1); i >= 0; i-- )
    {
        for( j = 1; j <= i; j++ )
        {
            if( numbers[j-1] > numbers[j] )
            {
                temp = numbers[j-1];
                numbers[j-1] = numbers[j];
                numbers[j] = temp;
            }
        }
    }
}

void sift_down( int numbers[], int root, int bottom )
{
    int done, maxChild, temp;

    done = 0;
    while( (root*2 <= bottom) && (!done) )
    {
        if( root*2 == bottom )
        {
            maxChild = root * 2;
        }
        else if( numbers[root * 2] > numbers[root * 2 + 1] )
        {
            maxChild = root * 2;
        }
        else
        {
            maxChild = root * 2 + 1;
        }
        if( numbers[root] < numbers[maxChild] )
        {
            temp = numbers[root];
            numbers[root] = numbers[maxChild];
            numbers[maxChild] = temp;
            root = maxChild;
        }
        else
        {
            done = 1;
        }
    }
}

void heap_sort( int numbers[], int array_size )
{
    int i, temp;

    for( i = (array_size / 2)-1; i >= 0; i-- )
    {
        sift_down(numbers, i, array_size);
    }

    for( i = array_size-1; i >= 1; i-- )
    {
        temp = numbers[0];
        numbers[0] = numbers[i];
        numbers[i] = temp;
        sift_down(numbers, 0, i-1);
    }
}


void insertion_sort( int numbers[], int array_size )
{
    int i, j, index;

    for( i=1; i < array_size; i++ )
    {
        index = numbers[i];
        j = i;
        while( (j > 0) && (numbers[j-1] > index) )
        {
            numbers[j] = numbers[j-1];
            j = j - 1;
        }
        numbers[j] = index;
    }
}

void merge( int numbers[], int temp[], int left, int mid, int right )
{
    int i, left_end, num_elements, tmp_pos;

    left_end = mid - 1;
    tmp_pos = left;
    num_elements = right - left + 1;

    while( (left <= left_end) && (mid <= right) )
    {
        if( numbers[left] <= numbers[mid] )
        {
            temp[tmp_pos] = numbers[left];
            tmp_pos = tmp_pos + 1;
            left = left +1;
        }
        else
        {
            temp[tmp_pos] = numbers[mid];
            tmp_pos = tmp_pos + 1;
            mid = mid + 1;
        }
    }

    while( left <= left_end )
    {
        temp[tmp_pos] = numbers[left];
        left = left + 1;
        tmp_pos = tmp_pos + 1;
    }
    while( mid <= right )
    {
        temp[tmp_pos] = numbers[mid];
        mid = mid + 1;
        tmp_pos = tmp_pos + 1;
    }

    for( i=0; i <= num_elements; i++ )
    {
        numbers[right] = temp[right];
        right = right - 1;
    }
}

void m_sort( int numbers[], int temp[], int left, int right )
{
    int mid;

    if( right > left )
    {
        mid = (right + left) / 2;
        m_sort( numbers, temp, left, mid );
        m_sort( numbers, temp, mid+1, right );

        merge( numbers, temp, left, mid+1, right );
    }
}

void merge_sort( int numbers[], int temp[], int array_size )
{
    m_sort( numbers, temp, 0, array_size - 1 );
}

void q_sort(int numbers[], int left, int right)
{
    int pivot, l_hold, r_hold;

    l_hold = left;
    r_hold = right;
    pivot = numbers[left];
    while( left < right )
    {
        while( (numbers[right] >= pivot) && (left < right) )
        {
            right--;
        }
        if( left != right )
        {
            numbers[left] = numbers[right];
            left++;
        }
        while( (numbers[left] <= pivot) && (left < right) )
        {
            left++;
        }
        if( left != right )
        {
            numbers[right] = numbers[left];
            right--;
        }
    }
    numbers[left] = pivot;
    pivot = left;
    left = l_hold;
    right = r_hold;
    if( left < pivot )
    {
        q_sort( numbers, left, pivot - 1 );
    }
    if( right > pivot )
    {
        q_sort( numbers, pivot + 1, right );
    }
}

void quick_sort( int numbers[], int array_size )
{
    q_sort( numbers, 0, array_size - 1 );
}

void selection_sort( int numbers[], int array_size )
{
    int i, j;
    int min, temp;

    for( i = 0; i < array_size - 1; i++ )
    {
        min = i;
        for( j = i+1; j < array_size; j++ )
        {
            if( numbers[j] < numbers[min] )
            {
                min = j;
            }
        }
        temp = numbers[i];
        numbers[i] = numbers[min];
        numbers[min] = temp;
    }
}

void shell_sort( int numbers[], int array_size )
{
    int i, j, increment, temp;

    increment = 3;
    while( increment > 0 )
    {
        for( i=0; i < array_size; i++ )
        {
            j = i;
            temp = numbers[i];
            while( (j >= increment) && (numbers[j-increment] > temp) )
            {
                numbers[j] = numbers[j - increment];
                j = j - increment;
            }
            numbers[j] = temp;
        }
        if( increment/2 != 0 )
        {
            increment = increment/2;
        }
        else if( increment == 1 )
        {
            increment = 0;
        }
        else
        {
            increment = 1;
        }
    }
}

Output:

Code:
Enter the number of elements: 100000 bubble,100000,20.307362 heap,100000,0.013039 insertion,100000,2.099443 merge,100000,0.017994 quick,100000,0.009736 selection,100000,4.463685 shell,100000,1.698534

...using a: Intel(R) Core(TM)2 CPU 6600 @ 2.40GHz


Note that I wrote my interfaces to the performance testing such that they return the elapsed time for the sorting algorithm call. Therefore, it would be easy (trivial) to modify this code to support averaging the sorts over a period of some number of iterations. Here is an example:

CPP / C++ / C Code:
#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>

#include <cstring>

using namespace std;

#include <sorting_algorithms.h>
#include <PerfTimer.h>

void spew_array( int const* array, const int array_size )
{
    for( int i = 0; i < array_size; i++ )
    {
        cout << array[i] << endl;
    }
}

float test_bubble_sort( int const* array, int array_size )
{
    int* copy_of_array = new int[array_size];
    memcpy( copy_of_array, array, array_size * sizeof( int ) );
    PerfTimer* pt = PerfTimer::instance();
    pt->start();
    bubble_sort( copy_of_array, array_size );
    float f = pt->stop();
    delete [] copy_of_array;
    return f;
}

float test_heap_sort( int const* array, int array_size )
{
    int* copy_of_array = new int[array_size];
    memcpy( copy_of_array, array, array_size * sizeof( int ) );
    PerfTimer* pt = PerfTimer::instance();
    pt->start();
    heap_sort( copy_of_array, array_size );
    float f = pt->stop();
    delete [] copy_of_array;
    return f;
}

float test_insertion_sort( int const* array, int array_size )
{
    int* copy_of_array = new int[array_size];
    memcpy( copy_of_array, array, array_size * sizeof( int ) );
    PerfTimer* pt = PerfTimer::instance();
    pt->start();
    insertion_sort( copy_of_array, array_size );
    float f = pt->stop();
    delete [] copy_of_array;
    return f;
}

float test_merge_sort( int const* array, int array_size )
{
    int* copy_of_array = new int[array_size];
    memcpy( copy_of_array, array, array_size * sizeof( int ) );
    int* temp_array = new int[array_size];
    PerfTimer* pt = PerfTimer::instance();
    pt->start();
    merge_sort( copy_of_array, temp_array, array_size );
    float f = pt->stop();
    delete [] copy_of_array;
    delete [] temp_array;
    return f;
}

float test_quick_sort( int const* array, int array_size )
{
    int* copy_of_array = new int[array_size];
    memcpy( copy_of_array, array, array_size * sizeof( int ) );
    PerfTimer* pt = PerfTimer::instance();
    pt->start();
    quick_sort( copy_of_array, array_size );
    float f = pt->stop();
    delete [] copy_of_array;
    return f;
}

float test_selection_sort( int const* array, int array_size )
{
    int* copy_of_array = new int[array_size];
    memcpy( copy_of_array, array, array_size * sizeof( int ) );
    PerfTimer* pt = PerfTimer::instance();
    pt->start();
    selection_sort( copy_of_array, array_size );
    float f = pt->stop();
    delete [] copy_of_array;
    return f;
}

float test_shell_sort( int const* array, int array_size )
{
    int* copy_of_array = new int[array_size];
    memcpy( copy_of_array, array, array_size * sizeof( int ) );
    PerfTimer* pt = PerfTimer::instance();
    pt->start();
    shell_sort( copy_of_array, array_size );
    float f = pt->stop();
    delete [] copy_of_array;
    return f;
}

int main()
{
    cout << "Enter the number of elements: ";
    int elements = 0;
    cin >> elements;

    int* array = (int*)new int[elements];
    for( int i = 0; i < elements; i++ )
    {
        array[i] = i + 1;
    }
    srand( time(0) );
    random_shuffle( array, array + elements );

    // uncomment the next line to view result of randomization of the array
    //spew_array( array, elements );

    cout << setiosflags( ios::fixed | ios::showpoint ) << setprecision( 6 );

    int iterations = 0;
    cout << "Enter the number of iterations (may take a long time with a lot of elements & iterations!): ";
    cin >> iterations;

    float bubble_result     = 0.0F;
    float heap_result       = 0.0F;
    float insertion_result  = 0.0F;
    float merge_result      = 0.0F;
    float quick_result      = 0.0F;
    float selection_result  = 0.0F;
    float shell_result      = 0.0F;

    for( int i = 0; i < iterations; i++ )
    {
        bubble_result       += test_bubble_sort( array, elements );
        heap_result         += test_heap_sort( array, elements );
        insertion_result    += test_insertion_sort( array, elements );
        merge_result        += test_merge_sort( array, elements );
        quick_result        += test_quick_sort( array, elements );
        selection_result    += test_selection_sort( array, elements );
        shell_result        += test_shell_sort( array, elements );
    }

    cout << "bubble," << elements << "," << iterations << "," << bubble_result/iterations << endl;
    cout << "heap," << elements << "," << iterations << "," << heap_result/iterations << endl;
    cout << "insertion," << elements << "," << iterations << "," << insertion_result/iterations << endl;
    cout << "merge," << elements << "," << iterations << "," << merge_result/iterations << endl;
    cout << "quick," << elements << "," << iterations << "," << quick_result/iterations << endl;
    cout << "selection," << elements << "," << iterations << "," << selection_result/iterations << endl;
    cout << "shell," << elements << "," << iterations << "," << shell_result/iterations << endl;

    delete [] array;

    return 0;
}    

Output:

Code:
Enter the number of elements: 10000 Enter the number of iterations (may take a long time with a lot of elements & iterations!): 100 bubble,10000,100,0.206015 heap,10000,100,0.001024 insertion,10000,100,0.021924 merge,10000,100,0.001537 quick,10000,100,0.000854 selection,10000,100,0.045961 shell,10000,100,0.018120


:davis:
  #9  
Old 06-Sep-2007, 20:15
xlonehats xlonehats is offline
New Member
 
Join Date: Sep 2007
Posts: 15
xlonehats is on a distinguished road

Re: sorting algorithms


thanks very much mate....didnt even know all those new code exist....learnt a lot...cheers
 
 

Recent GIDBlogMeeting the populace by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Sorting antorip C++ Forum 1 11-Aug-2006 15:14
Sorting a vector of structures within a structure jondean C++ Forum 4 03-Jul-2006 15:33
Problem with sorting algorithm MM4o C++ Forum 3 24-Feb-2006 09:40
sorting numbers through array zidanefreak01 C++ Forum 3 26-Jun-2005 02:41
sorting string w/strcmp doc_watson2007 C Programming Language 4 20-Nov-2004 09:17

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 03:33.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.