Professional Documents
Culture Documents
No1-/ Program For Sorting The Array
No1-/ Program For Sorting The Array
import java.io.*;
class sort
{
public static void main(String args[])throws IOException
{
int i=0;
int j=0;
int temp;
int n[]=new int[11];
DataInputStream in=new DataInputStream(System.in);
System.out.println("\nENTER THE LIST=");
for(i=0;i<10;i++)
{
n[i]=Integer.parseInt(in.readLine());
}
for(i=1;i<10;i++)
{
temp=n[i];
j=i-1;
while((j>=0)&&(n[j]>temp))
{
n[j+1]=n[j];
j=j-1;
}
n[j+1]=temp;
}
System.out.println("THE SORTED LIST IS=");
for(i=0;i<10;i++)
{
System.out.println(" "+n[i]);
}
}
}
OUTPUT:-
D:\sem3\maitreyapethe\DSF>java sort
ENTER THE LIST=
10
90
50
60
80
66
2
32
15
56
THE SORTED LIST IS=
2
10
15
32
50
56
60
66
80
90
PROG. NO-2/*PROGRAM FOR TOWER OF HANOI */
class hanoi
{
public static void tower( int n,char frompeg,char topeg,char aux)
{
if(n==1)
{
System.out.println("\n move disks frompeg"+frompeg+"topeg"+topeg);
}
else
{
tower(n-1,frompeg,aux,topeg);
System.out.println("\n more disks frompeg"+frompeg+"topeg"+topeg);
tower(n-1,aux,topeg,frompeg);
}
}
public static void main(String args[])
{
int a=4;
char frompeg1='A';
char topeg1='C';
char aux1='B';
tower(a,frompeg1,topeg1,aux1);
}
}
OUTPUT:-
D:\sem3\maitreyapethe\DSF>java hunai
import java.io.*;
class city
{
public static void main(String args[])
{
byte ss[]={'d','e','h','l','i','\n','m','u','m','b','a','i'};
FileOutputStream outfile;
try
{
outfile=new FileOutputStream("ss.txt");
outfile.write(city);
output.close();
}
catch(IOException e)
{
System.out.println(e);
System.exit(-1);
}
}
}
OUTPUT:
D:\sem3\ maitreyapethe \DSF>javac city.java
D:\sem3\ maitreyapethe \DSF>java city
D:\sem3\ maitreyapethe \DSF>city.txt
dehli
mumbai
PROG. NO-4/*PROGRAM FOR FILE CONCATENATION AND
BUFFERING */
import java.io.*;
class sequencebuffer
{
public static void main(String args[])throws IOException
{
FileInputStream f1=null;
FileInputStream f2=null;
SequenceInputStream f3=null;
f1=new FileInputStream("text1.txt");
f2=new FileInputStream("text2.txt");
f3=new SequenceInputStream(f1,f2);
BufferedInputStream inbuffer=new BufferedInputStream(f3);
BufferedOutputStream outbuffer=new BufferedOutputStream(System.out);
int ch;
while((ch=inbuffer.read())!=-1)
{
outbuffer.write((char)ch);
}
inbuffer.close();
outbuffer.close();
f1.close();
f2.close();
}
}
OUTPUT:-
D:\sem3\maitreyapethe\DSF\file concat>java sequencebuffer
hi this is a program for file concatination
congratulation its working
made by
maitreya pethe
PROGRAM NO-5/*PROGRAM FOR STACK*/
OUTPUT:-
D:\sem3\maitreyapethe\DSF\qu>java demo
87
32
15
86
front = 1
EXIT
PROGRAM NO-7/*PROGRAM FOR QUICK SORT*/
import java.io.*;
class Quick
{
public int a[ ]= {30,20,10,50,40,80,70,90};
public void Quicksort(int low,int high)
{
if(low<high)
{
int m= partition(low,high);
Quicksort(low,m-1);
Quicksort(m+1,high);
}
}
public int partition(int low,int high)
{
int i=low;
int j=high;
int pivot=a[low];
while(i<=j)
{
while(a[i]<=pivot)
i++;
while(a[j]>pivot)
j--;
if(i<j)
swap(i,j);
}
swap(low,j);
return j;
}
public void swap(int i,int j)
{
int temp=a[i];
a[i]=a[j];
a[j]=temp;
}
public void display( )
{
for (int i=0;i<8;i++)
{
System.out.print(" "+a[i]);
}
System.out.print("\n");
}
}
class Q
{
public static void main(String args[ ])throws IOException
{
Quick q= new Quick( );
q.Quicksort(0,7);
q.display( );
}
}
OUTPUT:-
D:\sem3\maitreyapethe\DSF>javac Q.java
D:\sem3\maitreyapethe\DSF>java Q
10 20 30 40 50 70 80 90
PROGRAM NO-8/*PROGRAM FOR MERGE SORT*/
import java.io.*;
class merge sort
{
public static void main(String args[ ])throws IOException
{
int i,n=0;
int increments[]={5,3,1};
int m[]=new int[25];
DataInputStream in = new DataInputStream(System.in);
System.out. Print("Enter how many numbers to be sorted : ");
n = Integer.parseInt(in.readLine());
for(i=0;i<n;i++)
{
System.out. Print("\t\tElement "+(i+1)+"=");
m[i] = Integer.parseInt(in.readLine());
}
merge(m,n);
System.out.println("\nSorted Elements are :"+m[i]);
for(i=0;i<n;i++)
System.out.println("\t\tElement "+(i+1)+"="+m[i]);
}
static void merge(int m[],int n)
{
int sub[] = new int[25];
int i,j,k,l1,l2,u1,u2,size;
size=1;
while(size<n)
{
l1=0;
k=0;
while((l1+size)<n)
{
l2=l1+size;
u1=l2-1;
u2=((l2+size-1)<n)?(l2+size-1):(n-1);
for(i=l1,j=l2;i<=u1 && j<=u2;k++)
if(m[i]<=m[j])
sub[k]=m[i++];
else
sub[k]=m[j++];
for(;i<=u1;k++)
sub[k]=m[i++];
for(;j<=u2;k++)
sub[k]=m[j++];
l1=u2+1;
}
for(i=l1;k<n;i++)
sub[k++]=m[i];
for(i=0;i<n;i++)
m[i]=sub[i];
size*=2;
}
}
}
OUTPUT:-
D:\sem3\maitreyapethe\DSF>java mergesort
Enter how many numbers to be sorted : 10
Element 1=50
Element 2=60
Element 3=10
Element 4=14
Element 5=21
Element 6=40
Element 7=70
Element 8=5
Element 9=8
Element 10=12
import java.io.*;
import java.util.*;
class Insert
{
public int a[];
public Insert(int max)
{
a=new int[max];
}
public void Insertion(int[] a)
{
for (int i=1;i<=4;i++)
{
int temp=a[i];
int j=i-1;
while((j>=0)&&(a[j]>temp))
{
a[j+1]=a[j];
j=j-1;
}
a[j+1]=temp;
}
for(int i=0;i<=4;i++)
System.out.println(" "+a[i]);
}
}
class I
{
public static void main(String args[])throws IOException
{
Insert s=new Insert(6);
s.a[0]=30;
s.a[1]=10;
s.a[2]=40;
s.a[3]=20;
s.a[4]=60;
System.out.println("\n");
System.out.println("Given list is:");
for(int k=0;k<=4;k++)
System.out.print(" "+s.a[k]);
System.out.println("\n");
System.out.println("Sorted list is:");
s.Insertion(s.a);
}
}
OUTPUT:-
D:\Sem3\MAITREYAPETHE\DSF>java demo4
Given list is:
20 10 30 40 50
import java.io.*;
class list
{
public int data;
public list next;
public list( )
{
data=0;
next=null;
}
}
class linearll
{
public static void main(String args[ ])
{
list n1=new list( );
list n2=new list( );
list n3=new list( );
list n4=new list( );
list n5=new list( );
n1.data=10;
n1.next=n2;
n2.data=30;
n2.next=n3;
n3.data=50;
n3.next=n4;
n4.data=70;
n4.next=n5;
n5.data=90;
n5.next=null;
list head;
head=n1;
while(head!=null)
{
System.out.println(head.data);
head=head.next;
}
}
}
OUTPUT:
D:\Sem3\MAITREYAPETHE\DSF>javac linearll.java
D:\Sem3\MAITREYAPETHE\DSF>java linearll
10
30
50
70
90
PROGRAM NO-11 /*PROGRAM FOR TREE*/
import java.io.*;
class node
{
int data;
node left;
node right;
}
class tree
{
node root=new node();
public void insert(node root,int val)throws IOException
{
node New=new node();
New.data=val;
char ch;
InputStreamReader input=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(input);
System.out.println("\n WHERE to insert as a left/right child of "+
root.data);
String s=br.readLine();
ch=s.charAt(0);
if((ch=='r') || (ch=='R'))
{
if(root.right==null)
root.right=New;
else
insert(root.right,val);
}
else
if(root.left==null)
root.left=New;
else
insert(root.left,val);
}
public void Inorder(node root)
{
if(root!=null)
{
Inorder(root.left);
System.out.println(" "+root.data);
Inorder(root.right);
}
}
public void Postorder(node root)
{
if(root!=null)
{
Postorder(root.left);
Postorder(root.right);
System.out.println(" "+root.data);
}
}
public void Preorder(node root)
{
if(root!=null)
{
System.out.println(" "+root.data);
Preorder(root.left);
Preorder(root.right);
}
}
}
class T
{
public static void main(String args[])throws IOException
{
tree tr=new tree();
node w=new node();
w.data=10;
tr.root=w;
tr.insert(tr.root,20);
tr.insert(tr.root,40);
tr.insert(tr.root,50);
tr.insert(tr.root,60);
System.out.println("\n Inorder series:");
tr.Inorder(tr.root);
System.out.println("\n ");
System.out.println("\n Postorder series:");
tr.Postorder(tr.root);
System.out.println("\n ");
System.out.println("\n Postorder series:");
tr.Preorder(tr.root);
}
}
OUTPUT:
D:\Sem3\maitreyapethe\DSF>javac T.java
D:\Sem3\maitreyapethe\DSF>java T
WHERE to insert as a left/right child of 10
r
WHERE to insert as a left/right child of 10
a
WHERE to insert as a left/right child of 10
d
WHERE to insert as a left/right child of 40
r
WHERE to insert as a left/right child of 10
r
WHERE to insert as a left/right child of 20
s
Inorder series:
40
50
10
60
20
Postorder series:
50
40
60
20
10
Postorder series:
10
40
50
20
60
PROGRAM NO-12 /*PROGRAM FOR GRAPH*/
import java.io.*;
class graph
{
public int g[][],v1,v2;
public int size;
public graph(int Max)
{
size=Max;
g=new int[size][size];
}
public void create(int v1,int v2,int flag)
{
if((v1>=size)||(v2>=size))
System.out.println("ERROR");
else
{
g[v1][v2]=1;
}
if(flag==1)
g[v2][v1]=1;
}
public void display()
{
for(int i=0;i<size;i++)
{
for(int j=0;j<size;j++)
System.out.print(" "+g[i][j]);
System.out.println(" \n");
}
}
}
class G
{
public static void main(String args[])
{
graph gr=new graph(3);
gr.create(1,2,0);
gr.create(0,1,0);
gr.create(0,2,0);
System.out.println("Graph is:");
gr.display();
}
}
OUTPUT:
D:\Sem3\MAITREYAPETHE\DSF>javac G.java
D:\Sem3\MAITREYAPETHE\DSF>java G
Graph is:
0 1 1
0 0 1
0 0 0