0% found this document useful (0 votes)
49 views13 pages

Dslab

/ Linear search without recursion #include #include int main() int a,i,n,m,c=0; clrscr(); scanf("%d",&n); printf("enter the elements of the array: "; for(i=0;in;i++) #include / int linearsearch(int a[],int n,int k); void main

Uploaded by

Gayathri Munji
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)
49 views13 pages

Dslab

/ Linear search without recursion #include #include int main() int a,i,n,m,c=0; clrscr(); scanf("%d",&n); printf("enter the elements of the array: "; for(i=0;in;i++) #include / int linearsearch(int a[],int n,int k); void main

Uploaded by

Gayathri Munji
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/ 13

//Linear search without recursion #include<stdio.h> #include<conio.

h> int main() { int a[10],i,n,m,c=0; clrscr(); printf("Enter the size of an array: "); scanf("%d",&n); printf("Enter the elements of the array: "); for(i=0;i<=n-1;i++){ scanf("%d",&a[i]); } printf("Enter the number to be search: "); scanf("%d",&m); for(i=0;i<=n-1;i++){ if(a[i]==m) { c=1; break; } } if(c==0) printf("The number is not in the list"); else printf("The number is found"); getch(); return 0; } //linear search with recursion #include<stdio.h> #include<conio.h> int linearsearch(int a[],int n,int k); void main() { int i,pos,n,k,a[10]; clrscr(); printf("enter no.of elements\n"); scanf("%d",&n); printf("enter elements into array\n"); for(i=0;i<n;i++) scanf("%d",&a[i]);
1

printf("enter key\n"); scanf("%d",&k); pos=-1; pos=linearsearch(a,n,k); if(pos!=-1) printf("the key %d is found at position %d",k,pos); else printf("element not found\n"); } int linearsearch(int a[],int n,int k) { int i; for(i=0;i<n;i++) { if(k==a[i]) return i; } return(-1); } //Binary search with recursion #include<stdio.h> #include<conio.h> #define max 10 int binarysearch(int a[],int n,int key,int low,int high); void main() { int i,a[max],low,high,mid,n,key,c; clrscr(); printf("\n enter number of elements\n"); scanf("%d",&n); printf("\n enter elements\n"); for(i=0;i<n;i++) scanf("%d",&a[i]); printf("\n enter the key to be searched\n"); scanf("%d",&key); low=0; high=n-1; c=binarysearch(a,n,key,low,high); if(c==1) printf("\n The key is found "); else printf("\n The key is not found");
2

getch(); } int binarysearch(int a[],int n,int key,int low,int high) { int mid; if(low>high) return 0; mid=(low+high)/2; if(key<a[mid]) binarysearch(a,key,n,low,mid-1); else if (key>a[mid]) binarysearch(a,key,n,mid+1,high); else return 1; return 0; } Binary serach without recursion #include<stdio.h> #include<conio.h> int main() { int a[10],i,n,k,c=0,l,h,mid; clrscr(); printf("enter size\n"); scanf("%d",&n); printf("enter elements in ascending order\n"); for(i=0;i<n;i++) scanf("%d",&a[i]); printf("enter key\n"); scanf("%d",&k); l=0; h=n-1; while(l<=h) { mid=(l+h)/2; if(k==a[mid]) { c=1; break; } else if(k<a[mid]) h=mid-1;
3

else l=mid+1; } if(c==1) printf("the number is found\n"); else printf("\n the number is not found"); return 0; } Bubble sort without recursion #include<stdio.h> #include<conio.h> void main() { int a[10],i,j,temp,n; clrscr(); printf("\n enter the max no.of elements u wanna sort \n"); scanf("%d",&n); printf("\n enter the elements u want to sort \n"); for(i=0;i<n;i++) scanf("%d",&a[i]); for(i=0;i<n;i++) for(j=i+1;j<n;j++) { if(a[i]>a[j]) { temp=a[i]; a[i]=a[j]; a[j]=temp; } } for(i=0;i<n;i++) printf("%d\t",a[i]); getch(); } Bubble sort with recursion #include<stdio.h> #include<conio.h> void bubblesort(int a[],int n); void main() { int a[100],i,n;
4

clrscr(); printf("\t how many elements\n"); scanf("%d",&n); printf("\t enter elements\n"); for(i=0;i<n;i++) scanf("%d",&a[i]); printf("Before sorting numbers are\n"); for(i=0;i<n;i++) printf("\t %d \t",a[i]); bubblesort(a,n); printf("\t\n numbers after sort\n"); for(i=0;i<n;i++) printf("\t %d \t",a[i]); getch(); } void bubblesort (int a[],int n) { int i,j,temp; for(i=0;i<n-1;i++) { for(j=i;j<n;j++) { if(a[i]>a[j]) { temp=a[i]; a[i]=a[j]; a[j]=temp; } } } } Selection sort #include<stdio.h> #include<conio.h> void selectionsort(int a[],int n); void main() { int a[100],n,i; clrscr(); printf("enter number of elements\n"); scanf("%d",&n); printf("enter elements\n");
5

for(i=0;i<n;i++) scanf("%d",&a[i]); printf("\n Before sorting numbers are\n"); for(i=0;i<n;i++) printf("%d\t",a[i]); selectionsort(a,n); printf("\n numbers after sort\n"); for(i=0;i<n;i++) printf("%d\t",a[i]); getch(); } void selectionsort(int a[],int n) { int i,min,temp,j; for(i=0;i<n-1;i++) { min=i; for(j=i+1;j<n;j++) { if(a[j]<a[min]) min=j; } temp=a[i]; a[i]=a[min]; a[min]=temp; } } //Insertionsort #include<stdio.h> #include<conio.h> void insertionsort(int a[],int n); void main() { int i,n,a[100]; clrscr(); printf("enter number of elements\n"); scanf("%d",&n); printf("\n enter elements \n"); for(i=0;i<n;i++) scanf("%d",&a[i]); insertionsort(a,n); printf("\n after sorting\n");
6

for(i=0;i<n;i++) printf("%d\n",a[i]); getch(); } void insertionsort(int a[],int n) { int i,j,index; for(i=1;i<n;i++) { index=a[i]; j=i; while ((j>0) && (a[j-1]>index)) { a[j]=a[j-1]; j--; } a[j]=index; } } //Mergesort #include<stdio.h> #include<conio.h> void getdata(int arr[],int n) { int i; printf("enter the data:"); for(i=0;i<n;i++) scanf("%d",&arr[i]); } void display(int arr[],int n) { int i; printf(" "); for(i=0;i<n;i++) printf("%d ",arr[i]); } void sort(int arr[],int low,int mid,int high) { int i,j,k,l,b[20]; l=low; i=low; j=mid+1;
7

while((l<=mid)&&(j<=high)) { if(arr[l]<=arr[j]) { b[i]=arr[l]; l++; } else { b[i]=arr[j]; j++; } i++; } if(l>mid) { for(k=j;k<=high;k++) { b[i]=arr[k]; i++; } } else { for(k=l;k<=mid;k++) { b[i]=arr[k]; i++; } } for(k=low;k<=high;k++) { arr[k]=b[k]; } } void partition(int arr[],int low,int high) { int mid; if(low<high) { mid=(low+high)/2; partition(arr,low,mid);
8

partition(arr,mid+1,high); sort(arr,low,mid,high); } } void main() { int arr[20],n; clrscr(); printf("Enter number of data:"); scanf("%d",&n); getdata(arr,n); partition(arr,0,n-1); display(arr,n); getch(); } Quicksort #include<stdio.h> #include<conio.h> void quicksort(int [10],int,int); int main() { int x[20],size,i; clrscr(); printf("Enter size of the array: "); scanf("%d",&size); printf("Enter %d elements: ",size); for(i=0;i<size;i++) scanf("%d",&x[i]); quicksort(x,0,size-1); printf("Sorted elements: "); for(i=0;i<size;i++) printf(" %d",x[i]); return 0; } void quicksort(int x[10],int first,int last) { int pivot,j,temp,i; if(first<last) { pivot=first; i=first; j=last;
9

while(i<j) { while(x[i]<=x[pivot]&&i<last) i++; while(x[j]>x[pivot]) j--; if(i<j) { temp=x[i]; x[i]=x[j]; x[j]=temp; } } temp=x[pivot]; x[pivot]=x[j]; x[j]=temp; quicksort(x,first,j-1); quicksort(x,j+1,last); } } //stack using array #include<iostream.h> #include<conio.h> #include<stdlib.h> #define max 10 class stack { int stk[5],top; public: stack() { top=-1; } void push(int x) { if(top==max) cout<<"stack is full\n"; else stk[++top]=x; } void pop() {
10

if(top==-1) cout<<"stack empty\n"; else cout<<"deleted element is "<<stk[top--]; } void display() { if(top==-1) cout<<"stack empty\n"; else { cout<<"stack elements are"; for(int i=top;i>=0;i--) cout<<" "<<stk[i]; } } }; void main() { clrscr(); int ch; stack s; while(1) { cout<<"\n 1.push 2.pop 3.display 4.exit"; cout<<"\n enter your choice"; cin>>ch; switch(ch) { case 1: int x; cout<<"enter element\n"; cin>>x; s.push(x); break; case 2: s.pop(); break; case 3: s.display(); break; case 4:
11

exit(0); default: cout<<"invalid choice\n"; } } getch(); } //Queue using array #include<iostream.h> #include<conio.h> #include<stdlib.h> #define max 10 class queue { int que[5],front,rear; public: queue() { rear=-1; front=-1; } void insert(int x) { if(rear==max) cout<<"queue is full\n"; else que[++rear]=x; } void remove() { if(front==rear) cout<<"queue is empty\n"; else cout<<"the deleted element is "<<que[++front]; } void display() { if(rear==front) cout<<"queue is empty\n"; else { cout<<"queue elements are";
12

for(int i=front+1;i<=rear;i++) cout<<" "<<que[i]; } } }; void main() { clrscr(); int ch; queue q; while(1) { cout<<"\n 1.insert 2.remove 3.display 4.exit"; cout<<"\n enter your choice"; cin>>ch; switch(ch) { case 1: int x; cout<<"enter element\n"; cin>>x; q.insert(x); break; case 2: q.remove(); break; case 3: q.display(); break; case 4: exit(0); default: cout<<"invalid choice\n"; } } getch(); }

13

You might also like