Professional Documents
Culture Documents
# 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;
}
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();
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
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
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
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
1 FOR SEARCHING
2 FOR SORTING
3 FOR EXIT
Enter ur choice : 3
Sorting –
C:\dev\Menu>java maine
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
1 PUSH THE ELEMENT IN STACK
2 POP THE ELEMENT IN STACK
3 DISPLAY
4 EXIT
Enter ur choice
1
Enter
1 PUSH THE ELEMENT IN STACK
2 POP THE ELEMENT IN STACK
3 DISPLAY
4 EXIT
Enter ur choice
1
Enter
1 PUSH THE ELEMENT IN STACK
2 POP THE ELEMENT IN STACK
3 DISPLAY
4 EXIT
Enter ur choice
1
Enter
1 PUSH THE ELEMENT IN STACK
2 POP THE ELEMENT IN STACK
3 DISPLAY
4 EXIT
Enter ur choice
1
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
1 Insert element
2 Delete element
3 Display element of Queue
4 EXIT
Enter ur choice
1
1 Insert element
2 Delete element
3 Display element of Queue
4 EXIT
Enter ur choice
1
1 Insert element
2 Delete element
3 Display element of Queue
4 EXIT
Enter ur choice
1
1 Insert element
2 Delete element
3 Display element of Queue
4 EXIT
Enter ur choice
1
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