Posts

Showing posts from January, 2019

Binary search in C

Image
Binary search in C Binary Search: in computer science,  binary search , also known as half-interval  search , logarithmic  search , or  binary chop, is a  search  algorithm that finds the position of a target value within a sorted array. It necessary that given data is sorted in ascending order or descending order. below is the program of binary search for ascending ordered data. PROGRAM   : #include <stdio.h> #include <stdlib.h> int main(int n,int a[10]) { int i,mid,key,ub,lb; printf("How many numbers you wants to enter="); scanf("%d",&n); lb=0; ub=n-1; printf("\n%d elements are=",n); for(i=0;i<n;i++){ printf("\na[%d]=",i); scanf("%d",&a[i]); } printf("\nKey=",n); scanf("%d",&key); mid=(lb+ub)/2; while(a[mid]!=key && lb<=ub){ if(key<a[mid]){ ub=mid-1; }

Stack using C

Image
Stack using C PROGRAM : For Stack using C #include <stdio.h> #include <stdlib.h> #define MAX 4 struct stack { int a[MAX]; int top; }st; int stfull(){ if(st.top>=MAX-1)return 1; else return 0; } int push(int val){ st.top++; st.a[st.top]=val; } int stempty(){ if(st.top==-1)return 1; else return 0; } int pop(){ st.top--; return 0; } int display(){ int i; if(stempty()==1)printf("\nstack is empty\n"); else{ printf("\n"); for(i=st.top;i>=0;i--){ printf("|%d|",st.a[i]); } } return 0; } /* Main function */ int main() { int val; int choice; st.top=-1; label: printf("\nselect the choice\n"); printf("1=push\n2=pop\n3=peep\n4=display\n5=clear screen\n"); scanf("%d",&choice); switch(choice){ case 1: if(stfu