Data Structure Algorithm | DSA | Programs Solved
SINGLY LINKED LIST
//Include Header Files
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
//Function Declarations
void insertAtBegin(int);
void insertAtEnd(int);
void insertInBetween(int);
void deleteAtBegin();
void deleteAtEnd();
void deleteInBetweenPos();
void deleteInBetweenKey();
void reverse();
void sort();
void display();
int countElements();
//Define Structure for Linked List
struct node
{
int data;
node *link;
};
node *root=NULL;
void main()
{
int item;
int choice,ch;
printf(“\n******************************************************************”);
printf(“\n\t\t\t* Singly Linked List *”);
printf(“\n******************************************************************\n”);
do
{
printf(“\n* Enter Your Choice *”);
printf(“\n\t1. Insert”);
printf(“\n\t2. Delete”);
printf(“\n\t3. Display”);
printf(“\n\t4. Count No of Elements”);
printf(“\n\t5. Reverse”);
printf(“\n\t6. Sort the Linked List”);
printf(“\n\t7. Exit\nChoice : “);
scanf(“%d”,&choice);
switch(choice)
{
case 1:
do
{
printf(“\n\t* Enter Ur Choice *”);
printf(“\n\t\t1. Insert at Begin”);
printf(“\n\t\t2. Insert at End”);
printf(“\n\t\t3. Insert in Between”);
printf(“\n\t\t4. Goto Main Menu\n\tChoice : “);
scanf(“%d”,&ch);
if(ch>=1&&ch<=3)
{
printf(“\nEnter the Data : “);
scanf(“%d”,&item);
}
switch(ch)
{
case 1:
insertAtBegin(item);
printf(“\n*** The Item %d is Inserted ***\n”,item);
break;
case 2:
insertAtEnd(item);
printf(“\n*** The Item %d is Inserted ***\n”,item);
break;
case 3:
insertInBetween(item);
break;
case 4:
break;
default:
printf(“\n*** Enter the Valid Choice ***\n”);
}
}while(ch!=4);
break;
case 2:
do
{
printf(“\n\t* Enter Ur Choice *”);
printf(“\n\t\t1. Delete at Begin”);
printf(“\n\t\t2. Delete at End”);
printf(“\n\t\t3. Delete the Element by Specifying the Position”);
printf(“\n\t\t4. Delete the Element by Specifying the Key”);
printf(“\n\t\t5. Goto Main Menu\n\tChoice : “);
scanf(“%d”,&ch);
switch(ch)
{
case 1:
deleteAtBegin();
break;
case 2:
deleteAtEnd();
break;
case 3:
deleteInBetweenPos();
break;
case 4:
deleteInBetweenKey();
break;
case 5:
break;
default:
printf(“\n*** Enter the Valid Choice ***\n”);
}
}while(ch!=5);
break;
case 3:
display();
break;
case 4:
printf(“\n*** Total No of Elements : %d ***\n”,countElements());
break;
case 5:
reverse();
display();
break;
case 6:
sort();
display();
break;
case 7:
printf(“\n\t\t\t*** Program Ends ***\n”);
break;
default:
printf(“\n*** Enter the Valid Choice ***\n”);
}
}while(choice!=7);
getch();
}
int countElements()
{
//This Function Will Return No of Elements in LL
node *pre,*cur;
int cnt=1;
if(root==NULL)
return(0);
else if(root->link==NULL)
return(1);
else
{
cur=root;
while(cur!=NULL)
{
pre=cur;
cur=cur->link;
cnt++;
}
return(cnt-1);
}
}
void insertAtBegin(int c)
{
node *p;
p=(node *)malloc(sizeof(node));
p->data=c;
p->link =NULL;
if(root==NULL) //if no elements in LL
root=p;
else
{
p->link=root;
root=p;
}
}
void insertAtEnd(int c)
{
node *p,*cur;
p=(node *)malloc(sizeof(node));
p->data=c;
p->link=NULL;
if(root==NULL)
root=p;
else if(root->link==NULL)
root->link=p;
else
{
cur=root;
while(cur->link!=NULL)
{
cur=cur->link;
}
cur->link=p;
}
}
void insertInBetween(int c)
{
int pos,i,cnt;
node *p,*cur,*pre;
p=(node *)malloc(sizeof(node));
p->data=c;
p->link=NULL;
printf(“\nEnter the Position U want to Insert : “);
scanf(“%d”,&pos);
cnt=countElements();
if(pos>0&&pos<=(cnt+1))
{
if(pos==1)
insertAtBegin(c);
else
{
cur=root;
for(i=1;i<pos;i++)
{
pre=cur;
cur=cur->link;
}
if(pre->link==NULL)
insertAtEnd(c);
else
{
pre->link=p;
p->link=cur;
}
}
printf(“\n*** The Item %d is Inserted ***\n”,c);
}
else
printf(“\n*** Invalid Position ***\n”);
}
void display()
{
node *cur;
cur=root;
if(root==NULL)
printf(“\n*** No Elements in the Linked List ***\n”);
else
{
printf(“\n*************************”);
printf(“\n\t* Elements Are *”);
printf(“\n*************************\n\n”);
while(cur!=NULL)
{
printf(“%d –> “,cur->data);
cur=cur->link;
}
printf(“NULL\n”);
}
}
void deleteAtBegin()
{
node *p;
if(root==NULL)
printf(“\n*** No Elements in the Linked List ***\n”);
else if(root->link==NULL)
{
printf(“\n*** The Item %d is Deleted ***\n”,root->data);
free(root);
root=NULL;
}
else
{
p=root->link;
root->link=NULL;
printf(“\n*** The Item %d is Deleted ***\n”,root->data);
free(root);
root=p;
}
}
void deleteAtEnd()
{
node *pre,*cur;
if(root==NULL)
printf(“\n*** No Elements in the Linked List ***\n”);
else if(root->link==NULL)
{
printf(“\n*** The Item %d is Deleted ***\n”,root->data);
free(root);
root=NULL;
}
else
{
cur=root;
while(cur->link!=NULL)
{
pre=cur;
cur=cur->link;
}
pre->link=NULL;
printf(“\n*** The Item %d is Deleted ***\n”,cur->data);
free(cur);
}
}
void deleteInBetweenPos()
{
int cnt,pos,i;
node *pre,*cur;
cnt=countElements();
if(cnt>0)
{
printf(“\nEnter the Position U want to Delete : “);
scanf(“%d”,&pos);
if(pos>=1&&pos<=cnt)
{
if(pos==1)
deleteAtBegin();
else
{
cur=root;
for(i=1;i<pos;i++)
{
pre=cur;
cur=cur->link;
}
if(cur->link==NULL)
deleteAtEnd();
else
{
pre->link=cur->link;
cur->link=NULL;
printf(“\n*** The Item %d is Deleted ***\n”,cur->data);
free(cur);
}
}
}
else
printf(“\n*** Invalid Position ***\n”);
}
else
printf(“\n*** No Elements in the Linked List ***\n”);
}
void deleteInBetweenKey()
{
int key,cnt;
node *pre,*cur;
cnt=countElements();
if(cnt>0)
{
printf(“\nEnter the Key U want to Delete : “);
scanf(“%d”,&key);
if(root==NULL)
printf(“\n*** No Elements in the Linked List ***\n”);
else if(root->link==NULL)
{
if(root->data==key)
{
printf(“\n*** The Key %d is Deleted ***\n”,root->data);
free(root);
root=NULL;
}
else
printf(“\n*** The Given Key is Not Matched ***\n”);
}
else
{
pre=root;
cur=root->link;
if(root->data==key)
{
root->link=NULL;
printf(“\n*** The Key %d is Deleted ***\n”,root->data);
free(root);
root=cur;
}
else
{
while(cur!=NULL)
{
if(cur->data==key)
break;
pre=cur;
cur=cur->link;
}
if(cur!=NULL)
{
pre->link=cur->link;
cur->link=NULL;
printf(“\n*** The Key %d is Deleted ***\n”,cur->data);
free(cur);
}
else
printf(“\n*** The Given Key is Not Matched ***\n”);
}
}
}
else
printf(“\n*** No Elements in the Linked List ***\n”);
}
void reverse()
{
node *pre,*cur,*next;
if(root!=NULL)
{
pre=NULL;
cur=root;
next=cur->link;
while(next!=NULL)
{
cur->link=pre;
pre=cur;
cur=next;
next=next->link;
}
cur->link=pre;
root=cur;
}
}
void sort()
{
node *pre,*cur;
int temp;
if(root!=NULL&&root->link!=NULL)
{
pre=root;
while(pre!=NULL)
{
cur=pre->link;
while(cur!=NULL)
{
if(pre->data>cur->data)
{
temp=pre->data;
pre->data=cur->data;
cur->data=temp;
}
cur=cur->link;
}
pre=pre->link;
}
}
}
DOUBLY LINKED LIST
//Include Header Files
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
//Function Declarations
void insertAtBegin(int);
void insertAtEnd(int);
void insertInBetween(int);
void deleteAtBegin();
void deleteAtEnd();
void deleteInBetweenPos();
void deleteInBetweenKey();
void reverse();
void sort();
void palindrome();
void display();
int countElements();
//Define Structure for Linked List
struct node
{
int data;
node *llink;
node *rlink;
};
node *first=NULL;
node *last=NULL;
void main()
{
int item;
int choice,ch,cnt;
printf(“\n******************************************************************”);
printf(“\n\t\t\tDoubly Linked List”);
printf(“\n******************************************************************\n”);
do
{
printf(“\n* Enter Your Choice * “);
printf(“\n\t1. Insert”);
printf(“\n\t2. Delete”);
printf(“\n\t3. Display”);
printf(“\n\t4. Count No of Elements”);
printf(“\n\t5. Reverse”);
printf(“\n\t6. Sort the Linked List”);
printf(“\n\t7. Check If the LL is Palindrome”);
printf(“\n\t8. Exit\nChoice : “);
scanf(“%d”,&choice);
switch(choice)
{
case 1:
do
{
printf(“\n\t* Enter Ur Choice *”);
printf(“\n\t\t1. Insert at Begin”);
printf(“\n\t\t2. Insert at End”);
printf(“\n\t\t3. Insert in Between”);
printf(“\n\t\t4. Goto Main Menu\n\tChoice : “);
scanf(“%d”,&ch);
if(ch>=1&&ch<=3)
{
printf(“\nEnter the Data : “);
scanf(“%d”,&item);
}
switch(ch)
{
case 1:
insertAtBegin(item);
printf(“\n*** The Item %d is Inserted ***\n”,item);
break;
case 2:
insertAtEnd(item);
printf(“\n*** The Item %d is Inserted ***\n”,item);
break;
case 3:
insertInBetween(item);
break;
case 4:
break;
default:
printf(“\n*** Enter the Valid Choice ***\n”);
}
}while(ch!=4);
break;
case 2:
do
{
printf(“\n\t* Enter Ur Choice *”);
printf(“\n\t\t1. Delete at Begin”);
printf(“\n\t\t2. Delete at End”);
printf(“\n\t\t3. Delete the Element by Specifying the Position”);
printf(“\n\t\t4. Delete the Element by Specifying the Key”);
printf(“\n\t\t5. Goto Main Menu\n\tChoice : “);
scanf(“%d”,&ch);
switch(ch)
{
case 1:
deleteAtBegin();
break;
case 2:
deleteAtEnd();
break;
case 3:
deleteInBetweenPos();
break;
case 4:
deleteInBetweenKey();
break;
case 5:
break;
default:
printf(“\n*** Enter the Valid Choice ***\n”);
}
}while(ch!=5);
break;
case 3:
display();
break;
case 4:
cnt=countElements();
printf(“\n*** Total No of Elements : %d ***\n”,cnt);
break;
case 5:
reverse();
break;
case 6:
sort();
display();
break;
case 7:
palindrome();
break;
case 8:
printf(“\n\t\t\t*** Program Ends ***\n”);
break;
default:
printf(“\n*** Enter the Valid Choice ***\n”);
}
}while(choice!=8);
getch();
}
int countElements()
{
//This Function Will Return No of Elements in LL
node *cur;
int cnt=1;
if(first==NULL&&last==NULL)
return(0);
else if(first->rlink==NULL)
return(1);
else
{
cur=first;
while(cur!=last->rlink)
{
cur=cur->rlink;
cnt++;
}
return(cnt-1);
}
}
void insertAtBegin(int c)
{
node *p;
p=(node *)malloc(sizeof(node));
p->data=c;
p->llink =NULL;
p->rlink =NULL;
if(first==NULL&&last==NULL) //if no elements in LL
{
first=p;
last=p;
}
else
{
p->rlink=first;
first->llink=p;
first=p;
first->llink=NULL;
}
}
void insertAtEnd(int c)
{
node *p;
p=(node *)malloc(sizeof(node));
p->data=c;
p->llink=NULL;
p->rlink=NULL;
if(first==NULL&&last==NULL)
{
first=p;
last=p;
}
else
{
last->rlink=p;
p->llink=last;
last=p;
last->rlink=NULL;
}
}
void insertInBetween(int c)
{
int pos,i,cnt;
node *p,*cur,*pre;
p=(node *)malloc(sizeof(node));
p->data=c;
p->llink=NULL;
p->rlink=NULL;
printf(“\nEnter the Position U want to Insert : “);
scanf(“%d”,&pos);
cnt=countElements();
if(pos>0&&pos<=(cnt+1))
{
if(pos==1)
insertAtBegin(c);
else
{
cur=first;
for(i=1;i<pos;i++)
{
pre=cur;
cur=cur->rlink;
}
if(pre->rlink==NULL)
insertAtEnd(c);
else if(pre!=NULL)
{
pre->rlink=p;
p->rlink=cur;
p->llink=pre;
cur->llink=p;
}
}
printf(“\n*** The Item %d is Inserted ***\n”,c);
}
else
printf(“\n*** Invalid Position ***\n”);
}
void display()
{
node *cur;
cur=first;
if(cur==NULL)
printf(“\n*** No Elements in the Linked List ***\n”);
else
{
printf(“\n******************************”);
printf(“\n\t* Elements are *”);
printf(“\n******************************\n\nNULL <– “);
while(cur!=NULL)
{
if(cur->rlink!=NULL)
printf(“%d <–> “,cur->data);
else
printf(“%d”,cur->data);
cur=cur->rlink;
}
printf(” –> NULL\n”);
}
}
void deleteAtBegin()
{
node *p;
if(first==NULL&&last==NULL)
printf(“\n*** No Elements in the Linked List ***\n”);
else if(first->rlink==NULL)
{
printf(“\n*** The Item %d is Deleted ***\n”,first->data);
free(first);
first=last=NULL;
}
else
{
p=first->rlink;
first->rlink=NULL;
printf(“\n*** The Item %d is Deleted ***\n”,first->data);
free(first);
first=p;
}
}
void deleteAtEnd()
{
node *cur;
if(first==NULL&&last==NULL)
printf(“\n*** No Elements in the Linked List ***\n”);
else if(first->rlink==NULL)
{
printf(“*** The Item %d is Deleted ***”,first->data);
free(first);
first=last=NULL;
}
else
{
cur=last->llink;
cur->rlink=NULL;
last->llink=NULL;
printf(“\n*** The Item %d is Deleted ***\n”,last->data);
free(last);
last=cur;
}
}
void deleteInBetweenPos()
{
int cnt,pos,i;
node *pre,*cur,*p;
cnt=countElements();
if(cnt>0)
{
printf(“\nEnter the Position U want to Delete : “);
scanf(“%d”,&pos);
if(pos>=1&&pos<=cnt)
{
if(pos==1)
deleteAtBegin();
else
{
cur=first;
for(i=1;i<pos;i++)
{
pre=cur;
cur=cur->rlink;
}
if(cur->rlink==NULL)
deleteAtEnd();
else
{
p=cur->rlink;
pre->rlink=p;
p->llink=pre;
cur->rlink=cur->llink=NULL;
printf(“\n*** The Item %d is Deleted ***\n”,cur->data);
free(cur);
}
}
}
else
printf(“\n*** Invalid Position ***\n”);
}
else
printf(“\n*** No Elements in the Linked List ***\n”);
}
void deleteInBetweenKey()
{
int key,cnt;
node *pre,*cur;
cnt=countElements();
if(cnt>0)
{
printf(“\nEnter the Key U want to Delete : “);
scanf(“%d”,&key);
if(first==NULL&&last==NULL)
printf(“\n*** No Elements in the Linked List ***\n”);
else if(first->rlink==NULL)
{
if(first->data==key)
{
printf(“\n*** The Key %d is Deleted ***\n”,first->data);
free(first);
first=NULL;
last=NULL;
}
else
printf(“\n*** The Given Key is Not Matched ***\n”);
}
else
{
pre=first;
cur=first->rlink;
if(first->data==key)
{
first->rlink=NULL;
printf(“\n*** The Key %d is Deleted ***\n”,first->data);
free(first);
first=cur;
first->llink=NULL;
}
else
{
while(cur!=NULL)
{
if(cur->data==key)
break;
pre=cur;
cur=cur->rlink;
}
if(cur!=NULL)
{
pre->rlink=cur->rlink;
cur->rlink->llink=pre;
cur->rlink=NULL;
cur->llink=NULL;
printf(“\n*** The Key %d is Deleted ***\n”,cur->data);
free(cur);
}
else
printf(“\n*** The Given Key is Not Matched ***\n”);
}
}
}
else
printf(“\n*** No Elements in the Linked List ***\n”);
}
void reverse()
{
node *cur;
if(first!=NULL)
{
cur=last;
printf(“\n******************************”);
printf(“\n\t* Elements are *”);
printf(“\n******************************\nNULL <– “);
while(cur!=NULL)
{
if(cur->llink!=NULL)
printf(“%d <–> “,cur->data);
else
printf(“%d”,cur->data);
cur=cur->llink;
}
printf(” –> NULL\n”);
}
else
printf(“\n*** No Elements in the Linked List ***\n”);
}
void sort()
{
node *pre,*cur;
int temp;
if(first!=NULL&&first->rlink!=NULL)
{
pre=first;
while(pre!=NULL)
{
cur=pre->rlink;
while(cur!=NULL)
{
if(pre->data>cur->data)
{
temp=pre->data;
pre->data=cur->data;
cur->data=temp;
}
cur=cur->rlink;
}
pre=pre->rlink;
}
}
}
void palindrome()
{
node *f,*l;
int cnt;
f=first;
l=last;
cnt=countElements();
if(cnt>0)
{
while(f!=l&&f->llink!=l)
{
if(f->data!=l->data)
break;
f=f->rlink;
l=l->llink;
}
if(f==l||f->llink==l)
printf(“\n*** Palindrome ***\n”);
else
printf(“\n*** Not Palindrome ***\n”);
}
else
printf(“\n*** No Elements in the Linked List ***\n”);
}
LINEAR SEARCH
#include<stdio.h>
#include<conio.h>
int linearSearch(int [],int,int,int);
void main()
{
int a[10],n,key,i,ans,ch;
printf(“\n\t\t\t********************************”);
printf(“\n\t\t\t Recursive Linear Search”);
printf(“\n\t\t\t********************************\n”);
printf(“\nEnter How many Elements U want to Enter : “);
scanf(“%d”,&n);
if(n>0)
{
printf(“\n\nEnter the Elements One By One\n”);
for(i=0;i<n;i++)
scanf(“%d”,&a[i]);
do
{
printf(“\n\nEnter the Key to Find : “);
scanf(“%d”,&key);
ans=linearSearch(a,0,n-1,key);
if(ans==1)
printf(“\n*** The Key Found ***”);
else
printf(“\n*** The Key Not Found ***”);
printf(“\n\nDo u want to Search any other Key Enter (1-Continue or 0-Exit) : “);
scanf(“%d”,&ch);
}while(ch==1);
}
printf(“\n\n\t\t\t*** Program Ends ***\n”);
getch();
}
int linearSearch(int a[],int low,int high,int key)
{
if(low>high)
return 0;
if(key==a[low])
return 1;
return linearSearch(a,low+1,high,key);
}
BINARY SEARCH
#include<stdio.h>
#include<conio.h>
int binarySearch(int [],int,int,int);
void main()
{
int a[10],n,key,i,j,ans,ch,temp;
printf(“\n\t\t\t********************************”);
printf(“\n\t\t\t Recursive Binary Search”);
printf(“\n\t\t\t********************************\n”);
printf(“\nEnter How many Elements U want to Enter : “);
scanf(“%d”,&n);
if(n>0)
{
printf(“\n\nEnter the Elements One By One\n”);
for(i=0;i<n;i++)
scanf(“%d”,&a[i]);
//Sorting
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;
}
}
}
do
{
printf(“\n\nEnter the Key to Find : “);
scanf(“%d”,&key);
ans=binarySearch(a,0,n-1,key);
if(ans==1)
printf(“\n*** The Key Found ***”);
else
printf(“\n*** The Key Not Found ***”);
printf(“\n\nDo u want to Search any other Key Enter (1-Continue or 0-Exit) : “);
scanf(“%d”,&ch);
}while(ch==1);
}
printf(“\n\n\t\t\t*** Program Ends ***\n”);
getch();
}
int binarySearch(int a[],int low,int high,int key)
{
int mid;
mid=(low+high)/2;
if(low>high)
return 0;
if(key==a[mid])
return 1;
else if(key<a[mid])
high=mid-1;
else
low=mid+1;
return binarySearch(a,low,high,key);
}
SELECTION SORT
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],n,temp,i,j,k,ch,small,pos;
do
{
printf(“\n\t\t******************************”);
printf(“\n\t\t * Selection Sort * “);
printf(“\n\t\t******************************\n”);
printf(“\nEnter How Many Elements U want to Enter : “);
scanf(“%d”,&n);
printf(“\nEnter the Elements One by One \n\n”);
for(i=0;i<n;i++)
scanf(“%d”,&a[i]);
for(i=0;i<n-1;i++)
{
small=a[i];
for(j=i+1;j<n;j++)
{
if(small>a[j])
{
small=a[j];
pos=j;
}
}
temp=a[i];
a[i]=small;
a[pos]=temp;
printf(“\nIn %d Step : “,i+1);
for(k=0;k<n;k++)
printf(“%d\t”,a[k]);
}
printf(“\n\n**********************”);
printf(“\n Sorting Elements Are “);
printf(“\n**********************\n\n”);
for(i=0;i<n;i++)
printf(“%d\t”,a[i]);
printf(“\n”);
do
{
printf(“\nDo U wish to Continue Enter (1 –> Continue or 0 –> Exit) : “);
scanf(“%d”,&ch);
switch(ch)
{
case 0:
printf(“\n\t\t\t*** Program Ends ***\n”);
break;
case 1:
break;
default:
printf(“\n*** Enter the Valid Choice ***\n”);
}
}while(ch!=0&&ch!=1);
}while(ch==1);
getch();
}
BUBBLE SORT
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],n,temp,i,j,k,ch;
do
{
printf(“\n\t\t*****************************”);
printf(“\n\t\t * Bubble Sort * “);
printf(“\n\t\t*****************************\n”);
printf(“\nEnter How Many Elements U want to Enter : “);
scanf(“%d”,&n);
printf(“\nEnter the Elements One by One \n\n”);
for(i=0;i<n;i++)
scanf(“%d”,&a[i]);
for(i=n-1;i>=0;i–)
{
for(j=0;j<i;j++)
{
if(a[j]>a[j+1])
{
temp=a[j+1];
a[j+1]=a[j];
a[j]=temp;
}
}
printf(“\nIn %d Step : “,n-i);
for(k=0;k<n;k++)
printf(“%d\t”,a[k]);
}
printf(“\n\n**********************”);
printf(“\n Sorting Elements Are “);
printf(“\n**********************\n\n”);
for(i=0;i<n;i++)
printf(“%d\t”,a[i]);
printf(“\n”);
do
{
printf(“\nDo U wish to Continue Enter (1 –> Continue or 0 –> Exit) : “);
scanf(“%d”,&ch);
switch(ch)
{
case 0:
printf(“\n\t\t\t*** Program Ends ***\n”);
break;
case 1:
break;
default:
printf(“\n*** Enter the Valid Choice ***\n”);
}
}while(ch!=0&&ch!=1);
}while(ch==1);
getch();
}
INSERTION SORT
#include<stdio.h>
#include<conio.h>
void insertionSort(int [],int);
void main()
{
int a[10],n,i,ch;
do
{
printf(“\n\t\t\t****************************”);
printf(“\n\t\t\t Insertion Sort”);
printf(“\n\t\t\t****************************\n”);
printf(“\nEnter Howmany Elements U want to Enter : “);
scanf(“%d”,&n);
printf(“\nEnter the Elements One By One :\n\n”);
for(i=0;i<n;i++)
scanf(“%d”,&a[i]);
insertionSort(a,n);
printf(“\n\n****************************”);
printf(“\n Sorted Elements Are”);
printf(“\n****************************\n\n”);
for(i=0;i<n;i++)
printf(“%d\t”,a[i]);
printf(“\n\n”);
do
{
printf(“\nDo U Wish to Continue Enter (1 –>Continue or 0 –>Exit : “);
scanf(“%d”,&ch);
switch(ch)
{
case 0:
printf(“\n\n\t\t\t*** Program Ends ***\n\n”);
break;
case 1:
break;
default:
printf(“\n*** Enter the Valid Choice ***\n”);
}
}while(ch!=0&&ch!=1);
}while(ch==1);
getch();
}
void insertionSort(int a[],int n)
{
int i,j,k,temp;
for(i=1;i<n;i++)
{
temp=a[i];
for(j=i-1;j>=0;j–)
{
if(a[j]>temp)
a[j+1]=a[j];
else
break;
}
a[j+1]=temp;
printf(“\nIn %d Step : “,i);
for(k=0;k<n;k++)
printf(“%d\t”,a[k]);
}
}
QUICK SORT
#include<stdio.h>
#include<conio.h>
void quickSort(int *, int, int);
int split(int *, int, int);
void Swap(int *, int *);
int arr[10];
void main()
{
int i, n, ch;
do
{
printf(“\n\t\t\t***********************”);
printf(“\n\t\t\t Quick Sort”);
printf(“\n\t\t\t***********************\n”);
printf(“\nEnter No of Elements want to Enter : “);
scanf(“%d”, &n);
if(n>0)
{
printf(“\nEnter the Elements One by One:\n\n”);
for(i = 0; i < n; i++)
scanf(“%d”, &arr[i]);
printf(“\n***********************”);
printf(“\n Before Sorting”);
printf(“\n***********************\n\n”);
for (i = 0; i < n; i++)
printf(“%d\t”, arr[i]);
quickSort(arr, 0, n-1);
printf(“\n\n***********************”);
printf(“\n After Sorting”);
printf(“\n***********************\n\n”);
for(i = 0; i < n; i++)
printf(“%d\t”, arr[i]);
}
do
{
printf(“\n\nDo U wish to Continue Enter (1-Continue or 0-Exit) : “);
scanf(“%d”,&ch);
switch(ch)
{
case 0:
printf(“\n\n\n\t\t\t*** Program Ends ***\n”);
break;
case 1:
break;
default:
printf(“\n*** Enter Valid Choice ***\n”);
}
}while(ch!=0&&ch!=1);
}while(ch==1);
getch();
}
void quickSort(int a[], int lower, int upper)
{
int i;
if(upper > lower)
{
i = split(a, lower,upper);
quickSort(a, lower, i -1);
quickSort(a, i + 1, upper);
}
}
int split(int a[], int lower, int upper)
{
int i, p, q;
p = lower + 1;
q = upper;
i = a[lower];
while (q >= p)
{
while(a[p] < i)
p++;
while(a[q] > i)
q–;
if(q > p)
Swap(&a[p], &a[q]);
}
Swap(&a[lower], &a[q]);
return q;
}
void Swap(int *p,int *q)
{
int temp;
temp = *p;
*p = *q;
*q = temp;
}