DS Lab Record (Iii-I-So)
DS Lab Record (Iii-I-So)
STUDENT NAME:……………………………………………………
ROLLNO :………………………………………………………………
BRANCH:………………………………………………………..……
CONTENTS
1.Write Java programs that use both recursive and non-recursive functions for
implementing the linear search.
Source code:
Test
if(r<1)
return -1;
if(arr[1]==x)
return 1;
if(arr[r]==x)
return r;
return recSearch(arr,l+1,r-1,x);
int x=3;
int index=recSearch(arr,0,arr.length-1,x);
if(index!=-1)
else
OUTPUT:
Source code:
class LinearSearch
if(arr[i]==x)
return i;
return -1;
int[]arr={3,4,1,7,5};
int n=arr.length;
int x=4;
int index=search(arr,n,x);
if(index == -1)
else
Source code:
import java.util.*;
class GSG{
if(r>=l&&l<=arr.length-1){
int mid=l+(r-l)/2;
if(arr[mid]==x)
return mid;
if(arr[mid]>x)
return binarySearch(arr,1,mid-1,x);
return binarySearch(arr,mid+1,r,x);}
return -1;
int arr[]={2,3,4,10,40};
int n=arr.length;
int x=10;
int result=ob.binarySearch(arr,0,n-1,x);
if(result==-1)
else
OUTPUT:
element found at index3
9
Source code:
class GGG{
int l=0,r=arr.length-1;
while(l<=r) {
int m=l+(r-l)/2;
if(arr[m]==x)
return m;
if(arr[m]<x)
l=m+1;
else
r=m-1;}
return -1;}
int arr[]={2,3,4,10,40};
int n= arr.length;
int x=12;
int result=ob.binarySearch(arr,x);
if(result==-1)
else
10
}}
OUTPUT:
2. Write Java programs to implement the following using arrays and linked lists
List ADT
Source code:
import java.io.*;
import java.util.*;
class Node {
Object data;
int next;
data = ob;
next = i;
}
12
int MAXSIZE;
Node list[];
ArrayList( int s) {
MAXSIZE = s;
int p;
count++;
list[p-1].next = -1;
System.out.println("***List is FULL");
return; }
int p = getNode();
if( p != -1 ) {
list[p].data = item;
head = p;
count++;
}}
System.out.println("***List is FULL");
return;
int q = getNode();
int p = find(x);
if( q != -1 ) {
list[q].data = item;
list[q].next = list[p].next;
list[p].next = q;
count++;
}}
if(list[p].data == null)
return p;
return -1;
int p = head;
while( p != -1){
p = list[p].next;
return -1;
if( isEmpty() )
return null;
if( list[head].next == -1 )
head = -1;
else
15
head = list[head].next;
int p = find(x);
if( p == -1 || list[p].next == -1 ) {
System.out.println("No deletion");
return null; }
int q = list[p].next;
list[p].next = list[q].next;
count--;
return tmp; }
int p = head;
System.out.print("\nList: [ " );
while( p != -1) {
p = list[p].next;
System.out.println("]\n");
return count;
}}
class ArrayListDemo
linkedList.initializeList();
linkedList.createList(4);
linkedList.display();
System.out.print("InsertFirst 55:");
linkedList.insertFirst(55);
linkedList.display();
linkedList.insertAfter(66, 33);
linkedList.display();
linkedList.display();
System.out.print("InsertFirst 77:");
linkedList.insertFirst(77);
linkedList.display();
item = linkedList.deleteAfter(22);
17
linkedList.display();
}}
OUTPUT :
List: [ 11 22 33 44 ]
InsertFirst 55:
List: [ 55 11 22 33 44 ]
List: [ 55 11 22 33 66 44 ]
Deleted node: 55
List: [ 11 22 33 66 44 ]
InsertFirst 77:
List: [ 77 11 22 33 66 44 ]
Deleted node: 33
List: [ 77 11 22 66 44 ]
size(): 5
18
Source code:
class LinkedList {
class Node{
Object data;
Node next;
Node( Object d ) {
data = d;
}}
Node head;
Node p;
int count;
p = new Node(item);
p.next = head;
head = p;
count++;
p = find(key);
if( p == null )
else {
q.next = p.next;
p.next = q;
count++;
}}
p = head;
while( p != null ) {
p = p.next; }
return null; }
{ if( isEmpty() ) {
return null;
head = tmp.next;
count--;
return tmp.data;
p = find(key);
if( p == null ){
return null;}
System.out.println("No deletion");
return null;}
else{
p.next = tmp.next;
count--;
return tmp.data; }}
p = head;
while( p != null ){
p = p.next; }
System.out.println(p);
return count;
}}
class LinkedListDemo{
list.displayList();
list.insertFirst(55);
list.displayList();
list.insertAfter(66, 33);
list.displayList();
list.displayList(); }
item = list.deleteAfter(22);
list.displayList(); }
}}
deleteFirst(): 55
size(): 0
22
import java.io.*;
import java.util.*;
class stack<T>
ArrayList<T> A;
int top=-1;
int size;
stack(int size)
this.size = size;
void push(T X)
if (top + 1 == size)
{
System.out.println("Stack Overflow");
}
else {
top = top+1;
if (A.size() > top)
A.set(top, X);
else
A.add(X);
23
}
}
T top()
{
if (top == -1)
{
System.out.println("Stack Underflow");
return null;
}
else
return A.get(top);
}
void pop()
{
if (top == -1)
{
System.out.println("Stack Underflow");
}
else
top--;
}
boolean empty()
{
return top == -1;
}
public String toString()
{
String Ans = "";
for (int i= 0;i< top;i++)
{
Ans += String.valueOf(A.get(i)) + "->";
}
Ans += String.valueOf(A.get(top));
return Ans;
}
}
public class GFG
{
public static void main(String[] args)
{
stack<Integer> s1 = new stack<>(3);
s1.push(10);
s1.push(20);
s1.push(30);
System.out.println("s1 after pushing 10, 20 and 30 :\n" + s1);
s1.pop();
24
OUTPUT :
10->20->30
s1 after pop:
10->20
hello->world->java
Stack Overflow
100.0->200.0
200.0
25
Queue(int c)
front=rear=0;
capacity=c;
if (capacity == rear)
System.out.printf("\nQueue is full\n");
return;
else
queue[rear] = data;
rear++;
}
26
return;
if(front == rear)
System.out.printf("\nQueue is empty\n");
return;
else
queue[rear] = 0;
rear--;
return;
int i;
27
if (front == rear)
System.out.printf("\nQueue is Empty\n");
return;
return;
if (front == rear)
System.out.printf("\nQueue is Empty\n");
return;
return;
}}
{
28
q.queueDisplay();
q.queueEnqueue(20);
q.queueEnqueue(30);
q.queueEnqueue(40);
q.queueEnqueue(50);
q.queueDisplay();
q.queueEnqueue(60);
q.queueDisplay();
q.queueDequeue();
q.queueDisplay();
q.queueFront();
}}
OUTPUT:
Queue is Empty
20<--30<--40<--50<--
Queue is full
20<--30<--40<--50<--
30<--40<--50<--
4. Write a java program that reads an infix expression, converts the expression
to postfix form and then evaluates the postfix expression (use stack ADT).
Aim: To implement a java program that reads an infix expression, converts the expression to
postfix form and then evaluates the postfix expression (use stack ADT)
Source code:
import java.io.*;
class Stack
int top=-1;
void push(char c)
try
a[++top]= c;
catch(StringIndexOutOfBoundsException e)
System.exit(0);
char pop()
{
30
return a[top--];
boolean isEmpty()
return (top==-1)?true:false;
char peek()
return a[top];
String infix;
infix = keyboard.readLine();
//output as postfix
char symbol;
for(int i=0;i<infix.length();++i)
symbol = infix.charAt(i);
if (Character.isLetter(symbol))
else if (symbol=='(')
//push (
operators.push(symbol);
else if (symbol==')')
else
operators.push(symbol);
while (!operators.isEmpty())
return postfix;
if (x == '+' || x == '-')
return 1;
return 2;
return 0;
} }
33
OUTPUT:
5. Write Java programs to implement the following using a singly linked list. (a) Stack ADT
(b) Queue ADT
Aim:/ * Java Program to Implement Stack using Linked List*/
Source Code:
import java.util.*;
class Node
public Node()
link = null;
data = 0;
data = d;
link = n;
link = n;
data = d;
35
return link;
return data;
class linkedStack
public linkedStack()
top = null;
size = 0;
{
36
return size;
if (top == null)
top = nptr;
else
nptr.setLink(top);
top = nptr;
size++ ;
if (isEmpty() )
top = ptr.getLink();
size-- ;
return ptr.getData();
{
37
if (isEmpty() )
return top.getData();
System.out.print("\nStack = ");
if (size == 0)
System.out.print("Empty\n");
return ;
System.out.print(ptr.getData()+" ");
ptr = ptr.getLink();
System.out.println();
{
38
char ch;
do
System.out.println("1. push");
System.out.println("2. pop");
System.out.println("3. peek");
System.out.println("5. size");
switch (choice)
case 1 :
ls.push( scan.nextInt() );
break;
case 2 :
try
catch (Exception e)
39
break;
case 3 :
try
catch (Exception e)
break;
case 4 :
break;
case 5 :
break;
case 6 :
System.out.println("Stack = ");
ls.display();
break;
default :
40
break;
ls.display();
ch = scan.next().charAt(0);
OUTPUT :
1. push
2. pop
3. peek
4. check empty
5. size
20
Stack = 20
1. push
41
2. pop
3. peek
4. check empty
5. size
Popped Element = 20
Stack = Empty
1. push
2. pop
3. peek
4. check empty
5. size
Stack = Empty
1. push
2. pop
3. peek
4. check empty
42
5. size
Stack = Empty
1. push
2. pop
3. peek
4. check empty
5. size
Size = 0
Stack = Empty
n
43
Source Code:
import java.util.*;
class Node
public Node()
link = null;
data = 0;
data = d;
link = n;
link = n;
data = d;
}
44
return link;
return data;
class linkedQueue
public linkedQueue()
front = null;
rear = null;
size = 0;
{
45
return size;
if (rear == null)
front = nptr;
rear = nptr;
else
rear.setLink(nptr);
rear = rear.getLink();
size++ ;
if (isEmpty() )
front = ptr.getLink();
if (front == null)
rear = null;
46
size-- ;
return ptr.getData();
if (isEmpty() )
return front.getData();
System.out.print("\nQueue = ");
if (size == 0)
System.out.print("Empty\n");
return ;
System.out.print(ptr.getData()+" ");
ptr = ptr.getLink();
System.out.println();
}}
char ch;
do
System.out.println("\nQueue Operations");
System.out.println("1. insert");
System.out.println("2. remove");
System.out.println("3. peek");
System.out.println("5. size");
switch (choice)
case 1 :
lq.insert( scan.nextInt() );
break;
case 2 :
try
{
48
catch (Exception e)
break;
case 3 :
try
catch (Exception e) {
break;
case 4 :
break;
case 5 :
break;
default :
break;
49
lq.display();
ch = scan.next().charAt(0);
}}
OUTPUT:
Queue Operations
1. insert
2. remove
3. peek
4. check empty
5. size
24
Queue = 24
Queue Operations
1. insert
2. remove
3. peek
50
4. check empty
5. size
Removed Element = 24
Queue = Empty
Queue Operations
1. insert
2. remove
3. peek
4. check empty
5. size
34
Queue = 34
51
Source code:
import java.util.Scanner;
class Task
String job;
int priority;
this.job = job;
this.priority = priority;
class PriorityQueue
{
52
this.capacity = capacity + 1;
heapSize = 0;
heapSize = 0;
return heapSize == 0;
return heapSize;
heap[++heapSize] = newJob;
heap[pos] = heap[pos/2];
pos /=2;
heap[pos] = newJob;
if (isEmpty() )
System.out.println("Heap is empty");
return null;
item = heap[1];
temp = heap[heapSize--];
parent = 1;
child = 2;
{
54
child++;
break;
heap[parent] = heap[child];
parent = child;
child *= 2;
heap[parent] = temp;
return item;
char ch;
do
System.out.println("1. insert");
System.out.println("2. remove");
System.out.println("5. clear");
System.out.println("6. size");
switch (choice)
case 1 :
pq.insert(scan.next(), scan.nextInt() );
break;
case 2 :
break;
case 3 :
break;
case 4 :
break;
case 5 :
pq.clear();
break;
case 6 :
break;
default :
break;
ch = scan.next().charAt(0);
}}
OUTPUT:
1. insert
2. remove
3. check empty
Source Code:
import java.util.Scanner;
class BinarySearchTreeNodes
BinarySearchTreeNodes left,right;
int data;
public BinarySearchTreeNodes()
left=null;
right=null;
data=0;
public BinarysearchTreeNodes(int n)
left=null;
right=null;
data=n;
left=n;
58
right=n;
public BinarySeachTreeNodesgetLeft()
return left;
public BinarySearchTreeNodesgetRight()
return right;
data=d;
return data;
class BinarySearchTree
{
59
private BinarySearchTreeNodesroot;
public BinarySearchTree()
root=null;
return root=null;
root=insert(root,data);
if(node==null)
node=new BinarySearchTreeNodes(data);
else
if(data<=node,getData())
node.left=inset(node.left,data);
else
node.right=insert(node.right,data);
return node;
60
preorder(root);
if(r!=NULL)
System.out.print(r.getData()+" ");
preorder(r.getLeft());
preorder(r.getRight());
}}}
BinarySearchTreebst=new BinarySearchTree();
int n=10;
for(int i=0;i<N;i++)
bst.insert(scan.nextInt());
System.out.print("\n preorder:");
61
bst.preorder();
scan.close();}]
OUTPUT:
12 10 11 03 15 19 01 04 70
Preorder: 12 10 3 2 1 4 11 15 19 70
62
Source Code:
import java.util.Scanner;
import java.util.Stack;
class BinarySearchTreeNode
BinarySearchTreeNode left,right;
int data;
public BinarySearchTreeNode()
left = null;
right = null;
data =0;
public BinarySearchTreeNode(int n)
left = null;
right = null;
data =n;
left =n;
}
63
right = n;
return left;
return right;
data =d;
return data;
class BinarySearchTreeOperations
public BinarySearchTreeOperations()
root = null;
root = insert(root,data);
if(node == null)
else
node.left = insert(node.left,data);
else
node.right = insert(node.right,data);
return node;
65
preorder(root);
s.push(r);
while(!s.empty())
r = s.pop();
System.out.print(r.data+" ");
if(r.right != null)
s.push(r.right);
if(r.left != null)
s.push(r.left);
{
66
int N =10;
bst.insert(scan.nextInt());
System.out.print("\n preorder:");
bst.preorder();
scan.close();
OUTPUT:
12 10 11 03 15 19 01 04 70 80
preorder:12 10 3 1 4 11 15 19 70 80
67
Source code:
import java.util.Scanner;
class BinarySearchTreeNodes
int data;
public BinarySearchTreeNodes()
left = null;
right = null;
data = 0;
public BinarySearchTreeNodes(int n)
left = null;
right = null;
data = n;
left = n;
68
right = n;
return left;
return right;
data = d;
return data;
class BinarySearchTree
{
69
public BinarySearchTree()
root = null;
if (node == null)
else
else
}
70
return node;
inorder(root);
if (r != null)
inorder(r.getLeft());
inorder(r.getRight());
int N = 10;
71
bst.insert(scan.nextInt());
bst.inorder();
scan.close();
OUTPUT:
Enter the first 10 elements of the tree
12 4 95 75 65 20 10 15 14 45
In order : 4 10 12 14 15 20 45 65 75 95
72
Source code:
import java.util.Scanner;
import java.util.Stack;
class BinarySearchTreeNode
int data;
public BinarySearchTreeNode()
left = null;
right = null;
data = 0;
public BinarySearchTreeNode(int n)
left = null;
right = null;
data = n;
left = n;
}
73
right = n;
return left;
return right;
data = d;
return data;
class BinarySearchTreeOperations
public BinarySearchTreeOperations()
root = null;
if (node == null)
else
else
return node;
75
inorder(root);
if (r == null)
return;
if (r != null)
stack.push(r);
r = r.left;
} else
r = stack.pop();
r = r.right;
}
76
int N = 10;
bst.insert(scan.nextInt());
bst.inorder();
scan.close();
OUTPUT:
Enter the first 10 elements of the tree
11 10 20 26 29 78 98 95 45 13
In order : 10 11 13 20 26 29 45 78 95 98
77
source code
import java.util.Scanner;
class BinarySearchTreeNodes
int data;
public BinarySearchTreeNodes()
left = null;
right = null;
data = 0;
public BinarySearchTreeNodes(int n)
left = null;
right = null;
data = n;
left = n;
right = n;
return left;
return right;
data = d;
return data;
class BinarySearchTree
public BinarySearchTree()
root = null;
if (node == null)
else
else
return node;
80
postorder(root);
if (r != null)
postorder(r.getLeft());
postorder(r.getRight());
int N = 10;
bst.insert(scan.nextInt());
bst.postorder();
scan.close();
OUTPUT:
Enter the first 10 elements of the tree
12 10 4 3 16 15 12 19 3 1
Post order : 1 3 3 4 12 10 15 19 16 12
82
Source code:
import java.util.Scanner;
import java.util.Stack;
class BinarySearchTreeNode
int data;
public BinarySearchTreeNode()
left = null;
right = null;
data = 0;
public BinarySearchTreeNode(int n)
left = null;
right = null;
data = n;
left = n;
}
83
right = n;
return left;
return right;
data = d;
return data;
class BinarySearchTreeOperations
public BinarySearchTreeOperations()
84
root = null;
if (node == null)
else
else
return node;
}
85
postorder(root);
if (root == null)
return;
if (node.right != null)
stack.add(node.right);
stack.add(node);
node = node.left;
node = stack.pop();
stack.push(node);
node = nodeRight;
} else
node = null;
int N = 10;
bst.insert(scan.nextInt());
bst.postorder();
scan.close();
}
87
OUTPUT:
12 4 13 7 4 89 78 98 45 20
Post order : 4 7 4 20 45 78 98 89 13 12
88
Aim: To display node values in a level order traversal for a binary tree.
Source Code:
class Node
int data;
Node left,right;
data=item;
left=right=null;
class BinaryTree
Node root;
public BinaryTree()
root=null;
void printLevelOrder()
89
int h=height(root);
int i;
for(i=1;i<=h;i++)
printCurrentLevel(root,i);
if(root==null)
return 0;
else
int lheight=height(root.left);
int rheight=height(root.right);
if(lheight>rheight)
return(lheight+1);
else
return(rheight+1);
if(root==null)
return;
90
if(level==1)
System.out.print(root.data+"");
else if(level>1)
printCurrentLevel(root.left,level-1);
printCurrentLevel(root.right,level-1);
tree.root=new Node(1);
tree.root.left=new Node(2);
tree.root.right=new Node(3);
tree.root.left.left=new Node(4);
tree.root.left.right=new Node(5);
tree.printLevelOrder();
}}
OUTPUT:
12345
91
Source Code:
import java.util.*;
class Bstnode
Bstnode rc,lc;
Bstnode root;
int data;
Bstnode()
data=0;
rc=lc=null;
Bstnode(int item)
data=item;
lc=rc=null;
Bstnode par,ptr;
92
ptr=root;
par=null;
while(ptr!=null)
if(ptr.data==key)
b[0]=par;
b[1]=ptr;
return b;
else if(ptr.data<key)
par=ptr;
ptr=ptr.rc;
else
par=ptr;
ptr=ptr.lc;
b[0]=par;b[1]=ptr;
return b;
93
arr=search(item);
if(root!=null)
Bstnode par=arr[0];
Bstnode ptr=arr[1];
if(ptr!=null)
else
if(par.data<item)
par.rc=nn;
else
par.lc=nn;
else
root = nn;
if(ptr!=null)
inorder(ptr.lc);
System.out.println(ptr.data);
inorder(ptr.rc);
if(ptr!=null)
System.out.println(ptr.data);
inorder(ptr.lc);
inorder(ptr.rc);
if(ptr!=null)
inorder(ptr.lc);
inorder(ptr.rc);
System.out.println(ptr.data);
95
if(par!=null)
if(par.lc==ptr)
par.lc=null;
else
par.rc=null;
else
root=null;
return ptr.data;
if(par!=null)
if(par.lc==ptr)
if(ptr.lc==null)
par.lc=ptr.rc;
else
96
par.lc=ptr.lc;
else if(par.rc==ptr)
if(ptr.lc==null)
par.rc=ptr.rc;
else
par.rc=ptr.lc;
else
if(ptr.rc!=null)
root=ptr.rc;
else
root=ptr.lc;
return ptr.data;
Bstnode ptr1=ptr.rc;
Bstnode par1=null;
while(ptr1.lc!=null)
97
par1=ptr1;
ptr1=ptr1.lc;
if(par1!=null)
if(ptr1.rc!=null)
par1.lc=ptr1.rc;
else
par1.lc=null;
ptr1.lc=ptr.lc;
ptr1.rc=ptr.rc;
else // if par1=null
ptr1.lc = ptr.lc;
if(par!=null)
if(par.lc==ptr)
par.lc=ptr1;
else
par.rc=ptr1;
else
root=ptr1;
98
return ptr.data;
Bstnode ptr=root,par=null;
boolean flag=false;
int k;
while(ptr!=null&&flag==false)
if(item<ptr.data)
par=ptr;
ptr=ptr.lc;
else if(item>ptr.data)
par=ptr;
ptr=ptr.rc;
else
ptr.data=item;
flag=true;
}
99
if(flag==false)
return -1;
if(ptr.lc==null&&ptr.rc==null)
k=deleteleaf(par,ptr);
else if(ptr.lc!=null&&ptr.rc!=null)
k=delete2childnode(par,ptr);
else
k=delete1childnode(par,ptr);
return k;
int ch;
do
System.out.println("1.insert");
System.out.println("2.delete");
System.out.println("3.search");
100
System.out.println("4.inorder");
System.out.println("5.preorder");
System.out.println("6.postorder");
System.out.print("enter ur choice:");
ch=s.nextInt();
switch(ch)
int n=s.nextInt();
b.insert(n);
break;
case 2:if(b.root!=null)
System.out.print("enter element:");
int n1=s.nextInt();
int res=b.deletenode(n1);
if(res!=-1)
else
break;
case 3:if(b.root!=null)
{
101
int key=s.nextInt();
search1=b.search(key);
if(search1[1]!=null)
System.out.println("key is found");
else
if(search1[0]!=null)
if(search1[1]!=null)
else
else
break;
case 4:if(b.root!=null)
b.inorder(b.root);
else
break;
102
case 5:if(b.root!=null)
b.preorder(b.root);
else
break;
case 6:if(b.root!=null)
b.postorder(b.root);
else
break;
}while(ch!=0);
OUTPUT:
1.insert
2.delete
3.search
4.inorder
5.preorder
6.postorder
enter ur choice:3
no elements in tree
103
11. Write Java programs for the implementation of bfs and dfs for a given graph.
Aim: to implement bfs for a given graph
Source code:
import java.io.*;
import java.util.*;
class Graph1
private int V;
@SuppressWarnings("unchecked")Graph1(int v)
V=v;
adj=new LinkedList[v];
for(int i=0;i<v;++i)
adj[i]=new LinkedList();
adj[v].add(w);
void BFS(int s)
LinkedList<Integer>queue=new LinkedList<Integer>();
visited[s]=true;
queue.add(s);
while(queue.size()!=0)
s=queue.poll();
System.out.print(s+"");
Iterator<Integer>i=adj[s].listIterator();
while(i.hasNext())
int n=i.next();
if(!visited[n])
visited[n]=true;
queue.add(n);
g.addEdge(0,1);
g.addEdge(0,2);
105
g.addEdge(1,2);
g.addEdge(2,0);
g.addEdge(2,3);
g.addEdge(3,3);
g.BFS(2);
OUTPUT:
2031
106
12. Write Java programs for implementing the following sorting methods: (a)
Bubble sort (b) Selection sort (c) Insertion sort (d) Radix sort
Source code
class BubbleSort{
int n = arr.length;
for(int i =0;i<n-1;i++)
for(int j =0;j<n-i-1;j++)
arr[j] = arr[j+1];
arr[j+1] = temp;
int n =arr.length;
for(int i=0;i<n;i++)
System.out.print(arr[i]+" ");
System.out.println();
}
107
ob.bubblesort(arr);
System.out.println("sorted array");
ob.PrintArray(arr);
OUTPUT:
sorted array
11 12 22 25 34 64 90
108
Source code:
import java.io.*;
import java.util.*;
class SelectionSort
int n=arr.length;
for(int i=0;i<n-1;i++){
int min_idx=i;
for(int j=i+1;j<n;j++)
if(arr[j]<arr[min_idx])
min_idx=j;
int temp=arr[min_idx];
arr[min_idx]=arr[i];
arr[i]=temp;
}}
int n=arr.length;
for(int i=0;i<n;++i)
System.out.println(arr[i]+" ");
System.out.println();
int arr[]={64,25,12,22,11};
ob.sort(arr);
System.out.println("SAorted array");
ob.printArray(arr);
11
12
22
25
64
110
Source code:
import java.io.*;
import java.util.*;
class InsertionSort{
int n=arr.length;
for(int i=1;i<n;++i){
int key=arr[i];
int j=i-1;
arr[j+1]=arr[j];
j=j-1;
arr[j+1]=key;
}}
int n=arr.length;
for(int i=0;i<n;++i)
System.out.print(arr[i]+" ");
System.out.println();
int arr[]={12,11,13,5,6};
ob.sort(arr);
printArray(arr);
}}
OUTPUT:
5 6 11 12 13
112
Source code:
import java.io.*;
import java.util.*;
class Radix{
int mx=arr[0];
for(int i=1;i<n;i++)
if(arr[i]>mx)
mx=arr[i];
return mx;
int i;
Arrays.fill(count,0);
for(i=0;i<n;i++)
count[(arr[i]/exp)%10]++;
for(i=1;i<10;i++)
count[i]+=count[i-1];
for(i=n-1;i>=0;i--){
output[count[(arr[i]/exp)%10]-1]=arr[i];
113
count[(arr[i]/exp)%10]--;
for(i=0;i<n;i++)
arr[i]=output[i];
int m=getMax(arr,n);
for(int exp=1;m/exp>0;exp*=10)
countSort(arr,n,exp);
for(int i=0;i<n;i++)
System.out.println(arr[i]+" ");
int arr[]={170,45,75,90,802,24,2,66};
int n=arr.length;
radixSort(arr,n);
print(arr,n);
}}
OUTPUT:
24
45
114
66
75
90
170
802
115
13. Write a Java program for implementing KMP pattern matching algorithm.
Source code:
class KMP_String_Matching
int M=pat.length();
int N=txt.length();
int j=0;
computeLPSArray(pat,M,lps);
int i=0;
while((N-i)>=(M-j))
if(pat.charAt(j)==txt.charAt(i)){
j++;
i++;
if(j==M){
j=lps[j-1];
}
116
if(j!=0)
j=lps[j-1];
else
i=i+1;
int len=0;
int i=1;
lps[0]=0;
while(i<M){
if(pat.charAt(i)==pat.charAt(len))
len++;
lps[i]=len;
i++;
else
if(len!=0){
117
len=lps[len-1];}
else{
lps[i]=len;
i++;
String txt="ABABDABACDABABCABAB";
String pat="ABABCABAB";
new KMP_String_Matching().KMPSearch(pat,txt);
OUTPUT: