Uploaded by PREMA SHANKAR

Binary search

advertisement
Binary search
#include<stdio.h>
#include<conio.h>
void main()
{
int a[50],i,temp,n,key,high,low,mid;
clrscr();
printf("Enter Size of Array \n");
scanf("%d",&n);
printf("Enter %d elements ",n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("Enter Array is: ");
for(i=0;i<n;i++)
{
printf("%d ",a[i]);
}
printf("Enter value to find n");
scanf("%d",&key);
low=0;
high=n-1;
mid=(low+high)/2;
while(low<=high)
{
if(a[mid]<key)
{
low=mid+1;
}
else if(a[mid]==key)
{
printf("%d Element found at %d location",key,mid+1);
break;
}
else
high=mid-1;
mid=(low+high)/2;
}
if(low>high)
printf("Element not Found! %d is not present in array",key);
getch();
}
Quick sort
#include<stdio.h>
void qs(int A[25],int f,int l,int ll){
int i, j, p, temp, k;
if(f<l){
p=f;
i=f;
j=l;
while(i<j){
while(A[i]<=A[p] && i<l)
i++;
while(A[j]>A[p])
j--;
if(i<j){
temp=A[i];
A[i]=A[j];
A[j]=temp;
}
}
temp=A[p];
A[p]=A[j];
A[j]=temp;
qs(A,f,j-1,ll);
qs(A,j+1,l,ll);
}
}
int main(){
int i, len, A[25];
printf("Enter length ");
scanf("%d",&len);
printf("Enter %d nos ", len);
for(i=0;i<len;i++)
scanf("%d",&A[i]);
qs(A,0,len-1,len);
printf("Sorted ");
for(i=0;i<len;i++)
printf(" %d",A[i]);
return 0;}
stack and queue using array
#include<stdio.h>
#include<conio.h>
#define Max 5
int st[Max];
int top=-1;
void push();
void pop();
void display();
void main()
{
int ch;
clrscr();
printf("1. Push\n 2.Pop\n 3.Display\n 4.End Program\n");
do
{
printf("Enter your Choice \n");
scanf("%d",&ch);
switch(ch)
{
case 1: push();
break;
case 2: pop();
break;
case 3: display();
break;
case 4: printf("Program Ends\n");
break;
}
}
while(ch!=4);
printf("Wrong Choice");
getch();
}
void push()
{
if(top==Max-1)
{
printf("Stack Overflow");
}
else
{
top++;
printf("Enter Elements \n");
scanf("%d",&st[top]);
}
}
void pop()
{
if(top==-1)
{
printf("Stack Empty");
}
else
{
top--;
}
}
void display()
{
int i;
if(top==-1)
{
printf("Stack Empty");
}
else
{
for(i=top;i>0;i--)
{
printf("%d\n",st[i]);
}
}
}
Queue:
#include<stdio.h>
#define Max 50
void enqueue();
void dequeue();
void display();
int queue_array[Max],rear=-1,front=-1;
int main()
{
int ch;
while(1)
{
printf("1.Enqueue\n 2.Dequeue\n 3. Display\n 4.Exit\n");
scanf("%d",&ch);
switch(ch)
{
case 1: enqueue();
break;
case 2: dequeue();
break;
case 3: display();
break;
case 4: exit(1);
default: printf("Wrong choice \n");
}
}
}
void enqueue()
{
int add_item;
if(rear==Max-1)
printf("Queue Overflow \n");
else
{
if(front==-1)
front=0;
printf("Insert Element to be added in Queue: ");
scanf("%d",&add_item);
rear=rear+1;
queue_array[rear]=add_item;
}
}
void dequeue()
{
if(front==-1||front>rear)
{
printf("Queue Underflow \n");
return ;
}
else
{
printf("Element deleted from queue is : %d \n",queue_array[front]);
front=front+1;
}
}
void display()
{
int i;
if(front==-1)
printf("Queue is Empty \n");
else
{
printf("Queue is: \n");
for(i=front;i<=rear;i++)
printf("%d",queue_array[i]);
printf("\n");
}
}
Bst
#include<stdio.h>
#include<stdlib.h>
struct node
{
int key;
struct node *left,*right;
};
struct node *getNewNode(int val)
{
struct node *NewNode=(struct node*)malloc(sizeof(struct node));
NewNode->key=val;
NewNode->left=NULL;
NewNode->right=NULL;
}
struct node *insert(struct node *root,int val)
{
if(root==NULL)
return getNewNode(val);
if(root->key<val)
root->right=insert(root->right,val);
else if(root->key>val)
root->left=insert(root->left,val);
return root;
}
void inorder(struct node*root)
{
if(root==NULL)
return ;
else
inorder(root->left);
printf("%d \t",root->key);
inorder(root->right);
}
void preorder(struct node*root)
{
if(root==NULL)
return ;
else
printf("%d \t",root->key);
preorder(root->left);
preorder(root->right);
}
void postorder(struct node*root)
{
if(root==NULL)
return ;
else
postorder(root->left);
postorder(root->right);
printf("%d \t",root->key);
}
int main(){
int c,n;
printf("Ënter data of root node");
scanf("%d",&n);
struct node*root=getNewNode(n);
while(c!=5){
{
printf("\n 1.Insertion \n 2.Inorder \n 3.Preorder \n 4.Postorder \n 5.Exit");
printf("\n Enter Choice");
scanf("%d",&c);
switch(c){
case 1: {
printf("Enter data:");
scanf("%d",&n);
insert(root,n);
break;
}
case 2:inorder(root);
break;
case 3:preorder(root);
break;
case 4:postorder(root);
break;
case 5:
break;
default:
printf("Wrong choice");
break;
}
}
}}
Toi:
#include <stdio.h>
void towerOfHanoi(int n, char from_rod, char to_rod, char aux_rod)
{
if (n == 1)
{
printf("\n Move disk 1 from rod %c to rod %c", from_rod, to_rod);
return;
}
towerOfHanoi(n-1, from_rod, aux_rod, to_rod);
printf("\n Move disk %d from rod %c to rod %c", n, from_rod,
to_rod);
towerOfHanoi(n-1, aux_rod, to_rod, from_rod);
}
int main()
{
int n = 4;
towerOfHanoi(n, 'A', 'C', 'B');
return 0;
}
Linked list:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *ptr=NULL;
int count=0;
void inserta(int val)
{
struct node *new;
new=(struct node*)malloc(sizeof(struct node));
new->data=val;
count++;
if(ptr==NULL)
{
ptr=new;
ptr->next=NULL;
}
else
{
new->next=ptr;
ptr=new;
}
}
void insertb(int val)
{
struct node *new;
struct node *temp;
new=(struct node*)malloc(sizeof(struct node));
new->data=val;
count++;
if(ptr==NULL)
{
ptr=new;
ptr->next=NULL;
}
else
{
temp=ptr;
while(temp->next!=NULL)
temp = temp->next;
temp->next=new;
new->next=NULL;
}
}
void traverse()
{
struct node *t;
t=ptr;
if (t==NULL)
{
printf("Linked list is empty.\n");
return;
}
printf("There are %d elements in linked list.\n", count);
while(t->next!=NULL)
{
printf("%d\t",t->data);
t = t->next;
}
printf("%d\n",t->data);
}
void deletea()
{
if(ptr==NULL)
printf("The list is empty \n");
else
{
printf("%d is deleted \n",ptr->data);
ptr=ptr->next;
count--;
}
}
void deleteb()
{
int a=0;
struct node *temp;
temp=ptr;
if(ptr==NULL)
printf("The list is empty \n");
else
{
while(a+2!=count)
{
a++;
temp=temp->next;
}
printf("%d is deleted \n",temp->next->data);
temp->next=NULL;
count--;
}
}
int main ()
{
int i, data;
for (;;) {
printf("1. Insert an element at the beginning of linked list.\n");
printf("2. Insert an element at the end of linked list.\n");
printf("3. Traverse linked list.\n");
printf("4. Delete an element from beginning.\n");
printf("5. Delete an element from end.\n");
printf("6. Exit\n");
scanf("%d", &i);
if (i == 1)
{
printf("Enter value of element\n");
scanf("%d", &data);
inserta(data);
}
else if (i == 2)
{
printf("Enter value of element\n");
scanf("%d", &data);
insertb(data);
}
else if (i == 3)
traverse();
else if (i == 4)
deletea();
else if (i == 5)
deleteb();
else if (i == 6)
{
printf("Exiting the program \n");
break;
}
else
printf("Please enter valid input.\n");
}
return 0;
}
Hashing:
#include <stdio.h>
#include<stdlib.h>
#define TABLE_SIZE 10
int h[TABLE_SIZE]={NULL};
void insert()
{
int key,index,i,flag=0,hkey;
printf("\nenter a value to insert into hash table\n");
scanf("%d",&key);
hkey=key%TABLE_SIZE;
for(i=0;i<TABLE_SIZE;i++)
{
index=(hkey+i)%TABLE_SIZE;
if(h[index] == NULL)
{
h[index]=key;
break;
}
}
if(i == TABLE_SIZE)
printf("\nelement cannot be inserted\n");
}
void search()
{
int key,index,i,flag=0,hkey;
printf("\nenter search element\n");
scanf("%d",&key);
hkey=key%TABLE_SIZE;
for(i=0;i<TABLE_SIZE; i++)
{
index=(hkey+i)%TABLE_SIZE;
if(h[index]==key)
{
printf("value is found at index %d",index);
break;
}
}
if(i == TABLE_SIZE)
printf("\n value is not found\n");
}
void display()
{
int i;
printf("\nelements in the hash table are \n");
for(i=0;i< TABLE_SIZE; i++)
printf("\nat index %d \t value = %d",i,h[i]);
}
main()
{
int opt,i;
while(1)
{
printf("\nPress 1. Insert\t 2. Display \t3. Search \t4.Exit \n");
scanf("%d",&opt);
switch(opt)
{
case 1:
insert();
break;
case 2:
display();
break;
case 3:
search();
break;
case 4:exit(0);
}
}
}
Infix to postfix:
#include<stdio.h>
#include<ctype.h>
char stack[100];
int top = -1;
void push(char x)
{
top++;
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 == '*' || x == '%') return 2;
return 0; }
int main()
{ char *e, x;
char exp[100];
printf("Enter the INFIX Expression : ");
scanf("%s",exp);
printf("\n\n");
printf("The POSTFIX Expression : ");
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; }
Download