4 Replies - 1846 Views - Last Post: 14 April 2011 - 06:30 PM Rate Topic: -----

#1 born2c0de   User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 187
  • View blog
  • Posts: 4,673
  • Joined: 26-November 04

Radix Sort

Posted 10 June 2008 - 07:32 AM

Description: Implements Radix Sort
/* Written by Sanchit Karve (born2c0de)
   Contact Me at born2c0de AT dreamincode DOT net
*/

#include <stdio.h>

#define MAX 5
#define SHOWPASS

void print(int *a,int n)
{
		int i;		
        for(i=0;i<n;i++)
			printf("%dt",a[i]);
}
   
void radixsort(int *a,int n)
{
		int i,b[MAX],m=0,exp=1;
		for(i=0;i<n;i++)
		{
			if(a[i]>m)
				m=a[i];
		}
		
		while(m/exp>0)
		{
			int bucket[10]={0};
			for(i=0;i<n;i++)
				bucket[a[i]/exp%10]++;
			for(i=1;i<10;i++)
				bucket[i]+=bucket[i-1];
			for(i=n-1;i>=0;i--)
				b[--bucket[a[i]/exp%10]]=a[i];
			for(i=0;i<n;i++)
				a[i]=b[i];
			exp*=10;

#ifdef SHOWPASS
		    printf("nPASS   : ");
			print(a,n);
#endif
        }		
	}


int main()
{
           int arr[MAX];
           int i,n;
           
           printf("Enter total elements (n < %d) : ",MAX);
           scanf("%d",&n);          
           
           printf("Enter %d Elements : ",n);
           for(i=0;i<n;i++)
				scanf("%d",&arr[i]);
           
           
           printf("nARRAY  : ");
           print(&arr[0],n);
           
           radixsort(&arr[0],n);
           
           printf("nSORTED : ");
           print(&arr[0],n);
           printf("n");

           return 0;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Radix Sort

#2 Princess.   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 6
  • Joined: 10-September 08

Re: Radix Sort

Posted 07 October 2008 - 09:12 AM

what does this mean? #endif
Was This Post Helpful? 0
  • +
  • -

#3 prasad_jrv   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 0
  • Joined: 16-April 09

Re: Radix Sort

Posted 16 April 2009 - 10:40 PM

The radix sort is simple aand useful. Thanks
Was This Post Helpful? 0
  • +
  • -

#4 ylqfp   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 0
  • Joined: 17-October 09

Re: Radix Sort

Posted 17 October 2009 - 06:09 AM

the most simple but useful radix sort that i'd ever seen
Was This Post Helpful? 0
  • +
  • -

#5 gudipati   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 0
  • Joined: 30-March 09

Re: Radix Sort

Posted 14 April 2011 - 06:30 PM

you can see the program with output here http://sourcecode4u....radix-sort.html
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1