0% found this document useful (0 votes)
23 views

DAS Lab Programs2024

thanks
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

DAS Lab Programs2024

thanks
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 55

1.

a) List ADT using arrays

#include<stdio.h>
#include<conio.h>
#define MAX 10
void create( );
void insert( );
void deletion( );
void search( );
void display ();
int a,b[20], n, p, e, f, i, pos;
void main( )
{
int ch;
char
g='y';
clrscr();
do
{
printf("\n Main Menu");
printf("\n 1.Create \n 2.Delete \n 3.Search \n 4.Insert \n 5.Display\n 6.Exit \n");
printf("\n Enter your Choice:");
scanf("%d", &ch);
switch(ch)
{
case 1:
create();
break;
case 2:
deletion();
break;
case 3:
search();
break;
case 4:
insert();
break;
case 5:
display();
break;
case 6:
exit();
break;
default:
printf("\n Enter the correct choice:");
}
printf("\n Do u want to continue:");
scanf("\n%c", &g);
}
while(g=='y'||g=='Y');
getch();
}
void create( )
{
printf("\n Enter the number of nodes:");
scanf("%d", &n);
for(i=0;i<n;i++)
{
printf("\n Enter the Element:",i+1);
scanf("%d", &b[i]);
}
}
void deletion( ) {
printf("\n Enter the position u want to
delete:"); scanf("%d", &pos);
if(pos>=n) {
printf("\n Invalid Location:");
}
else {
for(i=pos+1;i<n;i++) {
b[i-1]=b[i];
}n--;
}
printf("\n The Elements after
deletion:"); for(i=0;i<n;i++) {
printf("\t%d", b[i]);
}}
void search() {
printf("\n Enter the Element to be
searched:"); scanf("%d", &e);
for(i=0;i<n;i++) {
if(b[i]==e) {
printf("Value is in the %d Position", i);
}
else
{
printf("Value %d is not in the list:", e);
continue;
}
}
}
void insert()
{
printf("\n Enter the position u need to insert:");
scanf("%d", &pos);
if(pos>=n)
{
printf("\n invalid Location:");
}
else
{
for(i=MAX-1;i>=pos-1;i--)
{
b[i+1]=b[i];
}
printf("\n Enter the element to
insert:"); scanf("%d",&p);
b[pos]=p;
n++;
}
printf("\n The list after insertion:");
display();
}
void display()
{
printf("\n The Elements of The list ADT
are:"); for(i=0;i<n;i++)
{
printf("\n\n%d", b[i]);
}
}
OUTPUT
1. b) List ADT using Linked List

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
struct node
{
int data;
struct node *next;
};
struct node *start;
void insertbeg(void)
{
struct node *nn;
int a;
nn=(struct node *)malloc(sizeof(struct node));
printf("enter data:");
scanf("%d",&nn->data);
a=nn->data;
if(start==NULL)
{
nn->next=NULL;
start=nn;
}
else
{
nn->next=start;
start=nn;
}
printf("%d succ. inserted \n",a);
return;
}
void insertend(void)
{
struct node
*nn,*lp; int b;
nn=(struct node *)malloc(sizeof(struct node));
printf("enter data:");
scanf("%d",&nn->data);
b=nn->data;
if(start==NULL)
{
nn->next=NULL;
start=nn;
}
else {
lp=start;
while(lp->next!=NULL)
{
lp=lp->next;
}
lp->next=nn;
nn->next=NULL;
}
printf("%d is succ. inserted \n",b);
return;
}
void insertmid(void)
{
struct node *nn,*temp,*ptemp;
int x,v;
nn=(struct node *)malloc(sizeof(struct node));
if(start==NULL) {
printf("sll is empty\n");
return;
}
printf("enter data before which no. is to be inserted:\n");
scanf("%d",&x);
if(x==start->data)
{
insertbeg();
return;
}
ptemp=start;
temp=start-
>next;
while(temp!=NULL&&temp->data!=x)
{
ptemp=temp;
temp=temp-
>next;
}
if(temp==NULL) {
printf("%d data does not exist \n",x);
}
else {
printf("enter data:");
scanf("%d",&nn->data);
v=nn->data;
ptemp-
>next=nn; nn-
>next=temp;
printf("%d succ. inserted \n",v);
}
return;
}
void deletion(void) {
struct node *pt,*t;
int x;
if(start==NULL)
{
printf("sll is empty\n");
return;
}
printf("enter data to be deleted:");
scanf("%d",&x);
if(x==start->data)
{
t=start;
start=start->next;
free(t);
printf("%d is succ. deleted\n",x);
return;
}
pt=start;
t=start-
>next;
while(t!=NULL&&t->data!=x)
{
pt=t;t=t->next;
}
if(t==NULL)
{
printf("%d does not exist\n",x);
return;
}
else
{
pt->next=t->next;
}
printf("%d is succ. deleted\n",x);
free(t);
return;
}
void display(void)
{
struct node
*temp;
if(start==NULL)
{
printf("sll is empty\n");
return;
}
printf("elements are:\n");
temp=start; while(temp!
=NULL)
{
printf("%d\n",temp->data);
temp=temp->next;
}
return;
}
int main( )
{
int c,a;
start=NULL;
do
{
printf("1:insert\n2:delete\n3:display\n4:exit\nenter choice:");
scanf("%d",&c);
switch(c)
{
case 1:
printf("1:insertbeg\n2:insert end\n3:insert mid\nenter choice:");
scanf("%d",&a);
switch(a)
{
case 1:insertbeg(); break;
case 2:insertend(); break;
case 3:insertmid();
break;
} break;
case 2:deletion();
break; case 3:display();
break;
case 4:printf("program ends\n");break;
default:printf("wrong choice\n"); break;
}
}while(c!=4);
return 0;
}
OUTPUT
2. a) Stack ADT Using Singly Linked List

#include <stdio.h>
void push( );
void pop( );
void display( );
int main( )
{
int n;
printf("STACK USING LINKED LIST\n1.PUSH\n 2.POP\n 3.DISPLAY\n 4.EXIT \n");
do
{
printf("\nEnter your choice\n");
scanf("%d",&n);
switch(n)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display( );
break;
case 4:
break;
default:
printf("Invalid choice\n");
break;
}
}while(n!=4);
}
typedef struct node
{
int data;
struct node *link;
}n;
n *top=NULL;
void push()
{
int item; n *temp;
printf("Enter the item\n");
scanf("%d",&item);
temp=(n*)malloc(sizeof(n));
temp->data=item;
temp->link=top;
top=temp;
}
void pop()
{
n *temp;
if(top==NULL)
printf("Stack is empty\n");
else
{
temp=top;
printf("The element deleted = %d\n",temp->data);
free(temp);
top=top->link;
}
}
void display()

{
n *save;
if(top==NULL)
printf("Stack is empty\n");
else
{
save=top;
printf("The elements of the stack are :");
while(save!=NULL)
{
printf("%d\t",save-
>data); save=save->link;
}
printf("\nTopmost element = %d\n",top->data);
}
}
OUTPUT
2. b) Queue ADT Using Singly Linked List

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int data;
struct node* next;
}*rear, *front;
void dequeue()
{
struct node *temp, *var=rear;
if(var==rear)
{
rear = rear->next;
free(var);
}
else
printf("\nQueue Empty");
}
void enqueue(int value)
{ struct node *temp;
temp=(struct node
*)malloc(sizeof(struct node)); temp
->data=value;
if (front == NULL) {
front=temp;
front->next=NULL;
rear=front;
}
else {
front
->next=temp; front=temp;
front
->next=NULL;
}
}
void display() {
struct node *var=rear; if(var!
=NULL) { printf("\nElements
in Queue: "); while(var!
=NULL) {
printf("\t%d",var->data);
var=var->next;
}
printf("\n");
}
else
printf("\nQueue is Empty");
}
int main( )
{
int ch;
clrscr();
front=NULL;
printf("<-----Queue using Linked List----->\n");
printf("\n1. Enqueue an element");
printf("\n2. Dequeue an element");
printf("\n3. Display Queue");
printf("\n4. Exit\n");
while(1)
{
printf("\nEnter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1:
{
int value;
printf("\nEnter a value to Enqueue: ");
scanf("%d",&value);
enqueue(value);
display( );
break;
}
case 2:
{
dequeue( );
display( );
break;
}
case 3:
{
display( );
break;
}
case 4:
{
exit(0);
}
default: {
printf("\nwrong choice for operation");
}
}
}
}
OUTPUT
3. a)Infix to Postfix Expression

#include<stdio.h>
#include<ctype.h>

char stack[100];
int top = -1;

void push(char x)
{
stack[++top] = x;
}

char pop()
{
if(top == -1)
return -1;
else
return stack[top--];
}

int priority(char x)
{
if(x == '(')
return 0;
if(x == '+' || x ==
'-') return 1;
if(x == '*' || x == '/')
return 2;
return 0;
}

int main()
{
char exp[100];
char *e, x;
printf("Enter the expression :
"); scanf("%s",exp);
printf("\n");
e = exp;

while(*e != '\0')
{
if(isalnum(*e))
printf("%c ",*e);
else if(*e ==
'(') push(*e);
else if(*e == ')')
{
while((x = pop()) != '(')
printf("%c ", x);
}
else
{
while(priority(stack[top]) >=
priority(*e)) printf("%c ",pop());
push(*e);
}
e++;
}

while(top != -1)
{
printf("%c ",pop());
}return 0;
}
OUTPUT
3. b) Evaluating Postfix Expression

#include<stdio.h>
int stack[20];
int top = -1;

void push(int x)
{
stack[++top] = x;
}

int pop()
{
return stack[top--];
}

int main()
{
char exp[20];
char *e;
int n1,n2,n3,num;
printf("Enter the expression ::
"); scanf("%s",exp);
e = exp;
while(*e != '\0')
{
if(isdigit(*e))
{
num = *e - 48;
push(num);
}
else
{
n1 = pop();
n2 =
pop();
switch(*e)
{
case '+':
{
n3 = n1 +
n2; break;
}
case '-':
{
n3 = n2 -
n1; break;
}
case '*':
{
n3 = n1 *
n2; break;
}
case '/':
{
n3 = n2 /
n1; break;
}
}
push(n3);
}
e++;
}
printf("\nThe result of expression %s = %d\n\n",exp,pop());
return 0;
}
OUTPUT
4. write a program to implement priority queue ADT

# include <stdio.h>
# include <stdlib.h>
# include <conio.h>
# define MAX 10
int arr[MAX];
int i,item,n;
void insert(int num)
{
if(i<MAX)
{
}
else
}
arr[i]=num;
printf("\n Array is full");
void makeheap()
{
for(i=0;i<n;i++)
{
int val=arr[i]; int j=i;
int f=(j-1)/2;

while(j>0 && arr[f] < val)


{
arr[j]=arr[f];
j=f; f=(j-1)/2;
}
arr[j]=val;
}
}

void display()
{
printf("\n"); for(i=0;i<n;i++)
printf(" %d",arr[i]);
}
int main()
{
clrscr();
printf("\n Enter the total no. of
elements:"); scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n Enter the elements to be inserted:");
scanf("%d",&item);
insert(item);
}
printf("\n\t The Elements are...");
display();
makeheap();
printf("\n\t Heapified:");
display();
getch();
return 0;
}
OUTPUT
5. Write a program to perform the following operations:
 Insert an element into a binary search tree.
 Delete an element from a binary search tree.
 Search for a key element in a binary search tree.

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
struct searchtree
{
int element;
struct searchtree *left,*right;
}*root;
typedef struct searchtree *node;
typedef int ElementType;
node insert(ElementType, node);
node delete(ElementType,
node); void makeempty();
node findmin(node);
node findmax(node);
node find(ElementType, node);
void display(node, int);
void main()
{
int ch;
ElementType a;
node temp;
makeempty();
while(1)
{
printf("\n1. Insert\n2. Delete\n3. Find\n4. Find min\n5. Find max\n6.Display\n7.Exit
\nEnter Your Choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("Enter an element : ");
scanf("%d", &a);
root = insert(a, root);
break;
case 2:
printf("\nEnter the element to delete : ");
scanf("%d",&a);
root = delete(a, root);
break;
case 3:
printf("\nEnter the element to search : ");
scanf("%d",&a);
temp = find(a, root);
if (temp != NULL)
printf("Element found");
else
printf("Element not found");
break;
case 4:
temp = findmin(root);
if(temp==NULL)
printf("\nEmpty tree");
else
printf("\nMinimum element : %d", temp->element);
break;
case 5:
temp = findmax(root);
if(temp==NULL)
printf("\nEmpty tree");
else
printf("\nMaximum element : %d", temp->element);
break;
case 6: if(root==NULL)printf("\
nEmpty tree"); else
display(root, 1);
break;
case 7:
exit(0);
default:
printf("Invalid Choice");
}
}
}
node insert(ElementType x,node t)
{ if(t==NUL
L)
{
t=
(node)malloc(sizeof(node)); t-
>element = x;
t->left = t->right = NULL;
}
else
{
if(x < t->element)
t->left = insert(x, t->left);
else
if(x > t->element)t->right = insert(x, t->right);
}
return t;
}
node delete(ElementType x,node t)
{
node temp;
if(t ==
NULL)
printf("\nElement not found");
else {
if(x < t->element)
t->left = delete(x, t->left);
else if(x > t->element)
t->right = delete(x, t->right);
else {
if(t->left && t->right)
{
temp = findmin(t->right);
t->element = temp->element;
t->right = delete(t->element,t->right);
}
else if(t->left == NULL)
{
temp = t;
t=t-
>right;
free (temp);
} else {
temp = t;
t=t->left;
free
(temp);
}
}}
return t;
}
void makeempty() {
root = NULL;
}
node findmin(node temp) {
if(temp == NULL || temp->left ==
NULL) return temp;
return findmin(temp->left);
}
node findmax(node temp) {
if(temp==NULL || temp->right==NULL)
return temp;
return findmax(temp->right);
}
node find(ElementType x, node t) {
if(t==NULL)
return NULL;
if(x<t->element)
return find(x,t->left);
if(x>t->element)
return find(x,t->right);
return t; }
void display(node t,int level)
{ int i;
if(t) {
display(t->right, level+1);
for(i=0;i<level;i++)
printf(" ");
printf("%d", t->element);
display(t->left, level+1);
}
}
OUTPUT
6. Write a program to perform the following operations
 Insertion into an AVL-tree
 Deletion from an AVL-tree

#include<stdio.h>
#include<conio.h>
typedef struct node
{
int data;
struct node *left,*right;
int ht;
}node;
node *insert(node *,int);
node *Delete(node *,int);
void preorder(node *);
void inorder(node *);
int height( node *);
node *rotateright(node *);
node *rotateleft(node *);
node *RR(node *);
node *LL(node *);
node *LR(node *);
node *RL(node *);
int BF(node *);
int main( ) {
node *root=NULL;
int x,n,i,op;
clrscr();
do{
printf("\n1)Create:");
printf("\n2)Insert:");
printf("\n3)Delete:");
printf("\n4)Print:");
printf("\n5)Quit:"); printf("\n\
nEnter Your Choice:");
scanf("%d",&op);
switch(op) {
case 1: printf("\nEnter no. of
elements:"); scanf("%d",&n);
printf("\nEnter tree data:");
root=NULL;
for(i=0;i<n;i++) {
scanf("%d",&x);
root=insert(root,x);
}
break;
case 2: printf("\nEnter a data:");
scanf("%d",&x);
root=insert(root,x);
break;
case 3: printf("\nEnter a data:");
scanf("%d",&x);
root=Delete(root,x);
break;
case 4: printf("\nPreorder sequence:\n");
preorder(root);
printf("\n\nInorder sequence:\n");
inorder(root);
printf("\n");
break;
}
}while(op!=5);
return 0;
}
node * insert(node *T,int x) {
if(T==NULL) {
T=(node*)malloc(sizeof(node)); T->data=x; T->left=NULL; T->right=NULL;
}
else
if(x > T->data) // insert in right subtree
{
T->right=insert(T->right,x);
if(BF(T)==-2)
if(x>T->right->data)
T=RR(T);
else
T=RL(T);
}
else
if(x<T->data)
{
T->left=insert(T->left,x);
if(BF(T)==2)
if(x < T->left->data)
T=LL(T);
else
T=LR(T);
}
T->ht=height(T);
return(T);
}
node * Delete(node *T,int x) {
node *p;
if(T==NULL) {
return NULL;
}
else
if(x > T->data) // insert in right subtree
{
T->right=Delete(T->right,x);
if(BF(T)==2)
if(BF(T->left)>=0)
T=LL(T);
else
T=LR(T);
}
else
if(x<T->data)
{
T->left=Delete(T->left,x);
if(BF(T)==-2) //Rebalance during
windup if(BF(T->right)<=0)
T=RR(T);
else
T=RL(T);
}
else {
//data to be deleted is
found if(T->right!=NULL)
{ //delete its inorder succesor
p=T->right;
while(p->left!= NULL)
p=p
->left;
T->data=p->data;
T->right=Delete(T->right,p->data);
if(BF(T)==2)//Rebalance during
windup if(BF(T->left)>=0)
T=LL(T);
else
T=LR(T);
}
else
return(T->left);
}
T->ht=height(T);
return(T);
}
int height(node *T) {
int lh,rh;
if(T==NULL)
return(0);
if(T->left==NULL)
lh=0;
else
lh=1+T->left->ht;
if(T->right==NULL)
rh=0;
else
rh=1+T->right->ht;
if(lh>rh)
return(lh);
return(rh);
}
node * rotateright(node *x) {
node *y;
y=x->left;
x->left=y->right;
y->right=x;
x->ht=height(x);
y->ht=height(y);
return(y);
}
node * rotateleft(node *x)
{ node *y;
y=x->right;
x->right=y->left;
y->left=x;
x->ht=height(x);
y->ht=height(y);
return(y);
}
node * RR(node *T) {
T=rotateleft(T);
return(T);
}
node * LL(node *T) {
T=rotateright(T);
return(T);
}
node * LR(node *T) { T->left=rotateleft(T->left);
T=rotateright(T);
return(T);
}
node * RL(node *T) { T->right=rotateright(T->right);
T=rotateleft(T);
return(T);
}
int BF(node *T) {
int lh,rh;
if(T==NULL)
return(0);
if(T->left==NULL)
lh=0;
else
lh=1+T->left->ht;
if(T->right==NULL)
rh=0;
else
rh=1+T->right->ht;
return(lh-rh);
}
void preorder(node *T) {
if(T!=NULL) {
printf("%d(Bf=%d)",T->data,BF(T));
preorder(T->left);
preorder(T->right);
}
}
void inorder(node *T) {
if(T!=NULL) {
inorder(T->left); printf("%d(Bf=
%d)",T->data,BF(T)); inorder(T-
>right);
}
}
OUTPUT
7. Write a program for the implementation of BFS and DFS for a given graph

a) Birth First Search

#include<stdio.h>
int a[20][20], q[20], visited[20], n, i, j, f = 0, r = -1;
void bfs(int v) {
for(i = 1; i <= n; i++)
if(a[v][i] && !visited[i])
q[++r] = i;
if(f <= r) {
visited[q[f]] = 1;
bfs(q[f++]);
}
}
void main() {
int v;
printf("\n Enter the number of vertices:");
scanf("%d", &n);
for(i=1; i <= n; i++)
{ q[i] = 0;
visited[i] = 0;
}
printf("\n Enter graph data in matrix form:\n");
for(i=1; i<=n; i++) {
for(j=1;j<=n;j++) {
scanf("%d", &a[i][j]);
}
}
printf("\n Enter the starting vertex:");
scanf("%d", &v);
bfs(v);
printf("\n The nodes which are reachable are:\n");
for(i=1; i <= n; i++) {
if(visited[i])
printf("%d\t", i);
else {
printf("\n Bfs is not possible. Not all nodes are reachable");
break;
}
}
}
OUTPUT
b) Depth First Search

#include<stdio.h>
void DFS(int);
int G[10][10],visited[10],n; //n is no of vertices and graph is sorted in array G[10][10]
void main()
{
int i,j;
printf("Enter number of vertices:");
scanf("%d",&n);
//read the adjacency matrix
printf("\nEnter adjecency matrix of the
graph:"); for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&G[i][j]);
//visited is initialized to zero
for(i=0;i<n;i++)
visited[i]=0;
DFS(0);
}
void DFS(int i)
{
int j; printf("\n
%d",i);
visited[i]=1;
for(j=0;j<n;j++)
if(!visited[j]&&G[i][j]==1)
DFS(j);
}
OUTPUT
8. a) Linear search

#include<stdio.h>
#include<conio.h>
int main()
{
int arr[10], i, num,
pos; clrscr();
printf("Enter any 10 Numbers:
"); for(i=0; i<10; i++)
scanf("%d", &arr[i]); printf("\
nEnter a Number to Search: ");
scanf("%d", &num);
for(i=0; i<10; i++)
{
if(arr[i]==num)
{
pos=i;
break;
}
}
printf("\nFound at Index No.%d", pos);
getch();
return 0;
}
OUTPUT
8. b) Binary search

#include <stdio.h>
int main()
{
int i, low, high, mid, n, key, array[100];
printf("Enter number of elements:\n");
scanf("%d",&n);
printf("Enter %d integers:\n", n);
for(i = 0; i < n; i++)
scanf("%d",&array[i]);
printf("Enter value to find \n");
scanf("%d", &key);
low = 0;
high = n - 1;
mid = (low+high)/2;
while (low <= high) {
if(array[mid] < key)
low = mid + 1;
else if (array[mid] == key) {
printf("%d found at location %d \n", key,
mid+1); break;
}
else
high = mid - 1;
mid = (low + high)/2;
}
if(low > high)
printf("Not found! %d isn't present in the list \n", key);
return 0;
}
OUTPUT
9.a) Bubble sort

#include<stdio.h>
int main()
{
int a[10],i,j,temp,n;
printf("\n Enter the max no.of Elements to Sort: \
n"); scanf("%d",&n);
printf("\n Enter the Elements : \
n"); for(i=0; i<n; i++)
{
scanf("%d",&a[i]);
}
for(i=0; i<n; i++)
for(j=i+1; j<n; j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
printf("Sorted Elements :\n");
for(i=0; i<n; i++)
{
printf("%d\t",a[i]);
}
return 0;
}
OUTPUT
9. b)Insertion Sort

#include <stdio.h>
int main(void)
{
int n, i, j,
temp; int
arr[64];
printf("Enter number of elements\
n"); scanf("%d", &n);
printf("Enter %d integers\n",
n); for (i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
for (i = 1; i < n; i++)
{
j = i;
while (j > 0 && arr[j - 1] > arr[j])
{
temp = arr[j];
arr[j] = arr[j -
1]; arr[j - 1] =
temp; j--;
}
}
printf("Sorted list in ascending order:\
n"); for (i = 0; i < n; i++)
{
printf("%d\n", arr[i]);
}
return 0;
}
OUTPUT
9. c) Selection Sort

#include<stdio.h>
int main()
{
int a[10],i;
int j,temp,num;
clescr();

printf("Enter the number to give\


n"); scanf("%d",&num);

for(i=0; i<num; i++){


printf("a[%d]=\t",i);
scanf("%d",&a[i]);
}

for(i=0; i<num-1; i++){


for(j=i+1;j<num; j++){
if(a[i]>a[j]){
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf("Selection Sort \n");
for(i=0; i<num; i++)
{
printf("%d\n", a[i]);
}

return 0;
}
OUTPUT
9. d)Radix Sort

#include<stdio.h>

void Radixsort(int[], int);

void main() {
int x[20], i, n;
printf("\n Enter the no of element to be
sorted:"); scanf("%d", &n);
printf("\n Enter %d elements:", n);
for (i = 0; i < n; i++)
scanf("%d", &x[i]);
Radixsort(x, n);
printf("\n The sorted array is:\n");
for (i = 0; i < n; i++)
printf("%4d", x[i]);
}

void Radixsort(int a[], int n) {


int bucket[10][10], buck[10];
int i, j, k, l, num, div, large, pass;
div = 1;
num = 0;
large = a[0];
for (i = 0; i < n; i++) {
if (a[i] > large)
large = a[i];
}
while (large > 0) {
num = num + 1;
large = large / 10;
}
for (pass = 0; pass < num; pass++) {
for (k = 0; k < 10; k++)
buck[k] = 0;
for (i = 0; i < n; i++) {
l = (a[i] / div) % 10;
bucket[l][buck[l]++] =
a[i];
}
i = 0;
for (k = 0; k < 10; k++) {
for (j = 0; j < buck[k]; j++)
a[i++] = bucket[k][j];
}
div = div * 10;
}
}
OUTPUT

You might also like