You are on page 1of 74

HARSHARAN DEEP

1 /* wap to traverse the array */

#include<stdio.h>

#include<conio.h>

void main()

int a[10],i,n;

clrscr();

printf("enter n");

scanf("%d",&n);

printf("enter the element");

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

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

printf("print elements");

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

printf("\n %d",a[i]);

getch();

Output:-

Page 1 of 74
Page 2 of 74
HARSHARAN DEEP

/*wap for insertion in array */

#include<stdio.h>

#include<conio.h>

void main()

int a[5],i,n,k,item;

clrscr();

printf("enter n");

scanf("%d",&n);

printf("enter the elements of an array");

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

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

printf("\n elements of array");

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

printf("\n %d",a[i]);

printf("\n enter the position");

scanf("%d",&k);

printf("\n enter the item");

scanf("%d",&item);

Page 3 of 74
for(i=n;i>=k;i--)

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

a[k]=item;

n=n+1;

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

printf("\n elements before insertion%d",a[i]);

getch();

Output:-

Page 4 of 74
HARSHARAN DEEP

3. /*wap to delete the element of array */

#include<stdio.h>

#include<conio.h>

void main()

int a[5],i,n,k;

clrscr();

printf("enter n");

scanf("%d",&n);

printf("enter the elements of an array");

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

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

printf("\n elements of array");

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

printf("\n %d",a[i]);

printf("\n enter the position");

scanf("%d",&k);

for(i=n;i>=k;i--)

a[k-1]=a[k];

Page 5 of 74
}

n=n-1;

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

printf("\n elements before insertion%d",a[i]);

getch();

Output:-

Page 6 of 74
HARSHARAN DEEP

4. /*wap to print sparse matrix */

#include<stdio.h>

#include<conio.h>

void main()

int a[20][20],j,i,m,n;

clrscr();

printf("enter no. of rows");

scanf("%d",&m);

printf("enter no. of columns");

scanf("%d",&n);

printf("enter the elements of matrix");

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

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

if(i<=j)

a[i][j]=0;

else{

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

Page 7 of 74
}

printf("the matrix is\n");

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

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

printf("%d\t",a[i][j]);

printf("\n");

getch();

Output:-

Page 8 of 74
2. STACK

HARSHARAN DEEP

1./*wap for push operation.*/

#include<stdio.h>

#include<conio.h>

void main()

int s[10],maxsize=7,top,item,i;

clrscr();

printf("enter no");

scanf("%d",&top);

printf("enter elements in stack");

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

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

printf("elements of stack");

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

printf(" \n %d",s[i]);

printf("\n enter element to be inserted");

scanf("%d",&item);
Page 9 of 74
if(top==maxsize)

printf("overflow");

else

s[top]=item;

top=top+1;

printf("\n after push the value of stack");

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

printf("\n %d",s[i]);

getch();

Output:

Page 10 of 74
HARSHARAN DEEP

2./*wap for pop operation. */

#include<stdio.h>

#include<conio.h>

void main()

int s[10],top,item,i;

clrscr();

printf("enter no");

scanf("%d",&top);

printf("enter elements in stack");

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

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

printf("elements of stack");

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

printf(" \n %d",s[i]);

if(top==0)

Page 11 of 74
printf("underflow");

else

top=top-1;

item=s[top];

printf("\n after pop the value of stack");

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

printf("\n %d",s[i]);

getch();

Output:-

Page 12 of 74
HARSHARAN DEEP

3./* program for peek operaton*/

#include<stdio.h>

#include<conio.h>

void main()

int s[10],top,item,i;

clrscr();

printf("enter no");

scanf("%d",&top);

printf("enter elements in stack");

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

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

printf("elements of stack");

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

printf(" \n %d",s[i]);

if(top==0)

Page 13 of 74
{

printf("underflow");

else

item=s[top];

printf("\n after peek the value of stack");

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

printf("\n %d",s[i]);

getch();

Output:-

Page 14 of 74
HARSHARAN DEEP
4./* wap to reverse the string in stack. */

#include<stdio.h>

#include<conio.h>

#include<string.h>

char stack[20],i;

int top=-1;

void main()

void reverse(char[]);

char str[50];

clrscr();

printf("enter a string:");

gets(str);

reverse(str);

getch();

void reverse(char str[])

int i;

void push(char);

char pop();

Page 15 of 74
int isempty();

for(i=0;i<strlen(str);i++)

char ch=str[i];

push(ch);

while(! isempty())

printf("%c",stack[top]);

top=top-1;

void push(char c)

top=top+1;

stack[top]=c;

char pop()

char c=stack[top];

top=top-1;

return c;

int isempty()

if(top==-1)

return(1);

else

return(0);

Page 16 of 74
} Output:

5./* all operation of stack */HARSHARAN DEEP

#include<stdio.h>

#include<conio.h>

#include<alloc.h>

struct s

int info;

struct s *next;

} *top;

typedef struct s stack;

void create();

void push();

void pop();

void peek();

stack *start,*ptr,*loc;

int data,item,ele,exit;

void main()

int choice;
Page 17 of 74
char ch='y';

clrscr();

while(ch=='y')

printf("\n1. create \n2. push \n3. pop \n4. peek \n5. exit");

printf("\n enter your choice=");

scanf("%d",&choice);

switch(choice)

case 1:

create();

break;

case 2:

push();

break;

case 3:

pop();

break;

case 4:

peek();

break;

case 5:

exit();

fflush(stdin);

printf("\n do you want to continue?=");

Page 18 of 74
ch=getchar();

getch();

void create()

top=(stack*)malloc(sizeof(stack));

ptr=top;

printf("\n enter the data=");

scanf("%d",&data);

ptr->info=data;

ptr->next=NULL;

top=ptr;

peek();

void push()

loc=(stack*)malloc(sizeof(stack));

printf("\n enter the data=");

scanf("%d",&data);

loc->info=data;

loc->next=top;

top=loc;

peek();

void pop()

Page 19 of 74
{

if(top==NULL)

printf("\n stack is empty");

else

loc=top;

item=loc->info;

top=top->next;

free(loc);

printf("\n delete element is=%d",item);

peep();

void peep()

ptr=top;

while(ptr!=NULL)

ele=ptr->info;

printf("\n [%d]",ele);

ptr=ptr->next;

}Output:-

Page 20 of 74
6/*Program for parenthesis checking*/ HARSHARAN DEEP

#include<stdio.h>

#define MAX 30

int top=-1;

int stack[MAX];

void push(char);

char pop();

int match(char a,char b);

main()

char exp[MAX];

int valid;

printf("Enter an algebraic expression : ");

gets(exp);
Page 21 of 74
valid=check(exp);

if(valid==1)

printf("Valid expression\n");

else

printf("Invalid expression\n");

return 0;

int check(char exp[] )

int i;

char temp;

for(i=0;i<strlen(exp);i++)

if(exp[i]=='(' || exp[i]=='{' || exp[i]=='[')

push(exp[i]);

if(exp[i]==')' || exp[i]=='}' || exp[i]==']')

if(top==-1) /*stack empty*/

printf("Right parentheses are more than left parentheses\n");

return 0;

else

temp=pop();

if(!match(temp, exp[i]))

Page 22 of 74
printf("Mismatched parentheses are : ");

printf("%c and %c\n",temp,exp[i]);

return 0;

if(top==-1) /*stack empty*/

printf("Balanced Parentheses\n");

return 1;

else

printf("Left parentheses more than right parentheses\n");

return 0;

int match(char a,char b)

if(a=='[' && b==']')

return 1;

if(a=='{' && b=='}')

return 1;

if(a=='(' && b==')')

return 1;

return 0;

Page 23 of 74
}

void push(char item)

if(top==(MAX-1))

printf("Stack Overflow\n");

return;

top=top+1;

stack[top]=item;

char pop()

if(top==-1)

printf("Stack Underflow\n");

exit(1);

return(stack[top--]);

Output:-

Page 24 of 74
3.Queues
1./* program to process enqueue operation */HARSHARAN DEEP

#include<stdio.h>

#include<conio.h>

void main()

int q[10],front=1,rear=0,maxsize=10,item,i;

clrscr();

printf("enter rear");

scanf("%d",&rear);

printf("enter elements of queue");

for(i=1;i<=rear;i++)

Page 25 of 74
scanf("%d",&q[i]);

printf("\n elements of queue");

for(i=1;i<=rear;i++)

printf("%d\n",q[i]);

printf("enter item");

scanf("%d",&item);

if(rear==maxsize)

printf("overflow");

else

rear=rear+1;

q[rear]=item;

printf("after push the value of queue");

for(i=1;i<=rear;i++)

printf("\n %d",q[i]);

getch();

Output:-

Page 26 of 74
2/* program to carry out dequeue operation */ HARSHARAN DEEP

#include<stdio.h>

#include<conio.h>

void main()

int q[10],front,rear,maxsize=10,item,i;

clrscr();

printf("enter no.");

scanf("%d",&front);

printf("enter elements of queue");

for(i=1;i<=front;i++)

scanf("%d",&q[i]);
Page 27 of 74
}

printf("elements of queue");

for(i=1;i<=front;i++)

printf("%d\n",q[i]);

if(rear==0)

printf("underflow");

item=q[front];

if(front<rear);

front=front+1;

} }

else

front=1;

rear=0;

printf("element deleted");

for(i=1;i<=front;i++)

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

getch();

Page 28 of 74
Output:-

4.Linked list
1./* wap for traversing a link list */HARSHARAN DEEP

#include<stdio.h>

#include<conio.h>

#define NULL 0

typedef struct nodetype

int info;

struct nodetype *link;

}node;

void main()

Page 29 of 74
node* create();

void traverse(node*);

int data;

node *head,*loc,*ptr;

clrscr();

head=create();

printf("linked list :\n");

traverse(head);

printf("\n end of program---\n");

ptr=head;

while(ptr!=NULL)

ptr=ptr->link;

free(head);

head=ptr;

getch();

node* create()

node *ptr,*head=NULL;

char ch='y';

int data;

while(ch=='y'||ch=='y')

if(head==NULL)

Page 30 of 74
{

head=(node*)malloc(sizeof(node));

ptr=head;

else

ptr->link=(node*)malloc(sizeof(node));

ptr=ptr->link;

printf ("enter info nodes");

scanf("%d",&data);

ptr->info=data;

fflush(stdin);

printf("want to enter more nodes:(Y or N)=");

scanf("%c",&ch);

ptr->link=NULL;

return(head);

void traverse(node *head)

node* ptr=head;

while(ptr!=NULL)

printf("\n info of node is \t %d",ptr->info);

ptr=ptr->link;

Page 31 of 74
}

return;

Output:-

2./*wap for searcing a node in linked list*/ HARSHARAN DEEP

#include<stdio.h>

#include<conio.h>

#define NULL 0

typedef struct nodetype

int info;

struct nodetype *link;

}node;

Page 32 of 74
void main()

node* create();

void traverse(node*);

node* search(node*,int);

int item,data;

node *head,*loc,*ptr;

clrscr();

head=create();

printf("linked list before insertion:\n");

traverse(head);

printf("\n enter item to search from linked list");

scanf("%d",&item);

search(head,item);

printf("\n end of program---\n");

ptr=head;

while(ptr!=NULL)

ptr=ptr->link;

free(head);

head=ptr;

getch();

node* create()

Page 33 of 74
node *ptr,*head=NULL;

char ch='y';

int data;

while(ch=='y'||ch=='y')

if(head==NULL)

head=(node*)malloc(sizeof(node));

ptr=head;

else

ptr->link=(node*)malloc(sizeof(node));

ptr=ptr->link;

printf ("enter info of new node");

scanf("%d",&data);

ptr->info=data;

fflush(stdin);

printf("want to enter more nodes:(Y or N)=");

scanf("%c",&ch);

ptr->link=NULL;

return(head);

void traverse(node *head)

Page 34 of 74
{

node* ptr=head;

while(ptr!=NULL)

printf("\n info of node is \t %d",ptr->info);

ptr=ptr->link;

return;

node* search(node *head,int item)

node *loc,*ptr;

loc=NULL;

ptr=head;

while ((ptr!=NULL)&&(item!=ptr->info))

ptr=ptr->link;

printf("searched item=%d",item);

if(item==ptr->info)

loc=ptr;

else

printf("nothing found");

return(loc);

Page 35 of 74
}

Output:-

3./* wap to insert node at given location in link list */ HARSHARAN DEEP

#include<stdio.h>

#include<conio.h>

#define NULL 0

typedef struct nodetype

{
Page 36 of 74
int info;

struct nodetype *link;

}node

void main()

node *create();

void traverse(node*);

node *search(node*,int);

void instloc(node**,int, node*);

int item,data;

node *head,*loc,*ptr;

clrscr();

head=create();

printf("linked list before insertion:\n");

traverse(head);

printf("\n enter item to search from linked list");

scanf("%d",&item);

loc=search(head,item);

printf("\n enter info to be inserted after searched item=");

scanf("%d",&data);

printf("linked list after insertion\n");

traverse(head);

printf("\n end of program---\n");

ptr=head;

while(ptr!=NULL)

Page 37 of 74
ptr=ptr->link;

free=(head)

head=ptr;

getch();

node* create()

node* ptr*,head=NULL;

char ch='y';

int data

while(ch=='y'||ch=='y')

if(head==NULL)

head=(node*)malloc(sizeof(node));

ptr=head;

else

ptr->link=(node*)malloc(sizeof (node));

ptr=ptr->link;

printf ("enter info of new node");

scanf("%d",&data);

Page 38 of 74
ptr->info=data;

fflush(stdin);

printf("want to continue:(Y or N)=");

scanf("%d",&ch);

ptr->link=NULL;

return(head);

node* ptr=head;

while(ptr!=NULL)

printf("\n info of node is \t %d",ptr->info);

ptr=ptr->link;

return;

node* search(node*head,int item)

node*loc;*ptr;

loc=NULL;

ptr=head;

while ((ptr!=NULL)&&(item!=ptr->info))

ptr=ptr->link;

if(item==ptr->info)

loc=ptr;

return(loc);

Page 39 of 74
}

void int loc(node* *head,int,item,node*,loc)

node* new;

new=(node*)malloc(sizeof (node));

new->info=item;

if(loc==NULL)

new->link=*head;

else

new->link=loc->link;

loc->link=new;

} Output

4./* program to insert a node in begining of a link list */ HARSHARAN DEEP

#include<stdio.h>

#include<conio.h>
Page 40 of 74
#define NULL 0

typedef struct nodetype

int info;

struct nodetype *link;

}node;

void main()

node* create();

void traverse(node*);

node* search(node*,int);

void instbeg(node**,int, node*);

int item,data;

node *head,*avail,*ptr;

clrscr();

head=create();

printf("\n linked list before insertion:\n");

traverse(head);

printf("\n enter item to be inserted= \n");

scanf("%d",&item);

instbeg(&head,item,avail);

printf("\n linked list after insertion \n");

traverse(head);

printf("\n End of The program---\n");

ptr=head;

while(ptr!=NULL)

Page 41 of 74
{

ptr=ptr->link;

free(head);

head=ptr;

getch();

node* create()

node *ptr,*head=NULL;

char ch='y';

int data;

while(ch=='y'||ch=='y')

if(head==NULL)

head=(node*)malloc(sizeof(node));

ptr=head;

else

ptr->link=(node*)malloc(sizeof(node));

ptr=ptr->link;

printf ("Enter value of node:-");

scanf("%d",&data);

Page 42 of 74
ptr->info=data;

fflush(stdin);

printf("Want to continue:(Y or N)=");

scanf("%c",&ch);

ptr->link=NULL;

return(head);

void traverse(node *head)

node* ptr=head;

while(ptr!=NULL)

printf("\n Info of node is:- \t %d",ptr->info);

ptr=ptr->link;

return;

void instbeg(node** head,int item,node *avail)

node* new;

new=(node*)malloc(sizeof (node));

new->info=item;

if(avail==NULL)

printf("overflow");

Page 43 of 74
}

else

new->link=*head;

*head=new;

}}

Output:-

Page 44 of 74
5./* program to insert node at the end of link list */ HARSHARAN DEEP

#include<stdio.h>

#include<conio.h>

#define NULL 0

typedef struct nodetype

int info;

struct nodetype *link;

}node;

void main()

node* create();

void traverse(node*);

node* search(node*,int);

void instend(node**,int, node*);

int item,data;

node *head,*avail,*ptr;

clrscr();

head=create();

printf("linked list before insertion:\n");

traverse(head);

printf("\n enter info to be inserted=");

scanf("%d",&item);

instend(&head,item,avail);

printf("linked list after insertion\n");

Page 45 of 74
traverse(head);

printf("\n end of program---\n");

ptr=head;

while(ptr!=NULL)

ptr=ptr->link;

free(head);

head=ptr;

getch();

node* create()

node *ptr,*head=NULL;

char ch='y';

int data;

while(ch=='y'||ch=='y')

if(head==NULL)

head=(node*)malloc(sizeof(node));

ptr=head;

else

ptr->link=(node*)malloc(sizeof(node));

Page 46 of 74
ptr=ptr->link;

printf ("enter value of node");

scanf("%d",&data);

ptr->info=data;

fflush(stdin);

printf("want to continue:(Y or N)=");

scanf("%c",&ch);

ptr->link=NULL;

return(head);

void traverse(node *head)

node* ptr=head;

while(ptr!=NULL)

printf("\n info of node is \t %d",ptr->info);

ptr=ptr->link;

return;

void instend(node** head,int item,node *avail)

node* new,*ptr;

new=(node*)malloc(sizeof (node));

Page 47 of 74
new->info=item;

if(avail==NULL)

printf("overflow");

else

new->link=NULL;

ptr=*head;

while(ptr->link!=NULL)

ptr=ptr->link;

ptr->link=new;

Output:-

Page 48 of 74
6./* deleting a given node in link list */ HARSHARAN DEEP

#include<stdio.h>

#include<conio.h>

#define NULL 0

typedef struct nodetype

int info;

struct nodetype *link;

}node;

void main()

node* create();

void traverse(node*);

node* search(node*,int);

void delloc(node**,int, node*);

int item,data;

node *head,*loc,*ptr;

clrscr();

head=create();

printf("linked list before deletion:\n");

traverse(head);

printf("\n enter item to search from linked list");

Page 49 of 74
scanf("%d",&item);

loc=search(head,item);

printf("\n enter info to be deleted =");

scanf("%d",&data);

delloc(&head,data,loc);

printf("linked list after deletion\n");

traverse(head);

printf("\n end of program---\n");

ptr=head;

while(ptr!=NULL)

ptr=ptr->link;

free(head);

head=ptr;

getch();

node* create()

node *ptr,*head=NULL;

char ch='y';

int data;

while(ch=='y'||ch=='y')

if(head==NULL)

Page 50 of 74
head=(node*)malloc(sizeof(node));

ptr=head;

else

ptr->link=(node*)malloc(sizeof(node));

ptr=ptr->link;

printf ("enter info of new node");

scanf("%d",&data);

ptr->info=data;

fflush(stdin);

printf("want to continue:(Y or N)=");

scanf("%c",&ch);

ptr->link=NULL;

return(head);

void traverse(node *head)

node* ptr=head;

while(ptr!=NULL)

printf("\n info of node is \t %d",ptr->info);

ptr=ptr->link;

Page 51 of 74
return;

node* search(node *head,int item)

node *loc,*ptr;

loc=NULL;

ptr=head;

while ((ptr!=NULL)&&(item!=ptr->info))

ptr=ptr->link;

printf("searched item=%d",item);

if(item==ptr->info)

loc=ptr;

return(loc);

void delloc(node** head,int item,node *loc)

node* new,*ptr,*ploc,*avail;

new=(node*)malloc(sizeof (node));

new->info=item;

if(*head==NULL)

printf("underflow");

ptr=*head;

ploc=NULL;

while((ptr!=loc)&&(ptr->link!=NULL))

Page 52 of 74
{

ploc=ptr;

ptr=ptr->link;

if(ptr!=loc)

printf("node not found");

if(ploc==NULL)

*head=(*head)->link;

else

ploc->link=loc->link;

loc->link=avail;

avail=loc;

Page 53 of 74
}Output:-

7./*w.a.p.to print and insert elements in TREE*/ HARSHARAN DEEP

#include<stdio.h>

#include<conio.h>

struct t

int info;

struct t *left, *right;

};

Page 54 of 74
typedef struct t tree;

void inorder(tree *ptr);

void preorder(tree *ptr);

void postorder(tree *ptr);

tree *ptr,*pptr,*nptr,*root;

int data;

void main()

char ch='y';

clrscr();

root=NULL;

while(ch=='y')

printf("\n enter the data:");

scanf("%d",&data);

ptr=(tree *)malloc(sizeof(tree));

ptr->info=data;

ptr->left=NULL;

ptr->right=NULL;

if(root==NULL)

root=ptr;

else

pptr=NULL;

nptr=root;

while(nptr!=NULL)

Page 55 of 74
{

pptr=nptr;

if(data<nptr->info)

nptr=nptr->left;

else

nptr=nptr->right;

if(data<pptr->info)

pptr->left=ptr;

else

{pptr->right=ptr;

printf("\n do u want to continue(y/n):");

ch=getche();

printf("\n inorder of tree is:");

inorder(root);

printf("\n preorder of tree is:");

preorder(root);

Page 56 of 74
printf("\n postorder of tree is:");

postorder(root);

getch();

void inorder(tree *p)

if(p!=NULL)

inorder(p->left);

printf("[%d]",p->info);

inorder(p->right);

void preorder(tree *p)

if(p!=NULL)

printf("[%d]",p->info);

preorder(p->left);

preorder(p->right);

void postorder(tree *p)

if(p!=NULL)

Page 57 of 74
postorder(p->left);

postorder(p->right);

printf("[%d]",p->info);

Output:-

4.Searching and Sorting

1. /* wap for linear search */ HARSHARAN DEEP

#include<stdio.h>

Page 58 of 74
#include<conio.h>

void main()

int a[10],i,n,loc,item;

clrscr();

printf("enter n");

scanf("%d",&n);

printf("enter the list");

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

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

printf("enter item to be searched");

scanf("%d",&item);

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

if(item==a[i])

printf("\n %d is found at position %d",item,i);

break;

if(i==n)

printf("\n %d item is not found",item);

getch();

Page 59 of 74
Output:-

2/* program for binary search */ HARSHARAN DEEP

#include<stdio.h>
Page 60 of 74
#include<conio.h>

#define MAX 10

void main()

int a[MAX],n,item,beg,end,mid,loc,i;

clrscr();

printf("Enter no of elments:");

scanf("%d",&n);

printf("Elments %d elements of Array",n);

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

Printf(a[%d],i);

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

printf("Enter item ");

scanf("%d",&item);

beg=0;end=MAX-1;mid=(beg+end)/2;

while(beg<=end&&a[mid]!=item)

if(item<a[mid])

end=mid-1;

else

beg=mid+1;

mid=(beg+end)/2;

if(a[mid]==item)

Page 61 of 74
printf("\n %d is present at %d location",item,mid+1);

else

printf("location is -1");

getch();

Output:

3. /* sorting using bubble sort */ HARSHARAN DEEP

Page 62 of 74
#include<stdio.h>

#include<conio.h>

void main()

int a[10],i,n,j,temp;

clrscr();

printf("enter n:-");

scanf("%d",&n);

printf("enter %d elements of array:-",n);

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

Printf(a[%d],i);

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

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

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

if(a[j]>a[j+1])

temp=a[j];

a[j]=a[j+1];

a[j+1]=temp;

Page 63 of 74
printf("after sorting array:-");

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

printf("\na[%d]=%d"n,,a[i]);

getch();

Output:-

Page 64 of 74
5/* sorting by insertion sort */ HARSHARAN DEEP

#include<stdio.h>

#include<conio.h>

void main()

int a[10],n,i,target,j;

clrscr();

printf("enter n");

scanf("%d",&n);

printf("enter %d elements of array \n");

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

Printf(a[%d],i);

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

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

target=a[i];

j=i-1;

while(target<a[j]&&j>=1)

a[j+1]=a[j];

j=j-1;

a[j+1]=target;

Page 65 of 74
printf("after sorted elements:");

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

printf("\n%d",a[i]);

getch();

Output:-

Page 66 of 74
6./*sorting using selection sort.*/ HARSHARAN DEEP

#include<stdio.h>

#include<conio.h>

void main()

int a[20],i,n,j,temp,loc;

clrscr();

printf("enter n");

scanf("%d",&n);

printf("enter elements of array");

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

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

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

loc=i;

for(j=i+1;j<=n;j++)

if (a[j]<a[loc])

loc=j;

temp=a[i];

a[i]=a[loc];

a[loc]=temp;

Page 67 of 74
}

printf("after sorting the elements are:");

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

printf("\n%d",a[i]);

getch();

Output:-

Page 68 of 74
7./* program to implement quick sort */ HARSHARAN DEEP
#include<stdio.h>

#include<conio.h>

void quick_sort(int arr[20],int,int);


void main()
{
int arr[20],n,i;
clrscr();
printf("Enter the number of elements in the Array: ");
scanf("%d",&n);
printf("\nEnter %d elements:\n\n",n);
for(i=0 ; i<n ; i++)
{
printf(" Array[%d] = ",i);
scanf("%d",&arr[i]);
}
quick_sort(arr,0,n-1);
printf("\nThe Sorted Array is:\n\n");
for(i=0 ; i<n ; i++)
{
printf(" %4d",arr[i]);
}
getch();
}
void quick_sort(int arr[20],int low,int high)
{
int pivot,j,temp,i;
if(low<high)
{
pivot = low;
i = low;
j = high;
while(i<j)
{
while((arr[i]<=arr[pivot])&&(i<high))
{
i++;
}
while(arr[j]>arr[pivot])
{
j--;
Page 69 of 74
}
if(i<j)
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
temp=arr[pivot];
arr[pivot]=arr[j];
arr[j]=temp;
quick_sort(arr,low,j-1);
quick_sort(arr,j+1,high);
}
}
Output

Page 70 of 74
8./* program for merge sort */

#include <stdio.h>

#include<conio.h>

void mergesort(int arr[], int l, int h);

void main(void)

int array[100],n,i = 0;

clrscr();

printf("Enter the number of elements to be sorted: ");

scanf("%d",&n);

printf("\nEnter the elements to be sorted: \n");

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

printf("\tArray[%d] = ",i);

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

}printf("\nBefore Mergesort:");

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

printf("%4d", array[i]);

printf("\n");

mergesort(array, 0, n - 1);

printf("\nAfter Mergesort:"); //Array After Mergesort

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

Page 71 of 74
{

printf("%4d", array[i]);

printf("\n");

getch();

}void mergesort(int arr[], int l, int h)

int i = 0;

int length = h - l + 1;

int pivot = 0;

int merge1 = 0;

int merge2 = 0;

int temp[100];

if(l == h)

return;

pivot = (l + h) / 2;

mergesort(arr, l, pivot);

mergesort(arr, pivot + 1, h);

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

temp[i] = arr[l + i];

merge1 = 0;

merge2 = pivot - l + 1;

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

Page 72 of 74
if(merge2 <= h - l)

if(merge1 <= pivot - l)

if(temp[merge1] > temp[merge2])

arr[i + l] = temp[merge2++];

else

arr[i + l] = temp[merge1++];

}}

else

arr[i + l] = temp[merge2++];

}}

else

arr[i + l] = temp[merge1++];

Page 73 of 74
}}} Output

Page 74 of 74

You might also like