Vous êtes sur la page 1sur 24

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

move disks frompegAtopegB

more disks frompegAtopegC

move disks frompegBtopegC

more disks frompegAtopegB

move disks frompegCtopegA

more disks frompegCtopegB

move disks frompegAtopegB

more disks frompegAtopegC

move disks frompegBtopegC

more disks frompegBtopegA

move disks frompegCtopegA

more disks frompegBtopegC

move disks frompegAtopegB

more disks frompegAtopegC

move disks frompegBtopegC


PROG. NO-3/*PROGRAM FOR COPY BYTES */

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*/

public interface Stack


{
public Object peek();
public Object pop();
public void push(Object object);
public int size();
}
public class ArrayStack implements Stack
{
private Object[] a;
private int size;
}
public ArrayStack(int capacity)
{
a=new Object[capacity];
}
public boolean isEmpty()
{
return (size==0);
}
public Object peek()
{
if (size==0) throw new IllegalStateException("stack is empty");
return a[size-1];
}
public Object pop()
{
if (size==0) throw new IllegalStateException("stack is empty");
Object object=a[--size];
a[size]=null;
return object;
}
public void push(Object object)
{
if (size==a.length) resize();
a[size++]=object;
}
public int size()
{
return size;
}
private void resize()
{
Object[] aa=a;
a=new Object[2*aa.length];
System.arraycopy(aa,0,a,0,size);
}
}
public class testarraystack
{
public static void main(String args[])
{
Stack crates=new ArrayStack(4);
crates.push("Carrots");
crates.push("Orange");
crates.push("abcde");
crates.push("pickles");
crates.push("banana");
System.out.println("crates.size():"+crates.size()+"\t
crates.peek():"+crates.peek());
System.out.println("crates.pop()"+crates.pop());
System.out.println("crates.pop()"+crates.pop());
System.out.println("crates.pop()"+crates.pop());
System.out.println("crates.size():"+crates.size()+"\t
crates.peek():"+crates.peek());
System.out.println("crates.size():"+crates.size()+"\t
crates.peek():"+crates.peek());
System.out.println(“crates.pop()”+crates.pop());
System.out.println(“crates.pop()”+crates.pop());
System.out.println(“crates.pop()”+crates.pop());
System.out.println(“crates.pop()”+crates.pop());
System.out.println(“crates.pop()”+crates.pop());
}
}
Output:
D:\Sem3\maitreyapethe\DSF>java testarraystack
crates.size():5
crates.peek():banana
crates.pop()banana
crates.pop()pickles
crates.pop()abcde
crates.size():2
crates.peek():Orange
crates.size():2
crates.peek():Orange
crates.pop()Orange
crates.pop()Carrots
PROGRAM NO-6/*PROGRAM FOR QUEUE*/
import java.io.*;
class queue
{
private int size;
private long que[];
private int front;
private int rear;
private int count;
public queue(int max)
{
size=max;
que=new long[size];
front=0;
rear=-1;
count=0;
}
void insert(int a)
{
que[++rear]=a;
count++;
}
void delete()
{
front++;
}
void display()
{
for(int i=front;i<=rear;i++)
{
System.out.println(" "+que[i]);
}
System.out.println("\n");
System.out.println("front = "+front);
}
}
class demo
public static void main(String args[])
{
queue q1=new queue(5);
q1.insert(12);
q1.insert(87);
q1.insert(32);
q1.insert(15);
q1.insert(86);
q1.delete();
q1.display();
System.out.println("EXIT");
}
}

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

Sorted Elements are :-


5 8 10 12 14 21 40 50 60 70
PROGRAM NO-9/*PROGRAM FOR INSERTION SORT*/

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

Sorted list is:


10
20
30
40
50
PROGRAM NO-10 /*PROGRAM FOR LINEAR LINKED LIST*/

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

Vous aimerez peut-être aussi