Saturday, May 18, 2013

Radix sort c++ code

For in-depth understanding of Data Structure and Algorithm concepts refer :

1) INTRODUCTION TO ALGORITHMS by Coremen Introduction to Algorithms, 3rd Edition From flipkart.com
Introduction to Algorithms, 3rd Edition From amazon.com
Introduction to Algorithms from amazon.in

2) DATA STRUCTURES USING C AND C++ by Tenenbaum M Aaron - DATA STRUCTURES USING C AND C++ by Tenenbaum M Aaron from flipkart.com
Data Structures Using C and C++ (2nd Edition) from amazon.com
Data Structures Using C and C++ from amazon.in

#include<iostream.h>
#include<conio.h>
#include<string.h>

void countingsort(char s[][20],int a[],char b[][20],int n,int k)
{   int c[100];
    int l,j,i;
    for( l=0;l<=k;l++)
        c[l]=0;
    for(j=0;j<n;j++)
        c[a[j]]=c[a[j]]+1;
    for(i=1;i<=k;i++)
        c[i]=c[i]+c[i-1];
    for(j=n-1;j>=0;j--)
    {
        strcpy(b[c[a[j]]-1],s[j]);
        c[a[j]]=c[a[j]]-1;
    }
    for(i=0;i<n;i++)
    {
        strcpy(s[i],b[i]);
    }
}

void radixsort(char s[][20],int d,int n)
{  int z[20];
    char b[20][20];
    int i,j,k;
    j=d-1;
    for(i=1;i<=d;i++)
    {
        for(k=0;k<n;k++)
        {
            z[k]=s[k][j]-48;
        }
        --j;

        countingsort(s,z,b,n,9);
    }
    cout<<"Output"<<endl;
    for(i=0;i<n;i++)
        cout<<b[i]<<endl;
}


void main()
{
    int n,i,d;
    char a[20][20];
    cout<<"Entr the size of the array"<<endl;
    cin>>n;
    cout<<"Enter the no. of digits"<<endl;
    cin>>d;
    cout<<"Enter its element"<<endl;
    for(i=0;i<n;i++)
        cin>>a[i];
    radixsort(a,d,n);

    getch();
}

No comments:

Post a Comment