Design and Analysis of Algorithms - 7

Implement program of Counting Sort.


Code:


#include <stdio.h>

#include <stdlib.h>

#include <time.h>

 

int count1=0;

void count_s(int arr1[],int n,int max);

 

int main()

{

    int n,i;

    float t1,t2;

    printf("Enter the length of array: ");

    scanf("%d",&n);

    int arr[n],ar[n];

    for(i=0;i<n;i++)

    {

        ar[i]=i;

    }

    for(i=0;i<n;i++)

    {

        arr[i] = rand()%n;

        printf("%d ",arr[i]);

    }

    int max=0;

    for(i=0;i<n;i++)

    {

        if(arr[i]>max)

        {

            max = arr[i];

        }

    }

    clock_t st2 = clock();

    count_s(arr, n, max);

    clock_t et2 = clock();

    t2 = ((double)(et2-st2))/CLOCKS_PER_SEC;

 

    printf("\nThe Count step is: %d",count1);

    count1=0;

    clock_t st1 = clock();

    count_s(ar,n,n-1);

    clock_t et1 = clock();

    t1 = ((double)(et1-st1))/CLOCKS_PER_SEC;

    printf("\nThe Count step is: %d",count1);

 

    printf("\n\n");

    printf("Time taken for Sorted Data:- %f\n",t1);

    printf("Time taken for Random Data:- %f",t2);

    return 0;

}

 

void count_s(int arr1[],int n,int max)

{

    int i,j,count=0;

    int max_arr[max+1];

    for(i=0;i<max+1;i++)

    {

        count1++;

        max_arr[i] = 0;

    }

    for(i=0;i<=max+1;i++)

    {

        count1++;

        count = 0;

        for(j=0;j<n;j++)

        {

            count1++;

           if(arr1[j] == i)

           {

               count1++;

               count++;

           }

        }

        if(count == 0)

        {

            count1++;

            max_arr[i]=0;

        }

        else

        {

            count1++;

            if(i==max)

            {

                count1++;

                max_arr[i]=count;

            }

            else{

                    count1++;

                max_arr[i]=count;

            }

        }

    }

    printf("\n");

 

    for(i=0;i<max+1;i++)

    {

        count1++;

        int a=i;

        if(i==0)

        {

            count1++;

            max_arr[i]=a;

        }

        else

        {

            count1++;

            max_arr[i]=max_arr[i]+max_arr[a-1];

        }

    }

    int arr[n],a,b;

    printf("\n");

    for(i=0;i<n;i++)

    {

        count1++;

        a = arr1[i];

            count1++;

            b = max_arr[a];

            count1++;

            max_arr[a]=max_arr[a]-1;

            count1++;

            arr[b] = a;

    }

    for(i=0;i<n;i++)

    {

        printf("%d ",arr[i]);

    }

}

 

Output:


 





 

 

 

 


Analysis Table

 

Time

N

Sorted Data

Random Data

5

0.000004

0.000015

50

0.000029

0.000038

500

0.000684

0.000678

5000

0.066813

0.064291

50000

5.517792

5.450424

 

 Step Count

N

Sorted Data

Random Data

5

87

76

50

3102

3083

500

256002

255828

5000

25060002

25058179

50000

1794367294

1794375673

 

 

 

 

 

 Analysis Chart:







Conclusion:


In this Practical, I understand to Implement program of Counting Sort.

Here counting sort is effective when range is not greater than number of objects to be sorted.

Counting sort used to sort the negative input values.

Comments

Popular posts from this blog

Computer Architecture and Organization - 4

Design and Analysis of Algorithms - 2

Design and Analysis of Algorithms - 6

Design and Analysis of Algorithms - 1

Artificial Intelligence - 7