Showing Page:
1/12
SEARCHING AND SORTING TECHNIQUES
//non-recursive linear search
#include<stdio.h>
#include<stdlib.h>
void lsearch(int[],int,int);
void main()
{
int a[50],n,i,element;
printf("\n Enter no-of elements:");
scanf("%d",&n);
printf("\n Enter array elements:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\n Enter element to be searched:");
scanf("%d",&element);
lsearch(a,n,element);
}
void lsearch(int a[],int n,int element)
{
int i,flag=0;
for(i=0;i<n;i++)
{
if(a[i]==element)
{
printf("\n element is found at index: %d\n",i);
flag=1;
break;
}
}
if(flag==0)
}
//recursive linear search
#include<stdio.h>
#include<stdlib.h>
void lsearch(int[],int,int,int);
void main()
{
int a[50],n,i,element;
printf("\n Enter no-of elements:");
scanf("%d",&n);
printf("\n Enter array elements:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
Showing Page:
2/12
printf("\n Enter element to be searched:");
scanf("%d",&element);
lsearch(a,0,n,element);
}
void lsearch(int a[],int i,int n,int element)
{
int flag=0;
if(i==n)
else if(a[i]==element)
{
printf("\n element is found at index: %d\n",i);
flag=1;
}
else
lsearch(a,i+1,n,element);
}
//binary search Non-recursive
#include<stdio.h>
#include<stdlib.h>
void bisearch(int [],int ,int ,int );
void main()
{
int a[50],n,i,element,l,m,h;
printf("\n Enter no-of elements:");
scanf("%d",&n);
printf("\n Enter array elements:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\n Enter element to be searched:");
scanf("%d",&element);
l=0;h=n-1;
bisearch(a,l,h,element);
}
void bisearch(int a[],int l,int h,int element)
{
int flag=0,m;
while(l<=h)
{
m=(l+h)/2;
if(element==a[m])
{
Showing Page:
3/12
printf("\n element is found at index: %d ",m);
flag=1; break;
}
else if(element<a[m])
h=m-1;
else
l=m+1;
}
if(flag==0)
}
//binary search using recursion
#include<stdio.h>
#include<stdlib.h>
void bisearch(int [],int ,int ,int );
void main()
{
int a[50],n,i,element,l,m,h;
printf("\n Enter no-of elements:");
scanf("%d",&n);
printf("\n Enter array elements:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\n Enter element to be searched:");
scanf("%d",&element);
l=0;h=n-1;
bisearch(a,l,h,element);
}
void bisearch(int a[],int l,int h,int element)
{
int flag=0,m;
if(l>h)
else
{
m=(l+h)/2;
if(element==a[m])
{
printf("\n element is found at index: %d ",m);
}
else if(element<a[m])
bisearch(a,l,m-1,element);
else
bisearch(a,m+1,h,element);
}
}
Showing Page:
4/12
// C program for Bubble sort
#include <stdio.h>
void bubblesort(int [],int);
int a[100];
void main()
{
int a[100],i,j,n,temp; int *p;
printf("\nEnter number of elements\n");
scanf("%d", &n);
printf("\nEnter elements:");
for(i=0;i<n;i++)
scanf("%d", &a[i]);
printf("\nBefore sorting:\n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
bubblesort(a,n);
printf("\nSorted list in ascending order:\n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
}
void bubblesort(int a[],int n)
{
int i,j,temp;
for(i=0;i<(n-1);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;
}
}
}
}
// C program for selection sort
#include <stdio.h>
void selectionsort(int a[],int n);
void main()
{
Showing Page:
5/12
int a[100],i,j,n,temp,pos;
printf("\nEnter number of elements\n");
scanf("%d", &n);
printf("\nEnter elements:");
for(i=0;i<n;i++)
scanf("%d", &a[i]);
printf("\nBefore sorting:\n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
selectionsort(a,n);
printf("\nSorted list in ascending order:\n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
}
void selectionsort(int a[],int n)
{
int i,j,pos,temp;
for(i=0;i<n-1;i++)
{
pos=i;
for(j=i+1;j<n;j++)
{
if(a[j]<a[pos])
pos=j;
}
temp=a[i];
a[i]=a[pos];
a[pos]=temp;
}
}
// C program for insertion sort
#include <stdio.h>
void insertionsort(int a[], int n)
void main()
{
int a[100],i,j,n,temp;
printf("\nEnter number of elements\n");
scanf("%d", &n);
printf("\nEnter elements:");
for(i=0;i<n;i++)
scanf("%d", &a[i]);
printf("\nBefore sorting:\n");
Showing Page:
6/12
for(i=0;i<n;i++)
printf("%d\t",a[i]);
insertion sort(a,n);
printf("\nSorted list in ascending order:\n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
}
void insertionsort(int a[], int n)
{
int i,j,temp;
for(i=1;i<=n-1;i++)
{
temp=a[i];
for(j=i-1;j>=0&&a[j]>temp;j--)
{
a[j+1]=a[j] ;
}
a[j+1]=temp;
}
}
Showing Page:
7/12
// C program for MERGE SORT
#include<stdio.h>
void merge(int [],int ,int ,int );
void partition(int [],int ,int );
main()
{
int a[30],i,n;
system("clear");
printf("\nEnter no. of elements : ");
scanf("%d",&n);
printf("\n Enter Elements:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
partition(a,0,n-1);
printf("\n sorted elements are: ");
for(i=0;i<n;i++)
printf(" %d\t ",a[i]);
}
void partition(int a[],int first,int last)
{
int mid;
if(first<last)
{
mid=(first+last)/2;
partition(a,first,mid);
partition(a,mid+1,last);
merge(a,first,mid,last);
}
}
Showing Page:
8/12
void merge(int a[],int first,int mid,int last)
{
int b[30],i,j,k,l,size;
i=first;
j=mid+1;
k=0;
size=last-first+1;
while(i<=mid&&j<=last)
{
if(a[i]<a[j])
b[k++]=a[i++];
else
b[k++]=a[j++];
}
while(i<=mid)
b[k++]=a[i++];
while(j<=last)
b[k++]=a[j++];
for(l=0; l<size; l++)
a[first+l]=b[l];
}
Showing Page:
9/12
Implementation of Queue using two Stacks
.
A queue can be implemented using two stacks. Let queue to be implemented be q and stacks used to
implement q be stack1 and stack2. q can be implemented in two ways:
Method 1 (By making deQueue operation costly)In this method, in en-queue operation, the new
element is entered at the top of stack1. In de-queue operation, if stack2 is empty then all the elements
are moved to stack2 and finally top of stack2 is returned.
enQueue(q, x)
1) Push x to stack1 (assuming size of stacks is unlimited).
deQueue(q)
1) If both stacks are empty then error.
2) If stack2 is empty
While stack1 is not empty, push everything from stack1 to stack2.
3) Pop the element from stack2 and return it.
4) While stack2 is not empty, push everything from stack2 to stack1.
Method2 (By making enQueue operation costly) This method makes sure that oldest entered
element is always at the top of stack 1, so that deQueue operation just pops from stack1. To put the
element at top of stack1, stack2 is used.
enQueue(q, x):
While stack1 is not empty, push everything from stack1 to stack2.
Push x to stack1 (assuming size of stacks is unlimited).
Push everything back from stack2 to stack1.
deQueue(q):
If stack1 is empty then error
Pop an item from stack1 and return it
// Program to implement Queue using Stack in C.
#include<stdio.h>
#include<stdlib.h>
#define N 50
int stack1[5], stack2[5]; // declaration of two stacks
// declaration of top variables.
int top1=-1, top2=-1;
int count=0;
// inserting the elements in stack1.
Showing Page:
10/12
void push1(int data)
{
// Condition to check whether the stack1 is full or not.
if(top1==N-1)
{
printf("\n Stack is overflow...");
}
else
{
top1++; // Incrementing the value of top1
stack1[top1]=data; // pushing the data into stack1
}
}
// Removing the elements from the stack1.
int pop1()
{
// Condition to check whether the stack1 is empty or not.
if(top1==-1)
{
printf("\nStack is empty..");
}
else
{
int a=stack1[top1]; // Assigning the topmost value of stack1 to 'a' variable.
top1--; // decrementing the value of top1.
return a;
}
}
// pushing the data into the stack2.
void push2(int x)
{
// Condition to check whether the stack2 is full or not
if(top2==N-1)
{
printf("\nStack is full..");
}
else
{
top2++; // incrementing the value of top2.
stack2[top2]=x; // assigning the 'x' value to the Stack2
}
}
// Removing the elements from the Stack2
int pop2()
{
int element = stack2[top2]; // assigning the topmost value to element
top2--; // decrement the value of top2
return element;
}
Showing Page:
11/12
void enqueue(int x)
{
push1(x);
count++;
}
void dequeue()
{
if((top1==-1) && (top2==-1))
{
printf("\nQueue is empty");
}
else
{
for(int i=0;i<count;i++)
{
int element = pop1();
push2(element);
}
int b= pop2();
printf("\nThe dequeued element is %d", b);
printf("\n");
count--;
for(int i=0;i<count;i++)
{
int a = pop2();
push1(a);
}
}
}
void display()
{
for(int i=0;i<=top1;i++)
{
printf("%d \t ", stack1[i]);
}
}
void main()
{
int ch,a,x;
while(1)
{
printf("\n__________________________");
printf("__________________________");
printf("\n\t1.INSERTION");
printf("\n\t2.DELETION");
printf("\n\t3.DISPLAY");
printf("\n\t4.EXIT");
Showing Page:
12/12
printf("\n Enter ur choice:");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("\n enter element to insert:");
scanf("%d",&a);
enqueue(a);
break;
case 2: dequeue();
break;
case 3: display();
break;
case 4 : exit(0);
break;
default: printf("\n wrong choice\n");
}
}
}

### Unformatted Attachment Preview

SEARCHING AND SORTING TECHNIQUES //non-recursive linear search #include #include void lsearch(int[],int,int); void main() { int a[50],n,i,element; printf("\n Enter no-of elements:"); scanf("%d",&n); printf("\n Enter array elements:"); for(i=0;i #include void lsearch(int[],int,int,int); void main() { int a[50],n,i,element; printf("\n Enter no-of elements:"); scanf("%d",&n); printf("\n Enter array elements:"); for(i=0;i #include void bisearch(int [],int ,int ,int ); void main() { int a[50],n,i,element,l,m,h; printf("\n Enter no-of elements:"); scanf("%d",&n); printf("\n Enter array elements:"); for(i=0;i #include void bisearch(int [],int ,int ,int ); void main() { int a[50],n,i,element,l,m,h; printf("\n Enter no-of elements:"); scanf("%d",&n); printf("\n Enter array elements:"); for(i=0;ih) printf("\n Element not found"); else { m=(l+h)/2; if(element==a[m]) { printf("\n element is found at index: %d ",m); } else if(element void bubblesort(int [],int); int a[100]; void main() { int a[100],i,j,n,temp; int *p; printf("\nEnter number of elements\n"); scanf("%d", &n); printf("\nEnter elements:"); for(i=0;ia[j+1]) { temp = a[j]; a[j]=a[j+1]; a[j+1]=temp; } } } } // C program for selection sort #include void selectionsort(int a[],int n); void main() { int a[100],i,j,n,temp,pos; printf("\nEnter number of elements\n"); scanf("%d", &n); printf("\nEnter elements:"); for(i=0;i void insertionsort(int a[], int n) void main() { int a[100],i,j,n,temp; printf("\nEnter number of elements\n"); scanf("%d", &n); printf("\nEnter elements:"); for(i=0;i=0&&a[j]>temp;j--) { a[j+1]=a[j] ; } a[j+1]=temp; } } // C program for MERGE SORT #include void merge(int [],int ,int ,int ); void partition(int [],int ,int ); main() { int a[30],i,n; system("clear"); printf("\nEnter no. of elements : "); scanf("%d",&n); printf("\n Enter Elements:"); for(i=0;i #include #define N 50 int stack1[5], stack2[5]; // declaration of two stacks // declaration of top variables. int top1=-1, top2=-1; int count=0; // inserting the elements in stack1. void push1(int data) { // Condition to check whether the stack1 is full or not. if(top1==N-1) { printf("\n Stack is overflow..."); } else { top1++; // Incrementing the value of top1 stack1[top1]=data; // pushing the data into stack1 } } // Removing the elements from the stack1. int pop1() { // Condition to check whether the stack1 is empty or not. if(top1==-1) { printf("\nStack is empty.."); } else { int a=stack1[top1]; // Assigning the topmost value of stack1 to 'a' variable. top1--; // decrementing the value of top1. return a; } } // pushing the data into the stack2. void push2(int x) { // Condition to check whether the stack2 is full or not if(top2==N-1) { printf("\nStack is full.."); } else { top2++; // incrementing the value of top2. stack2[top2]=x; // assigning the 'x' value to the Stack2 } } // Removing the elements from the Stack2 int pop2() { int element = stack2[top2]; // assigning the topmost value to element top2--; // decrement the value of top2 return element; } void enqueue(int x) { push1(x); count++; } void dequeue() { if((top1==-1) && (top2==-1)) { printf("\nQueue is empty"); } else { for(int i=0;i
User generated content is uploaded by users for the purposes of learning and should be used following Studypool's honor code & terms of service.
Studypool
4.7
Trustpilot
4.5
Sitejabber
4.4
Similar Documents