1. 1.Program for search an element by using linearsearch
algorithm.
#include <stdio.h>
#include<conio.h>
int main()
{
int array[100], search, c, n;
printf("Enter the number of elements in arrayn");
scanf("%d",&n);
printf("Enter %d integer(s)n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
printf("Enter the number to searchn");
scanf("%d", &search);
for (c = 0; c < n; c++)
{
if (array[c] == search) /* if required element found */
{
printf("%d is present at location %d.n", search, c+1);
break;
}
}
if (c == n)
printf("%d is not present in array.n", search);
3. 2.Program for search an element by using binary search
algorithm.
#include <stdio.h>
#include<conio.h>
int main()
{
int c, first, last, middle, n, search, array[100];
printf("Enter number of elementsn");
scanf("%d",&n);
printf("Enter %d integersn", n);
for (c = 0; c < n; c++)
scanf("%d",&array[c]);
printf("Enter value to findn");
scanf("%d", &search);
first = 0;
last = n - 1;
middle = (first+last)/2;
while (first <= last) {
if (array[middle] < search)
first = middle + 1;
else if (array[middle] == search) {
printf("%d found at location %d.n", search, middle+1);
break;
}
4. else
last = middle - 1;
middle = (first + last)/2;
}
if (first > last)
printf("Not found! %d is not present in the list.n", search);
getch();
}
OUTPUT-
5. 3.Program of sorting elements by using merge sort algorithm.
#include <stdio.h>
#include <conio.h>
int main( )
{
int a[5] = { 11, 2, 9, 13, 57 } ;
int b[5] = { 25, 17, 1, 90, 3 } ;
int c[10] ;
int i, j, k, temp ;
printf ( "Merge sort.n" ) ;
printf ( "nFirst array:n" ) ;
for ( i = 0 ; i <= 4 ; i++ )
printf ( "%dt", a[i] ) ;
printf ( "nnSecond array:n" ) ;
for ( i = 0 ; i <= 4 ; i++ )
printf ( "%dt", b[i] ) ;
for ( i = 0 ; i <= 3 ; i++ )
{
for ( j = i + 1 ; j <= 4 ; j++ )
{
if ( a[i] > a[j] )
{
temp = a[i] ;
a[i] = a[j] ;
6. a[j] = temp ;
}
if ( b[i] > b[j] )
{
temp = b[i] ;
b[i] = b[j] ;
b[j] = temp ;
}
}
}
for ( i = j = k = 0 ; i <= 9 ; )
{
if ( a[j] <= b[k] )
c[i++] = a[j++] ;
else
c[i++] = b[k++] ;
if ( j == 5 || k == 5 )
break ;
}
for ( ; j <= 4 ; )
c[i++] = a[j++] ;
for ( ; k <= 4 ; )
c[i++] = b[k++] ;
printf ( "nnArray after sorting:n") ;
7. for ( i = 0 ; i <= 9 ; i++ )
printf ( "%dt", c[i] ) ;
getch( ) ;
}
OUTPUT-
8. 4.Program of sorting elements by using quick sort algorithm.
#include<stdio.h>
#include<conio.h>
void quicksort(int [10],int,int);
int main(){
int x[20],size,i;
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;
while(i<j){
11. 5.Program of sorting elements by using selection sort
algorithm.
#include <stdio.h>
#include<conio.h>
int main()
{
int array[100], n, c, d, position, swap;
printf("Enter number of elementsn");
scanf("%d", &n);
printf("Enter %d integersn", n);
for ( c = 0 ; c < n ; c++ )
scanf("%d", &array[c]);
for ( c = 0 ; c < ( n - 1 ) ; c++ )
{
position = c;
for ( d = c + 1 ; d < n ; d++ )
{
if ( array[position] > array[d] )
position = d;
}
if ( position != c )
{
swap = array[c];
array[c] = array[position];
16. }
int pop() {
int item;
item = st.s[st.top];
st.top--;
return (item);
}
void display() {
int i;
if (stempty())
printf("nStack Is Empty!");
else {
for (i = st.top; i >= 0; i--)
printf("n%d", st.s[i]);
}
}
int main() {
int item, choice;
char ans;
st.top = -1;
printf("ntImplementation Of Stack");
do {
printf("nMain Menu");
printf("n1.Push n2.Pop n3.Display n4.exit");
17. printf("nEnter Your Choice");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("nEnter The item to be pushed");
scanf("%d", &item);
if (stfull())
printf("nStack is Full!");
else
push(item);
break;
case 2:
if (stempty())
printf("nEmpty stack!Underflow !!");
else {
item = pop();
printf("nThe popped element is %d", item);
}
break;
case 3:
display();
break;
case 4:
exit(0);
18. }
printf("nDo You want To Continue?");
ans = getche();
} while (ans == 'Y' || ans == 'y');
getch();
}
OUTPUT-
19. 8.Program to displayFibonacciseries using recursion.
#include<stdio.h>
#include<conio.h>
void printFibonacci(int);
int main(){
int k,n;
long int i=0,j=1,f;
printf("Enter the range of the Fibonacci series: ");
scanf("%d",&n);
printf("Fibonacci Series: ");
printf("%d %d ",0,1);
printFibonacci(n);
return 0;
}
void printFibonacci(int n){
static long int first=0,second=1,sum;
if(n>0){
sum = first + second;
first = second;
second = sum;
printf("%ld ",sum);
printFibonacci(n-1);
}
getch();
21. 9.Queue implementationusing array.
#include<stdio.h>
#include<conio.h>
#define MAX 10
void insert(int);
int del();
int queue[MAX], rear=0, front=0;
void display();
int main()
{
char ch , a='y';
int choice, token;
printf("1.Insert");
printf("n2.Delete");
printf("n3.show or display");
do
{
printf("nEnter your choice for the operation: ");
scanf("%d",&choice);
switch(choice)
{
case 1: insert(token);
display();
break;
22. token=del();
printf("nThe token deleted is %d",token);
display();
break;
case 3:
display();
break;
default:
printf("Wrong choice");
break;
}
printf("nDo you want to continue(y/n):");
ch=getch();
}
while(ch=='y'||ch=='Y');
getch();
}
void display()
{
int i;
printf("nThe queue elements are:");
for(i=rear;i<front;i++)
{
printf("%d ",queue[i]);
23. }
}
void insert(int token)
{
char a;
if(rear==MAX)
{
printf("nQueue full");
return;
}
do
{
printf("nEnter the token to be inserted:");
scanf("%d",&token);
queue[front]=token;
front=front+1;
printf("do you want to continue insertion Y/N");
a=getch();
}
while(a=='y');
}
int del()
{
int t;
25. 10.Program for insertion, deletion,displayand traversal in
binary search tree.
#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
void insert(int,int );
void delte(int);
void display(int);
int search(int);
int search1(int,int);
int tree[40],t=1,s,x,i;
main()
{
int ch,y;
for(i=1;i<40;i++)
tree[i]=-1;
while(1)
{
cout <<"1.INSERTn2.DELETEn3.DISPLAYn4.SEARCHn5.EXITnEnter your choice:";
cin >> ch;
switch(ch)
{
case 1:
26. cout <<"enter the element to insert";
cin >> ch;
insert(1,ch);
break;
case 2:
cout <<"enter the element to delete";
cin >>x;
y=search(1);
if(y!=-1) delte(y);
else cout<<"no such element in tree";
break;
case 3:
display(1);
cout<<"n";
for(int i=0;i<=32;i++)
cout <<i;
cout <<"n";
break;
case 4:
cout <<"enter the element to search:";
cin >> x;
y=search(1);
if(y == -1) cout <<"no such element in tree";
else cout <<x << "is in" <<y <<"position";