↓↑ Program to implement binary Search.
#include<stdio.h>
int main()
{
int low,high,mid,i,a[15],key,n;
printf("Enter No in array in sorted order:");
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
low=0;
high=n-1;
printf("\nenter key to be searched:");
scanf("%d",&key);
for(i=0;i<n;i++)
{
mid=(low+high)/2;
if(a[mid]==key)
{
printf("element fount at %d position\n",mid+1);
break;
}
if(a[mid]<key)
low=mid+1;
if(a[mid]>key)
high=mid-1;
}
return 0;
}
↓↑ Program to implement bubble sort.
#include<stdio.h>
#define MAX 15
int main()
{
int a[MAX],i,temp,j,n;
printf("enter the no of element:");
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
{
for(j=0;j<n-1-i;j++)
{
if(a[j]>a[j+1])
{
temp=a[j+1];
a[j+1]=a[j];
a[j]=temp;
}
}
}
printf("sorted array is:");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
printf("\n");
return 0;
}
↓↑ Program to implement circular queue.
#include<stdio.h>
#define MAXSIZE 5
int cq[10];
int front=-1,rear=0;
int choice;
char ch;
void main()
{
do
{
printf("1.Insert-------\n");
printf("2. Delete------\n");
printf("3. Display-----\n");
printf("4.exit---------\n");
printf("Enter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1 : cqinsert();
break;
case 2 : cqdelete();
break;
case 3 : cqdisplay();
break;
case 4: return;
}
fflush(stdin);
}while(choice!=4);
}
cqinsert()
{
int num;
if(front==(rear+1)%MAXSIZE)
{
printf("Queue is full\n");
return;
}
else
{
printf("Enter the element to be inserted\n");
scanf("%d",&num);
if(front==-1)
front=rear=0;
else
rear=(rear+1) % MAXSIZE;
cq[rear]= num;
}
return;
}
int cqdelete()
{
int num;
if(front==-1)
{
printf("Queue is Empty\n");
return;
}
else
{
num=cq[front];
printf("Deleted element is =%d\n",cq[front]);
if(front==rear)
front=rear=-1;
else
front=(front+1)%MAXSIZE;
}
return(num);
}
cqdisplay()
{
int i;
if(front==-1)
{
printf("Queue is empty\n");
return;
}
else
{
printf("\nThe status of the queue\n");
for(i=front;i<=rear;i++)
{
printf("%d\n",cq[i]);
}
}
if(front>rear)
{
for(i=front;i<MAXSIZE;i++)
{
printf("%d\n",cq[i]);
}
for(i=0;i<=rear;i++)
{
printf("%d\n",cq[i]);
}
}
printf("\n");
}
↓↑ Program to implement infix to postfix expression.
#include<stdio.h>
#include<string.h>
char stack[50];
int top=-1;
void in_to_post(char infix[]);
void push (char);
char pop();
int main()
{
char infx[25];
printf("Enter the infix expression");
gets(infx);
in_to_post(infx);
return 0;
}
void push (char symb)
{
if (top>=49)
{
printf("stack overflow");
return;
}
else
{
top=top+1;
stack[top]=symb;
}
}
char pop()
{
char item;
if(top==-1)
{
printf("stack empty");
return(0);
}
else
{
item=stack[top];
top--;
}
return(item);
}
int preced(char ch)
{
if(ch==47)
{
return(5);
}
else
if(ch==42)
{
return(4);
}
else if(ch==43)
{
return(3);
}
else
return(2);
}
void in_to_post(char infix[])
{
int length;
static int index=0,pos=0;
char symbol,temp;
char postfix[40];
length=strlen(infix);
push('#');
while(index<length)
{
symbol=infix[index];
switch(symbol)
{
case'(':push(symbol);
break;
case')' :temp=pop();
while(temp!='(')
{
postfix[pos]=temp;
pos++;
temp=pop();
}
break;
case '+' :
case '-' :
case '*' :
case '/' :
case '^' :
while (preced(stack[top])>=preced(symbol))
{
temp=pop();
postfix[pos]=temp;
pos++;
}
push(symbol);
break;
default : postfix[pos++]=symbol;
break;
}
index++;
}
while(top>0)
{
temp=pop();
postfix[pos++]=temp;
}
postfix[pos++]='\0';
puts(postfix);
return;
}
↓↑ Program to implement insertion sort.
#include<stdio.h>
int main()
{
int a[15],i,k,key,n;
printf("enter no of element in array n=:");
scanf("%d",&n);
printf("enter array element:");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(k=1;k<n;k++)
{
key=a[k];
for(i=k-1;key<a[i]&&i>=0;i--)
{
a[i+1]=a[i];
}
a[i+1]=key;
}
printf("sorted array is::");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
printf("\n");
return 0;
}
↓↑ Program to implement linear search.
#include<stdio.h>
#define MAX 15
int main()
{
int a[MAX],i,n,item,flag=0,pos;
printf("enter how many element you want to enter:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("\nenter item to be searched:");
scanf("%d",&item);
for(i=0;i<n;i++)
{
if(item==a[i])
{
flag=1;
pos=i;
break;
}
}
if(flag==1)
printf("item found at %d position\n",pos);
else
printf("item not found\n");
return 0;
}
↓↑ Program to implement merge sort.
#include<stdio.h>
#define MAX 50
void mergeSort(int arr[],int low,int mid,int high);
void partition(int arr[],int low,int high);
int main(){
int merge[MAX],i,n;
printf("Enter the total number of elements: ");
scanf("%d",&n);
printf("Enter the elements which to be sort: ");
for(i=0;i<n;i++){
scanf("%d",&merge[i]);
}
partition(merge,0,n-1);
printf("After merge sorting elements are: ");
for(i=0;i<n;i++){
printf("%d ",merge[i]);
}
return 0;
}
void partition(int arr[],int low,int high){
int mid;
if(low<high){
mid=(low+high)/2;
partition(arr,low,mid);
partition(arr,mid+1,high);
mergeSort(arr,low,mid,high);
}
}
void mergeSort(int arr[],int low,int mid,int high){
int i,m,k,l,temp[MAX];
l=low;
i=low;
m=mid+1;
while((l<=mid)&&(m<=high)){
if(arr[l]<=arr[m]){
temp[i]=arr[l];
l++;
}
else{
temp[i]=arr[m];
m++;
}
i++;
}
if(l>mid){
for(k=m;k<=high;k++){
temp[i]=arr[k];
i++;
}
}
else{
for(k=l;k<=mid;k++){
temp[i]=arr[k];
i++;
}
}
for(k=low;k<=high;k++){
arr[k]=temp[k];
}
}
↓↑ Program to evaluate postfix expression.
#include<stdio.h>
#include<math.h>
float stack[10];
int top=-1;
void push(char);
float pop();
float eval(char [],float[]);
int main()
{
int i=0;
char suffix[20];
float value[20],result;
printf("Enter a valid postfix expression:");
gets(suffix);
while (suffix[i]!='\0')
{
if(isalpha(suffix[i]))
{
fflush(stdin);
printf("\nEnter the value of %c",suffix[i]);
scanf("%f",&value[i]);
}
i++;
}
result=eval(suffix,value);
printf("The result of %s=%f",suffix,result);
return 0;
}
float eval(char suffix[],float data[])
{
int i=0;
float op1,op2,res;
char ch;
while(suffix[i]!='\0')
{
ch=suffix[i];
if(isalpha(suffix[i]))
{
push(data[i]);
}
else
{
op2=pop();
op1=pop();
switch(ch)
{
case '+' : push(op1+op2);
break;
case '-' : push(op1-op2);
break;
case '*' : push(op1*op2);
break;
case '/' :push(op1/op2);
break;
case '^' : push(pow(op1,op2));
break;
}
}
i++;
}
res=pop();
return(res);
}
void push(char num)
{
top=top+1;
stack[top]=num;
}
float pop()
{
float num;
num=stack[top];
top=top-1;
return(num);
}
↓↑ Program to implement queue.
#include<stdio.h>
#define MAXSIZE 5
int front=-1, rear=-1,choice;
int q[10];
void qinsert();
void qdelete();
void qdisplay();
int main()
{
do
{
printf("\n1-->insert\n");
printf("2-->delete\n");
printf("3-->display\n");
printf("4-->exit\n");
printf("enter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:qinsert();
break;
case 2:qdelete();
break;
case 3:qdisplay();
break;
case 4:break;
}
}while(choice!=4);
return 0;
}
void qinsert()
{
int num;
if(rear==(MAXSIZE-1))
{
printf("queue is full\n");
return;
}
else
{
printf("enter no\n");
scanf("%d",&num);
rear=rear+1;
q[rear]=num;
if(front==-1)
{
front++;
}
}
return;
}
void qdelete()
{
int num;
if(front==-1)
{
printf("queue empty\n");
return;
}
else
{
if(front==rear)
front=rear=-1;
else
{
num=q[front];
printf("deleted item=%d",q[front]);
front++;
}
}
//return(num);
}
void qdisplay()
{
int i;
if(front==-1)
{
printf("queue empty\n");
return;
}
else
{
printf("\nThe status of the queu\n");
for(i=front;i<=rear;i++)
{
printf("%d\t",q[i]);
}
}
printf("\n");
}
↓↑ Program to implement Quicksort.
#include<stdio.h>
void quick(int a[],int,int);
int partition(int a[],int,int);
int main()
{
int a[15],i,lb,hb,n;
printf("enter no of element in array :");
scanf("%d",&n);
printf("enter element:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
lb=0;
hb=n-1;
quick(a,lb,hb);
printf("sorted array after quick sort:");
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
return 0;
}
void quick(int a[],int lb,int ub)
{
int pos;
if(lb>=ub)return;
pos=partition(a,lb,ub);
quick(a,lb,pos-1);
quick(a,pos+1,ub);
}
int partition(int a[],int l,int h)
{
int pivot,down,up,pos,temp;
pivot=a[l];
down=l;
up=h;
do
{
while(pivot>=a[down])
down++;
while(pivot<a[up])
up--;
if(down<up)
{
temp=a[down];
a[down]=a[up];
a[up]=temp;
}
}while(down<up);
pos=up;
a[l]=a[pos];
a[pos]=pivot;
return pos;
}
↓↑ Program to implement selection sort.
#include<stdio.h>
int main()
{
int a[15],i,j,min,n,minpos,temp;
printf("enter no of element:");
scanf("%d",&n);
printf("\nenter array element:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
min=a[0];
for(i=0;i<n;i++)
{
min=a[i];
minpos=i;
for(j=i+1;j<n;j++)
{
if(a[j]<min)
{
min=a[j];
minpos=j;
}
if(minpos!=i)
{
temp=a[i];
a[i]=a[minpos];
a[minpos]=temp;
}
}
}
printf("\nsorted array is:");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
printf("\n");
return 0;
}
↓↑ Proram to implement stack.
#include<stdio.h>
#include<stdlib.h>
#define MAX 10
struct student;
void push(struct student *,int);
int pop(struct student *);
void display(struct student *);
struct student
{
int arr[MAX];
int top;
};
typedef struct student *ps;
struct student s1;
int main()
{
int n,choice;
char ch;
s1.top=-1;
do{
printf("\n1.push\n2.pop\n3.display\n0.exit\nenter choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("enter value :");
scanf("%d",&n);
push(&s1,n);
break;
case 2:
n=pop(&s1);
if(n==-1)
{
printf("stack is empty\n");
}
else
printf("popped value is:%d \n",n);
break;
case 3:
display(&s1);
break;
case 0:
exit(0);
default:
printf("invalid choice!!!!\n");
}
}while(1);
return 0;
}
void push(ps p,int n)
{
if(s1.top==MAX-1)
printf("stack overflow\n");
else
p->arr[++p->top]=n;
}
int pop(ps p)
{
if(p->top==-1)
{
printf("stack underflow!!!\n");
return;
}
return (p->arr[(p->top)--]);
}
void display(ps p)
{
int i;
for(i=p->top;i>=0;i--)
{
printf("%d ",p->arr[i]);
}
printf("\n");
}
↓↑ Program to implement tower of hanoi.
#include<stdio.h>
void move(int,char,char,char);
int k=0;
int main()
{
int n;
printf("enter no of disc's:");
scanf("%d",&n);
move(n,'A','C','B');
printf("total moves =%d\n",k);
return 0;
}
void move(int n,char src,char dest,char mid)
{
if(n==0)
return;
move(n-1,src,mid,dest);
printf("move %d from %c\n",n,src,dest);
k++;
move(n-1,mid,dest,src);
}
↓↑ Program to implement Binary search tree,inorder,preorder,postorder traversal.
#include<stdio.h>
#include<malloc.h>
struct bt
{
struct bt *left;
int data;
struct bt *right;
};
typedef struct bt snode;
typedef struct bt* psnode;
void insert(int);
void inorder(psnode);
void preorder(psnode);
void postorder(psnode);
int search(int);
psnode root=NULL;
int main()
{
int ch,n;
do
{
printf("1.insert.\n2.inorder\n3.preorder\n4.postorder\n5.search element.\n6.exit.\nenter chioce:");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("\nenter the element to insert:");
scanf("%d",&n);
insert(n);
break;
case 2:inorder(root); printf("\n"); break;
case 3:preorder(root);printf("\n"); break;
case 4:postorder(root);printf("\n"); break;
case 5:printf("enter element to search:");
scanf("%d",&n);
search(n);
break;
case 6:break;
default: printf("wrong choice!!!!!\n");
}
}while(ch!=6);
return 0;
}
void insert(int n)
{
psnode ptr1,ptr,prev;
ptr=(psnode)malloc(sizeof(struct bt));
ptr->data=n;
ptr->left=ptr->right=NULL;
if(root==NULL)
{
root=ptr;return;
}
else
{
ptr1=root;
prev=NULL;
while(ptr1)
{
prev=ptr1;
if(n< ptr1->data)
{
ptr1=ptr1->left;
}
else if(n > ptr1->data)
{
ptr1=ptr1->right;
}
}
if(n>prev->data) prev->right=ptr;
if(n<prev->data) prev->left=ptr;
}
}
void inorder(psnode p)
{
if(p==NULL) return;
inorder(p->left);
printf("%d ",p->data);
inorder(p->right);
}
void preorder(psnode p)
{
if(p==NULL)return;
printf("%d ",p->data);
preorder(p->left);
preorder(p->right);
}
void postorder(psnode p)
{
if(p==NULL)return;
postorder(p->left);
postorder(p->right);
printf("%d ",p->data);
}
int search(int key)
{
psnode ptr=root;
while(ptr)
{
if(key==ptr->data)
{
printf("element found.\n"); return 1;
}
else if(key<ptr->data)
ptr=ptr->left;
else
ptr=ptr->right;
}
printf("\nnot found\n");
return 0;
}
↓↑ Program for Dijkstra's single source shortest path algorithm.
#include <stdio.h>
#include <limits.h>
#include <stdbool.h>
// Number of vertices in the graph
#define V 9
// A utility function to find the vertex with minimum distance value, from
// the set of vertices not yet included in shortest path tree
int minDistance(int dist[], bool sptSet[])
{
int v;
// Initialize min value
int min = INT_MAX, min_index;
for (v = 0; v < V; v++)
if (sptSet[v] == false && dist[v] <= min)
min = dist[v], min_index = v;
return min_index;
}
// A utility function to print the constructed distance array
int printSolution(int dist[], int n)
{
int i;
printf("Vertex Distance from Source\n");
for (i = 0; i < V; i++)
printf("%d \t\t %d\n", i, dist[i]);
}
// Funtion that implements Dijkstra's single source shortest path algorithm
// for a graph represented using adjacency matrix representation
void dijkstra(int graph[V][V], int src)
{
int i,count,v;
int dist[V]; // The output array. dist[i] will hold the shortest
// distance from src to i
bool sptSet[V]; // sptSet[i] will true if vertex i is included in shortest
// path tree or shortest distance from src to i is finalized
// Initialize all distances as INFINITE and stpSet[] as false
for (i = 0; i < V; i++)
dist[i] = INT_MAX, sptSet[i] = false;
// Distance of source vertex from itself is always 0
dist[src] = 0;
// Find shortest path for all vertices
for (count = 0; count < V-1; count++)
{
// Pick the minimum distance vertex from the set of vertices not
// yet processed. u is always equal to src in first iteration.
int u = minDistance(dist, sptSet);
// Mark the picked vertex as processed
sptSet[u] = true;
// Update dist value of the adjacent vertices of the picked vertex.
for (v = 0; v < V; v++)
// Update dist[v] only if is not in sptSet, there is an edge from
// u to v, and total weight of path from src to v through u is
// smaller than current value of dist[v]
if (!sptSet[v] && graph[u][v] && dist[u] != INT_MAX
&& dist[u]+graph[u][v] < dist[v])
dist[v] = dist[u] + graph[u][v];
}
// print the constructed distance array
printSolution(dist, V);
}
// driver program to test above function
int main()
{
int graph[V][V] = {{0, 4, 0, 0, 0, 0, 0, 8, 0},
{4, 0, 8, 0, 0, 0, 0, 11, 0},
{0, 8, 0, 7, 0, 4, 0, 0, 2},
{0, 0, 7, 0, 9, 14, 0, 0, 0},
{0, 0, 0, 9, 0, 10, 0, 0, 0},
{0, 0, 4, 0, 10, 0, 2, 0, 0},
{0, 0, 0, 14, 0, 2, 0, 1, 6},
{8, 11, 0, 0, 0, 0, 1, 0, 7},
{0, 0, 2, 0, 0, 0, 6, 7, 0}
};
dijkstra(graph, 0);
return 0;
}
↓↑ Program to implement Heap sort.
#include<stdio.h>
int a[15],n;
void del_root(int);
void heap_sort();
void insert(int,int);
void create_heap();
int main()
{
int i;
printf("Enter the No. of Elements\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the %d Element\n",i+1);
scanf("%d",&a[i]);
}
printf("\nEntered list is:");
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
create_heap();
printf("\nHeap is:");
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
heap_sort();
printf("\nSorted list is:");
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
printf("\n");
return 0;
}
void del_root(int last)
{
int left,right,i,temp;
i=0;
temp=a[i];
a[i]=a[last];
a[last]=temp;
left=2*i+1;
right=2*i+2;
while(right<last)
{
if((a[i]>=a[left])&&(a[i]>=a[right]))
return;
if(a[right]<=a[left])
{
temp=a[i];
a[i]=a[left];
a[left]=temp;
i=left;
}
else
{
temp=a[i];
a[i]=a[right];
a[right]=temp;
i=right;
}
left=2*i+1;
right=2*i+1;
}
if((left==last-1)&&a[i]<a[left])
{
temp=a[i];
a[i]=a[left];
a[left]=temp;
}
}
void heap_sort()
{
int i=n-1;
for(;i>0;i--)
{
del_root(i);
}
}
void insert(int num,int loc)
{
int parent;
while(loc>0)
{
parent=(loc-1)/2;
if(num<=a[parent])
{
a[loc]=num;
return;
}
a[loc]=a[parent];
loc=parent;
}
a[0]=num;
}
void create_heap()
{
int i;
for(i=0;i<n;i++)
{
insert(a[i],i);
}
}
↓↑ link list which makes two other list one with no's divisible by 3 other with rest
#include<stdio.h>
#include<malloc.h>
struct node{
int data;
struct node *next;
};
struct node *start,*start1,*start2;
void display();
void create(int);
void displaydiv3();
void displaynotdiv3();
int main()
{
int ch,n2,n3,i;
start=start1=start2=NULL;
do
{
printf("\n0.create list\n1.list of no divisible by 3.\n2.list of no not divisible by 3.\n3.display.\n4.exit\nenter choice:");
scanf("%d",&ch);
switch(ch)
{
case 0:printf("how many node you want to create:");
scanf("%d",&n2);
for(i=0;i<n2;i++)
{
printf("\nenter element:");
scanf("%d",&n3);
create(n3);
}
break;
case 1:displaydiv3();
break;
case 2:displaynotdiv3();
break;
case 3:display();
break;
case 4: break;
default:
printf("invalid choice\n");
}
}while(ch!=4);
return 0;
}
void create(int m)
{
struct node *p,*q;
p=(struct node*)malloc(sizeof(struct node)); //complete list.
p->data=m;
p->next=NULL;
if(start==NULL){ start=p;}
else { q=start; while(q->next!=NULL){ q=q->next; }q->next=p; }
if(m%3==0)
{
struct node *p1,*q1;
p1=(struct node*)malloc(sizeof(struct node)); // list with no divisible by 3.
p1->data=m;
p1->next=NULL;
if(start1==NULL){ start1=p1;}
else { q1=start1; while(q1->next!=NULL){ q1=q1->next; }q1->next=p1;}
}
else
{
struct node *p2,*q2;
p2=(struct node*)malloc(sizeof(struct node)); // list with other no which are not divisible by 3.
p2->data=m;
p2->next=NULL;
if(start2==NULL){ start2=p2;}
else { q2=start2; while(q2->next!=NULL){ q2=q2->next; }q2->next=p2;}
}
}
void display()
{
struct node *p;
p=start;
if(p==NULL){ printf("\nthere is no element in list\n"); return;}
while(p!=NULL)
{
printf("%d\t",p->data);
p=p->next;
}
}
void displaynotdiv3()
{
struct node *p;
p=start2;
if(p==NULL){ printf("\nthere is no element in list\n"); return;}
while(p!=NULL)
{
printf("%d\t",p->data);
p=p->next;
}
}
void displaydiv3()
{
struct node *p;
p=start1;
if(p==NULL){ printf("\nthere is no element in list\n"); return;}
while(p!=NULL)
{
printf("%d\t",p->data);
p=p->next;
}
}