You are on page 1of 19

Program – 1 by dev

Program for various searching and sorting techniques –


 Searching
# Binary search –
class Binary
{
void sear(int n, int a[],int b)
{
int beg=0,end=n-1;
int mid=0,i=0;
mid=(beg+end)/2;
while(b!=a[mid])
{
if(b>a[mid])
beg=mid+1;
else if(b<a[mid])
end=mid-1;
mid=(beg+end)/2;
}
System.out.println("Number found at "+mid);
}
}

# Linear Search-
class Linear
{
void sear(int n, int a[],int b)
{
int count=0;
for(int i=0;i<n;i++)
{
if(a[i]==b)
count++;
}
if(count!=0)
System.out.println("Number found...");
else
System.out.println("Number not found...");
}
}
 sorting
#bubble sort
class Bubble
{
void sort(int n, int a[])
{
int temp=0;
for(int i=0;i<n;i++)
{
for(int j=0;j<n-1-i;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
System.out.println("sorted array is:");
for(int i=0;i<n;i++)
{
System.out.println(a[i]);
}
} }

# Insertion sort
class Insertion
{
void sort(int a[], int n)
{
for (int i = 1; i < n; i++)
{
int j = i;
int b = a[i];
while ((j > 0) && (a[j-1] > b))
{
a[j] = a[j-1];
j--;
}
a[j] = b;
}
System.out.println("sorted array is:");
for(int i=0;i<n;i++)
{
System.out.println(a[i]);
}
}

# Selection sort

class Selection
{
void sort(int a[], int n)
{
for(int i=0; i<n; i++)
{
int x = i;
for(int j=i; j<n; j++)
{
if(a[x]>a[j])
{
x = j;
}
}
int temp = a[i];
a[i] = a[x];
a[x] = temp;
}
System.out.println("sorted array is:");
for(int i=0;i<n;i++)
{
System.out.println(a[i]);
}
}
}
// Mearge sort

class Merge
{
void sort(int a[],int l, int n)
{
int low = l;
int high = n;
if (low >= high)
{
return;
}

int mid = (low + high) / 2;


sort(a, low, mid);
sort(a, mid + 1, high);
int end_low = mid;
int start_high = mid + 1;
while ((l <= end_low) && (start_high <= high))
{
if (a[low] < a[start_high])
{
low++;
}
else
{
int Temp = a[start_high];
for (int k = start_high- 1; k >= low; k--)
{
a[k+1] = a[k];
}
a[low] = Temp;
low++;
end_low++;
start_high++;
}
}

System.out.println("sorted array is:");


for(int i=0;i<=n;i++)
{
System.out.println(a[i]);
}
}
}
// QUICK SORT

class Quick
{
void sort(int a[],int low, int n)
{
int lo = low;
int hi = n;
if(lo >= n)
{
return;
}
int mid = a[(lo + hi) / 2];
while (lo < hi)
{
while (lo<hi && a[lo] < mid)
{
lo++;
}
while (lo<hi && a[hi] > mid)
{
hi--;
}
if (lo < hi)
{
int t = a[lo];
a[lo] = a[hi];
a[hi] = t;
}
}
if (hi < lo)
{
int t = hi;
hi = lo;
lo = t;
}
sort(a, low, lo);
sort(a, lo == low ? lo+1 : lo, n);
System.out.println("sorted array is:");
for(int i=0;i<=n;i++)
{
System.out.println(a[i]);
}
}
}
// Main program-

import java.io.*;
class maine
{
public static void main(String arg[])throws IOException
{
Linear li=new Linear();
Binary bi=new Binary();
Bubble bu=new Bubble();
Insertion in=new Insertion();
Selection se=new Selection();
Merge me=new Merge();
Quick qu=new Quick();

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));


System.out.println("\nEnter how many number u want :");
int n=Integer.parseInt(br.readLine());
int a[]=new int[n];
System.out.println("\nEnter desired numbers into array :");

for(int i=0;i<n;i++)
a[i]=Integer.parseInt(br.readLine());

while(true)
{
System.out.println("\nEnter\n1 FOR SEARCHING \n2 FOR SORTING \n3 FOR EXIT");
System.out.println("\nEnter ur choice :");
int ch=Integer.parseInt(br.readLine());
switch(ch)
{
case 1:
System.out.println("\n\nEnter \n1 LINEAR SEARCH \n2 BINARY SEARCH");
System.out.println("\nEnter ur choice");
int cs=Integer.parseInt(br.readLine());
System.out.println("\nEnter number to be search :");
int b=Integer.parseInt(br.readLine());

if(cs==1)
li.sear(n,a,b);
else if(cs==2)
bi.sear(n,a,b);
else
System.out.println("WRONG INPUT");
break;

case 2:
System.out.println("\nEnter \n1 Bubble sort \n2 insertion sort \n3 selection

sort \n4 merge sort \n5 Quick sort");


System.out.println("\nEnter ur choice :");
int chs=Integer.parseInt(br.readLine());

if(chs==1)
bu.sort(n,a);
else if(chs==2)
in.sort(a,n);
else if(chs==3)
se.sort(a,n);
else if(chs==4)
me.sort(a,0,n-1);
else if(chs==5)
qu.sort(a,0,n-1);
else
System.out.println("WRONG INPUT");

break;

case 3:
System.exit(0);

default:
System.out.println("WRONG INPUT");
}
}
}

Output – searching

C:\dev\Menu>java maine

Enter how many number u want :


5

Enter desired numbers into array :


6
5
7
2
5

Enter
1 FOR SEARCHING
2 FOR SORTING
3 FOR EXIT

Enter ur choice :
1

Enter
1 LINEAR SEARCH
2 BINARY SEARCH

Enter ur choice
1

Enter number to be search :


7
Number found...

Enter
1 FOR SEARCHING
2 FOR SORTING
3 FOR EXIT

Enter ur choice :
1

Enter
1 LINEAR SEARCH
2 BINARY SEARCH

Enter ur choice
2

Enter number to be search :


7
Number found at 2

Enter
1 FOR SEARCHING
2 FOR SORTING
3 FOR EXIT

Enter ur choice : 3
Sorting –
C:\dev\Menu>java maine

Enter how many number u want :


5

Enter desired numbers into array :


4
56
32
0
7

Enter
1 FOR SEARCHING
2 FOR SORTING
3 FOR EXIT

Enter ur choice :
2

Enter
1 Bubble sort
2 insertion sort
3 selection sort
4 merge sort
5 Quick sort

Enter ur choice :
1
sorted array is:
0
4
7
32
56

Enter
1 FOR SEARCHING
2 FOR SORTING
3 FOR EXIT

Enter ur choice :
3
Program – 2
Program for Implementing stack functions–

# PUSH FUNCTION

import java.io.*;
class Push
{
public int push(int top, int b,int a[])
{
if(top==a.length-1)
{
System.out.println("\n Stack overflow!!!");
}
else
{
top++;
a[top]=b;
}
return top;
}
}

# POP FUNCTION

class Pop
{
public int pop(int top,int a[])
{
if(top==-1)
{
System.out.println("\n Stack underflow!!!");
}
else
{
System.out.println("element popped"+a[top]);
top--;
}
return top;
}
}

# DISPLAY FUNCTION

class Display_stack
{
public void display(int top,int a[])
{
if(top==-1)
{
System.out.println("\n Stack empty!!!");
}
else
{
for(int i=top;i>=0;i--)
{
System.out.println("stack element:"+a[i]);
}
}
}
}
#Main program

import java.io.*;
class maine
{
public static void main(String arg[])throws IOException
{
Pop po=new Pop();
Push pu=new Push();
Display_stack di=new Display_stack();
int top=-1;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("\n Enter the size of stack");
int n=Integer.parseInt(br.readLine());
int a[]=new int[n];
while(true)
{
System.out.println("\n\n Enter\n1 PUSH THE ELEMENT IN STACK \n2 POP THE
ELEMENT IN STACK \n3 DISPLAY \n4 EXIT");
System.out.println("\n Enter ur choice");
int ch=Integer.parseInt(br.readLine());
switch(ch)
{
case 1:
System.out.print("\n Enter element to push:");
int b=Integer.parseInt(br.readLine());
top=pu.push(top,b,a);
break;
case 2:
top=po.pop(top,a);
break;
case 3:
di.display(top,a);
break;
case 4:
System.exit(0);
default:
System.out.println("WRONG INPUT");
}
}
}
}
Output –

C:\dev\stack>java maine

Enter the size of stack


3

Enter
1 PUSH THE ELEMENT IN STACK
2 POP THE ELEMENT IN STACK
3 DISPLAY
4 EXIT

Enter ur choice
1

Enter element to push:4

Enter
1 PUSH THE ELEMENT IN STACK
2 POP THE ELEMENT IN STACK
3 DISPLAY
4 EXIT

Enter ur choice
1

Enter element to push:9

Enter
1 PUSH THE ELEMENT IN STACK
2 POP THE ELEMENT IN STACK
3 DISPLAY
4 EXIT

Enter ur choice
1

Enter element to push:0

Enter
1 PUSH THE ELEMENT IN STACK
2 POP THE ELEMENT IN STACK
3 DISPLAY
4 EXIT

Enter ur choice
1

Enter element to push:2

Stack overflow!!!

Enter
1 PUSH THE ELEMENT IN STACK
2 POP THE ELEMENT IN STACK
3 DISPLAY
4 EXIT

Enter ur choice
2
element popped0

Enter
1 PUSH THE ELEMENT IN STACK
2 POP THE ELEMENT IN STACK
3 DISPLAY
4 EXIT

Enter ur choice
3
stack element:9
stack element:4

Enter
1 PUSH THE ELEMENT IN STACK
2 POP THE ELEMENT IN STACK
3 DISPLAY
4 EXIT

Enter ur choice
Program – 3
Program for Implementing Queue functions–

# To add at last

class Addlast
{
public int last(int n,int a[],int front,int rear,int b)
{
if(rear==n-1)
System.out.println("Queue is full");
else
a[++rear]=b;

return rear;
}
}

# Delete at first

class Delfirst
{
public int first(int a[],int front,int rear)
{
if(rear==-1 || front==rear+1)
System.out.println("Queue is empty");
else
{
System.out.println("deleted element is"+a[front]);
front++;
}
return front;
}
}

# Display function

class Display_queue
{
public void display(int a[],int front,int rear)
{
if(front==rear+1 || rear==-1)
{
System.out.println("queue is empty");
}
else
{
for(int i=front;i<=rear;i++)
{
System.out.println("Queue element:"+a[i]);
}
}
}
}

# Maine program

import java.io.*;
class Queue
{
public static void main(String arg[])throws IOException
{
Addlast la=new Addlast();
Delfirst def=new Delfirst();
Display_queue di=new Display_queue();
int front=0;
int rear=-1;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("\n Enter the size of Queue");
int n=Integer.parseInt(br.readLine());
int a[]=new int[n];
while(true)
{
System.out.println("\n1 Insert element \n2 Delete element \n3 Display element of
Queue \n4 EXIT");
System.out.println("\n Enter ur choice");
int ch=Integer.parseInt(br.readLine());
switch(ch)
{

case 1:
System.out.print("\n Enter element in Queue:");
int b=Integer.parseInt(br.readLine());
rear= la.last(n,a,front,rear,b);
break;

case 2:
front=def.first(a,front,rear);
break;

case 3:
di.display(a,front,rear);
break;

case 4:
System.exit(0);

default:
System.out.println("WRONG INPUT");
}
}}}

Output –

C:\dev\queue>java Queue

Enter the size of Queue


3

1 Insert element
2 Delete element
3 Display element of Queue
4 EXIT

Enter ur choice
1

Enter element in Queue:3

1 Insert element
2 Delete element
3 Display element of Queue
4 EXIT

Enter ur choice
1

Enter element in Queue:6

1 Insert element
2 Delete element
3 Display element of Queue
4 EXIT

Enter ur choice
1

Enter element in Queue:0

1 Insert element
2 Delete element
3 Display element of Queue
4 EXIT

Enter ur choice
1

Enter element in Queue:6


Queue is full

1 Insert element
2 Delete element
3 Display element of Queue
4 EXIT

Enter ur choice
2
deleted element is3

1 Insert element
2 Delete element
3 Display element of Queue
4 EXIT

Enter ur choice
3
Queue element:6
Queue element:0

1 Insert element
2 Delete element
3 Display element of Queue
4 EXIT
Enter ur choice

You might also like