Sunteți pe pagina 1din 8

ASSIGNMENT 23

SORTING OF AN UNSORTED ARRAY BY USING RADIX SORT

STATEMENT:

In ‘Radix Sort’, sorting operation is carried out by means of distribution based on the
constituent components in the elements. This is why the sorting method is called sorting by
distribution. Sorting without key comparison-this feature makes this sorting algorithm
distinguished from others type of sorting. This sorting technique is based on radix or base
of constituent elements in keys so it is called ‘Radix Sort’.

PICTORIAL REPRESENTATION OF RADIX SORT:


ALGORITHM:

INPUT: An unsorted array a[6,2,4,1,…………….,n]

OUTPUT: An sorted array a[1,2,3,4,5,…………..,n] (in ascending order)

Step 1: Create a function “void radixsort(int a[],int n)”

Step 1.1: Set or initialise the all elements of an integer type array ‘d’ at 0 i.e

Set d[i] 0 [ i= 0 to 9]

Step 1.2: Take the max number of digit in an integer type variable ‘max’ and this is
the number of required pass to do the sorting.

Step 1.3: While(pass ≤ max) then repeat Step 1.4 to Step 1.22
Step 1.4: For i=0 to i<n repeat Step 1.5 to Step 1.13

Step 1.5: Set c0

Step 1.6: Set pa[i]

Step 1.7: While(c<pass) then repeat Step 1.8 to Step 1.10

Step 1.8: Set rp mod 10

Step 1.9: Set pp/10

Step 1.10: Set cc+1

[End of Step 1.7 ‘While’ loop]

Step 1.11: Set jd[r]

Step 1.12: Set b[r][j] a[i]

Step 1.13: Set d[r] d[r]+1

[End of Step 1.4 ‘For’ loop]

Step 1.14: Set passpass+1

Step 1.15: Set i0

Step 1.16: For j=0 to 9 repeat Step 1.17 to Step 1.20

Step 1.17: If(d[j]>0) then do

Step 1.18: For k=0 to k<d[j] repeat Step 1.19 and Step 1.20

Step 1.19: Set a[i] b[j][k]

Step 1.20: Set ii+1

[End of Step 1.18 ‘For’ loop]

[End of ‘If’]

[End of Step 1.16 ‘For’ loop]

Step 1.21: For i=0 to 9 repeat Step 1.22

Step 1.22: Set d[i] 0

[End of 1.3 ‘While’ loop]


[End of the function “void radixsort(int a[],int n)”]

Step 2: Create the function “void main()”

Step 2.1 Take the number of elements from the user in an integer type variable ‘n’ .

Step 2.2: Take the unsorted elements from the user in an array ‘a’.

Step 2.3: Print the unsorted array.

Step 2.4: Call the function ‘radixsort(a,n)’

Step 2.5: Print the sorted array ‘a’.

Step 2.6: Stop.

[End of the function “void main()”]

Step 3: Stop.

PROGRAM CODE:
#include<stdio.h>

#include<conio.h>

#define NULL 0

//*Creation of the function 'radixsort()' to sort an unsorted array using radix sort
technique*//

void radixsort(int a[],int n)

int i,b[20][20],pass=1,c,max,p,r,d[10],j,k;

for(i=0;i<10;i++)
d[i]=NULL;

//* Taking the maximum number of digit in the array*//

printf("\n\nEnter the max no of digit: ");

scanf("%d",&max);

//*Sorting of the unsorted array*//

while(pass<=max)

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

c=0;

p=a[i];

while(c<pass)

r=p%10;

p=p/10;

c++;

j=d[r];

b[r][j]=a[i];

d[r]=d[r]+1;

pass++;

i=0;

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

{
if(d[j]>0)

for(k=0;k<d[j];k++)

a[i]=b[j][k];

i++;

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

d[i]=NULL;

void main()

int n,a[20],i;

//*Taking the number of elements of the unsorted array*//

printf("Enter the no of elements: ");

scanf("%d",&n);

//*Taking the unsorted elements from the user in array 'a'*//


printf("Enter the elements: ");

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

scanf("%d",&a[i]);

//* Printing the unsorted elements of the array 'a'*//

printf("\nThe unsorted array: \n\n");

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

printf("%d->",a[i]);

//*Calling the function 'radixsort()'*//

radixsort(a,n);

//*Print the sorted elements of the array 'a'*//

printf("\n\nThe unsorted array: \n\n");

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

printf("%d->",a[i]);

getch();

OUTPUT:
DISCUSSION:
The runtime complexity of the radix sort algorithm is O(n) i.e the time complexity of radix
sort algorithm is O(n). The run time of the radix sort algorithm is mainly due to two
operations: distribution of key elements and followed by a combination. It can be easily
checked that the run time remains invariant irrespective of the order of the elements in the
list.

S-ar putea să vă placă și