counting sort

#include <stdio.h>
#include <string.h>

#define BOUNDARY 7

/**
* inputArr - pointer to input array
* outputArr - pointer to output array
* length - lenght of input array
**/
int *countingSort( int *inputArr, int *outputArr, int length );

int main( void )
{
    int inputArr[] = {3,4,6,5,3,4,2,1};
    int outputArr[8];

    int *result = countingSort( inputArr, outputArr, 8 );

    int j;
    for( j = 0; j < 8; j++ )
    {
        printf( "%d  ", *(result+j) );
    }
    return 0;
}

int *countingSort( int *inputArr, int *outputArr, int length )
{
    int countArr[BOUNDARY];
    int i;
    int total = 0;
    int c;
    memset( countArr, 0, BOUNDARY*4 );

    for( i = 0; i < length; i++ )
    {
        countArr[*(inputArr+i)] = countArr[*(inputArr+i)] + 1;

    }

    for( i = 0; i < BOUNDARY; i++ )
    {
        c = countArr[i];
        countArr[i] = total;
        total = total + c;
    }

    for( i = 0; i < length; i++ )
    {
        *(outputArr + countArr[*(inputArr+i)]) = *(inputArr+i);
        countArr[*(inputArr+i)] = countArr[*(inputArr+i)] + 1;
    }

    return outputArr;

}

Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s