Experiment No 2: AIM: Program To Implement Binary Search
Experiment No 2: AIM: Program To Implement Binary Search
Output
enter the size of array 6 plz enter the array elements 1 3 2 4 5 7 enter the item to be searched7 item found at location =6
Experiment no: 1
AIM: Program to implement Stack operations
#include<stdio.h> #include<conio.h> #include<stdlib.h> #define MAXSIZE 10 void push(); int pop(); void traverse(); int stack [MAXSIZE]; int Top=-1; void main() { int choice; char ch; do { clrscr(); printf("\n1.PUSH"); printf("\n2.POP"); printf("\n3.TRAVERSE"); printf("\n4.Exit"); printf("\nenter your choices"); scanf("%d",&choice); switch(choice) { case 1 : push(); break; case 2 : printf("\n the deleted element is %d",pop()); break; case 3 : traverse(); break; case 4 : return;
printf("\n do you wish to continue(Y/N)"); fflush(stdin); scanf("%c",& ch); } while(choice!=4); } void push() { int item; if(Top==MAXSIZE-1) { printf("\n the stack is full"); getch(); exit(0); } else { printf("enter the elements to be inserted"); scanf("%d",& item); Top=Top+1; stack[Top]=item; } } int pop() { int item; if(Top==-1) { printf("the stack is empty"); getch(); exit(0); } else { item=stack[Top]; Top=Top-1; } return(item); } void traverse() {
int i; if(Top==-1) { printf("the stack is empty"); getch(); exit(0); } else { for(i=Top;i>=0;i--) { printf("traverse the elements"); printf("\n%d",stack[i]); } } }
OUTPUT
1. PUSH 2. POP 3. TRAVERSE 4. Exit Enter your choices1 Enter the elements to be inserted 4 Do you wish to continue(Y/N) y
1. PUSH 2. POP 3. TRAVERSE 4. Exit Enter your choices2 The deleted element is 4 Do you wish to continue(Y/N) y
1. PUSH 2. POP 3. TRAVERSE 4. Exit Enter your choices3 Traverse the elements Do you wish to continue(Y/N) n
Experiment no: 3
AIM: Program to implement Linear Search
#include<stdio.h> #include<conio.h> # define size 10 void main() { int a[size],i,n,item; clrscr(); printf("\n enter the size of array"); scanf("%d",&n); printf("enter the elements of an array"); for(i=0;i<n;i++) { scanf("%d",&a[i]); } printf("enter the item to be searched"); scanf("%d",&item); linearsearch(a,n,item); } linearsearch(int a[],int n,int item) { int i,flag; flag=0; for(i=0;i<n;i++) { if(item==a[i]) { flag=1; break; } } if(flag==1) printf("item %d is found at location=%d",item,i+1); else printf("item %d not found",item); getch(); }
Output
enter the size of array7 enter the elements of an array 6 8 6 5 4 3 2 enter the item to be searched6 item 6 is found at location=1
Experiment no: 4
AIM: Program to implement Bubble Sort
#include<stdio.h> #include<conio.h> void main() { int a[10],n,i; printf("enter the size of array"); scanf("%d",&n); for(i=0;i<n;i++) { scanf("%d",&a[i]); } bubblesort(a,n); printf("elements of array after sorting are=\n"); for(i=0;i<n;i++) printf("%d\n",a[i]); getch(); } bubblesort(int a[],int n) { int temp,i,j; for(i=0;i<n;i++) { for(j=0;j<n-i-1;j++) { if(a[j]>a[j+1]) { temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; } } } }
Output
enter the size of array 6 1 6 5 4 3 8 elements of array after sorting are= 1 3 4 5 6 8
Experiment no: 5
AIM: Program to implement Insertion Sort
#include<stdio.h> #include<conio.h> void main() { int a[10],n,k,i,j,temp; clrscr(); printf("enter the size of array"); scanf("%d",&n); printf("enter the elements of array"); for(i=0;i<n;i++) { scanf("%d",&a[i]); } for(k=0;k<n;k++) { temp=a[k]; j=k-1; while(temp< a[j] && (j>=0)) { a[j+1]=a[j]; j=j-1; } a[j+1]=temp; } printf("elements of array after sorting=\n"); for(i=0;i<n;i++) { printf("%d\n",a[i]); } getch(); }
Output
enter the size of array 7 enter the elements of array3 2 1 4 5 6 8 elements of array after sorting= 1 2 3 4 5 6 8
Experiment no: 6
AIM: Program to implement Quick Sort
#include<stdio.h> #include<conio.h> #define max 10 int a[max],i,n,l,h; void main() { void input(void); input(); getch(); } void input(void) { void quicksort(int a[],int l,int h); void output(int a[],int n); printf("enter the size of array"); scanf("%d",&n); printf("\n"); printf("enter the elements of array=\n"); for(i=0;i<n;i++) { scanf("%d",&a[i]); } l=0; h=n-1; quicksort(a,l,h); printf("sorted array is=\n"); output(a,n); } void quicksort(int a[],int l,int h) { int temp,key,low,high; low=0; high=h; key=a[(low+high)/2]; do {
while(key>a[low]) { low++; } while(key<a[high]) { high--; } if(low<=high) { temp=a[low]; a[low++]=a[high]; a[high--]=temp; } } while(low<=high); if(low<high) quicksort(a,low,h); } void output(int a[],int n) { for(i=0;i<n;i++) { printf("%d\n",a[i]); } }
Output
enter the size of array4 enter the elements of array= 1 3 5 2 sorted array is= 1 2 3 5
Experiment no: 7
AIM: Program to implement Queue operations
#include<stdio.h> #include<conio.h> int queue[5]; long front=0,rear=0; void main() { int choice,info; clrscr(); initqueue(); while(1) { clrscr(); printf("1.insert an element in queue\n"); printf("2.delete an element from queue\n"); printf("3.display the queue\n"); printf("4.exit\n"); printf("\n"); printf("enter your choice"); scanf("%d",&choice); switch(choice) { case 1:if(rear<4) { printf("enter the number"); scanf("%d",&info); if(front==-1) { front=0; rear=0; } else rear=rear+1; queue[rear]=info; } else printf("queue is full"); getch();
break; case 2:info; if(front!=-1) { info=queue[front]; if(front==rear) { front=-1; rear=-1; } else front=front+1; printf("number deleted is=%d",info); } else printf("queue is empty"); getch(); break; case 3:display(); getch(); break; case 4: exit(); default:printf("you entered wrong choice"); getch(); break; } } } initqueue() { front=rear=-1; } display() { int i; for(i=front;i<=rear;i++) printf("%d\n",queue[i]); }
Output
1. insert an element in queue 2. delete an element from queue 3. display the queue 4. exit enter your choice1 Enter the elements to be inserted 4 Do you wish to continue(Y/N) y
1. insert an element in queue 2. delete an element from queue 3. display the queue 4. exit enter your choice 3 4 Do you wish to continue(Y/N) n
Experiment no: 8
AIM: Program to implement Selection Sort
#include<stdio.h> #include<conio.h> void main() { int a[10],n,i; clrscr(); printf("enter the size of array"); scanf("%d",&n); for(i=0;i<n;i++) { scanf("%d",&a[i]); } selectionsort(a,n); printf("elements of array after sorting are=\n"); for(i=0;i<n;i++) { printf("%d\n",a[i]); } getch(); } selectionsort(int a[],int n) { int min,loc,temp,i,j; min=a[0]; for(i=0;i<n;i++) { min=a[i]; loc=i; for(j=i+1;j<=n-1;j++) { if(a[j]<min) { min=a[j]; loc=j; } } if(loc!=i)
Output
enter the size of array6 1 2 3 6 5 4 elements of array after sorting are= 1 2 3 4 5 6