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

program For Binary Search

The document contains code snippets for various data structure programs in C including: 1. Binary search and sequential search algorithms 2. Quicksort, selection sort, and bubble sort sorting algorithms 3. Linked list programs for insertion, deletion, display in single, doubly, circular linked lists 4. Stack and queue implementations using linked lists 5. Reverse string program using a stack.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
66 views

program For Binary Search

The document contains code snippets for various data structure programs in C including: 1. Binary search and sequential search algorithms 2. Quicksort, selection sort, and bubble sort sorting algorithms 3. Linked list programs for insertion, deletion, display in single, doubly, circular linked lists 4. Stack and queue implementations using linked lists 5. Reverse string program using a stack.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 18

//PROGRAM FOR BINARY SEARCH #include<stdio.h> #include<conio.

h> int k[20],N; int binary_search(int x); int main() { int i,t,x; clrscr(); printf("\nEnter N = "); scanf("%d",&N); for(i=1;i<=N;i++) { printf("\nEnter k[%d] = ",i); scanf("%d",&k[i]); } printf("\nYour array is "); for(i=1;i<=N;i++) { printf("%5d",k[i]); } printf("\nEnter a no to search"); scanf("%d",&x); t=binary_search(x); if(t>0) printf("\nThe required no %d is found at %d",x,t); else printf("\nThe required no %d is not found ",x); getch(); return 0; } int binary_search(int x) { int middle=0, low=1, high=N;

while(low<=high) { middle=(low+high)/2 ; if(x>k[middle]) { low=middle+1; } else if(x<k[middle]) { high=middle-1; } else { printf("\nSuccessfull Search"); return(middle); } } printf("\nUnsuccessfull Search"); return(0); } //PROGRAM FOR SEQUENTIAL SEARCH ) printf("\nThe required no %d is found at %d",x,t+1); else printf("\nThe required no %d is not found ",x); getch(); return 0; } int seq_s(int x) { int i=0; for(i=0;i<N;i++) { if(k[i]==x) { printf("\nSuccessfull Search"); return(i); } }

printf("\nUnsuccessfull search"); return(0); } //PROGRAM FOR QUICK SORTING #include<stdio.h> #include<conio.h> int k[20]; void q_sort(int lb,int ub); int main() { int i,lb,ub,N; clrscr(); printf("\nEnter N = "); scanf("%d",&N); for(i=0;i<N;i++) { printf("\nEnter k[%d] = ",i); scanf("%d",&k[i]); } printf("\nBefore Sorting : "); for(i=0;i<N;i++) { printf("%5d",k[i]); } lb=0;ub=N-1; q_sort(lb,ub); printf("\nAfter Sorting : "); for(i=0;i<N;i++) { printf("%5d",k[i]); } getch(); return 0; }

void q_sort(int lb,int ub) { int t,key,f=1,i,j; if(lb<ub) { i=lb; j=ub; key=k[lb]; while(f==1) { i++; while(k[i]<key) i++; while(k[j]>key) j--; if(i<j) { //EXCHANGE k[i] and k[j] t=k[i]; k[i]=k[j]; k[j]=t; } else { f=0; } } //EXCHANGE k[lb] and k[j] t=k[lb]; k[lb]=k[j]; k[j]=t; q_sort(lb,j-1); q_sort(j+1,ub); } }

//PROGRAM FOR SELECTION SORT #include<stdio.h> #include<conio.h> int k[20]; void s_sort(); int main() { int i,N; clrscr(); printf("\nEnter N = "); scanf("%d",&N); for(i=0;i<N;i++) { printf("\nEnter k[%d] = ",i); scanf("%d",&k[i]); } printf("\nBefore Sorting : "); for(i=0;i<N;i++) { printf("%5d",k[i]); } s_sort(); printf("\nAfter Sorting : "); for(i=0;i<N;i++) { printf("%5d",k[i]); } getch(); return 0; } void s_sort() { int t,min_inx=0,last=n,i,j;

for(i=0;i<last-1;i++) { min_inx=i; for(j=i+1;j<last;j++) { if(k[j]<k[min_inx]) min_inx=j; } if(min_inx!=i) { t=k[i]; k[i]=k[min_inx]; k[min_inx]=t; } } return; }

//Write a program to insert, delete and display in linked list


#include<stdio.h> #include<conio.h> struct Stud { int data; struct Stud *next; }; typedef struct Stud node; void disp(node *FIRST); node *insord(node *FIRST,int x); node *delNode(node *FIRST,int x); int main() { node *FIRST=NULL; int ch=1,x; while(ch!=0) { clrscr(); printf("\n1. Create a Node"); printf("\n2. Delete a Node");

printf("\n3. Display"); printf("\n0. Exit"); printf("\nEnter Your Choice : "); scanf("%d",&ch); switch(ch) { case 1: printf("\nEnter no to Insert"); scanf("%d",&x); FIRST=insord(FIRST,x); break; case 2: printf("\nEnter no to Delete"); scanf("%d",&x); FIRST=delNode(FIRST,x); break; case 3: disp(FIRST); break; case 0: printf("\nGood Bye..."); break; default: printf("\nWrong Choice..."); } getch(); } return 0; } node *insord(node *FIRST,int x) { node *temp; node *nn = (node *) malloc(sizeof(node)); nn->data = x; nn->next = NULL; if(FIRST==NULL) { return(nn); }

if(x <= FIRST->data) { nn->next=FIRST; return nn; } temp = FIRST; while(temp->next!=NULL && temp->next->data < x) temp = temp->next; nn->next = temp->next; temp->next = nn; return(FIRST); }

void disp(node *FIRST) { node *temp; printf("\nThe list is\n"); temp=FIRST; while(temp!=NULL) { printf("%d-->",temp->data); temp=temp->next; } } node *delNode(node *FIRST,int x) { node *temp; if(FIRST==NULL) { printf("Linked List is empty"); return FIRST; } if(FIRST->data==x) { temp = FIRST; FIRST = FIRST->next; free(temp); return FIRST;

} temp = FIRST; while(temp->next!=NULL && temp->next->data != x) temp = temp->next; if(temp->next==NULL) { printf("\nNode not found..."); } else { temp->next = temp->next->next; } return FIRST; }

//Program Doubly Linked List


#include<stdio.h> #include<conio.h> struct Stud { int data; struct Stud *pre; struct Stud * next; }; typedef struct Stud node; node *FIRST=NULL,*LAST=NULL; node *insend(int x); void disp(node *FIRST); int main() { int c=1,x; node *temp; clrscr(); while(c==1) { printf("\nEnter no"); scanf("%d",&x); FIRST=insend(x);

printf("\nEnter 1 to continue"); scanf("%d",&c); } disp(FIRST); getch(); return 0; } node *insend(int x) { node *temp; node *nn=(node *)malloc(sizeof(node)); nn->data=x; if(LAST==NULL) { nn->next=NULL; nn->pre=NULL; LAST=nn; return(nn); } temp=FIRST; while(temp->next!=NULL) { temp=temp->next; } temp->next=nn; nn->next=NULL; nn->pre=temp; LAST=nn; return(FIRST); } void disp(node *FIRST) { node *temp; printf("\nThe list is\n"); temp=FIRST; while(temp!=NULL) { printf("%d-->",temp->data);

temp=temp->next; } }

//Program Circular Linked List


#include<stdio.h> #include<conio.h> struct Stud { int data; struct Stud *next; }; typedef struct Stud node; void disp(node *FIRST); node *insend(node *FIRST,int x); int main() { int c=1,x; node *FIRST=NULL; clrscr(); while(c==1) { printf("\nEnter no"); scanf("%d",&x); FIRST=insend(FIRST,x); printf("\nEnter 1 to continue"); scanf("%d",&c); } disp(FIRST); getch(); return 0; } node *insend(node *FIRST,int x) { node *temp; node *nn=(node *) malloc(sizeof(node)); nn->data=x;

if(FIRST==NULL) { nn->next=nn; return(nn); } temp=FIRST; while(temp->next!=FIRST) temp=temp->next; temp->next=nn; nn->next=FIRST; return(FIRST); } void disp(node *FIRST) { node *temp; if(FIRST==NULL) { printf("\nLinked list is empty\n"); return; } printf("\nThe list is\n"); temp=FIRST; while(temp->next!=FIRST) { printf("%d-->",temp->data); temp=temp->next; } printf("%d-->",temp->data); }

//Program Linked List Implementation Using Sorting


#include<stdio.h> #include<conio.h> struct Stud {

int data; struct Stud *next; }; typedef struct Stud node; void disp(node *FIRST); node *insord(node *FIRST,int x); node *delNode(node *FIRST,int x); int main() { int c=1,x; node *FIRST=NULL; clrscr(); while(c==1) { printf("\nEnter no"); scanf("%d",&x); FIRST=insord(FIRST,x); printf("\nEnter 1 to continue"); scanf("%d",&c); } disp(FIRST); getch(); return 0; } node *insord(node *FIRST,int x) { node *temp; node *nn = (node *) malloc(sizeof(node)); nn->data = x; nn->next = NULL; if(FIRST==NULL) { return(nn); } if(x <= FIRST->data) { nn->next=FIRST; return nn;

} temp = FIRST; while(temp->next!=NULL && temp->next->data < x) temp = temp->next; nn->next = temp->next; temp->next = nn; return(FIRST); }

void disp(node *FIRST) { node *temp; printf("\nThe list is\n"); temp=FIRST; while(temp!=NULL) { printf("%d-->",temp->data); temp=temp->next; } }

//Program Linked List Implementation Using Stack


#include<stdio.h> #include<conio.h> struct Stud { int data; struct Stud *next; }; typedef struct Stud node; void disp(node *FIRST); node *insert(node *FIRST,int x); int main() { int c=1,x; node *FIRST=NULL;

clrscr(); while(c==1) { printf("\nEnter no"); scanf("%d",&x); FIRST=insert(FIRST,x); printf("\nEnter 1 to continue"); scanf("%d",&c); } disp(FIRST); getch(); return 0; } node *insert(node *FIRST,int x) { node *nn = (node *)malloc(sizeof(node)); nn->data=x; nn->next=FIRST; return(nn); } void disp(node *FIRST) { node *temp; printf("\nThe list is\n"); temp=FIRST; while(temp!=NULL) { printf("%d-->",temp->data); temp=temp->next; } }

//Program Linked List Implementation Using Queue


#include<stdio.h> #include<conio.h> struct Stud

{ int data; struct Stud *next; }; typedef struct Stud node; void disp(node *FIRST); node *insend(node *FIRST,int x); int main() { int c=1,x; node *FIRST=NULL; clrscr(); while(c==1) { printf("\nEnter no"); scanf("%d",&x); FIRST=insend(FIRST,x); printf("\nEnter 1 to continue"); scanf("%d",&c); } disp(FIRST); getch(); return 0; } node *insend(node *FIRST,int x) { node *temp; node *nn=(node *) malloc(sizeof(node)); nn->data=x; nn->next=NULL; if(FIRST==NULL) { return(nn); } temp=FIRST; while(temp->next!=NULL) temp=temp->next; temp->next=nn;

return(FIRST); }

void disp(node *FIRST) { node *temp; printf("\nThe list is\n"); temp=FIRST; while(temp!=NULL) { printf("%d-->",temp->data); temp=temp->next; } }

//Program of Reverse String Using Stack


#include<stdio.h> #include<conio.h> char s[50]; int top=0; void push(char t); char pop(); int main() { int i; char y,st[50]; clrscr(); printf("\nEnter the string : "); gets(st); for(i=0;i<strlen(st);i++) { push(st[i]); } printf("\n"); for(i=0;i<strlen(st);i++) {

y=pop(); printf("%c",y); } getch(); return 0; } void push(char t) { top++; s[top]=t; } char pop() { char x; x=s[top]; top=top-1; return(x); }

You might also like