Adapdf
Adapdf
Adapdf
Certificate Of Completion
SIGN : DATE :
Practical:1
Aim: Sorting algorithms. Selection sort, Insertion sort, Bubble sort, Merge sort and Quick sor
1.Selection Sort
class SelectionSort
{
void sort(int arr[])
{
int n = arr.length;
2.Insertion Sort
public class InsertionSort {
void sort(int arr[])
{
int n = arr.length;
for (int i = 1; i < n; ++i) {
int key = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}
static void printArray(int arr[])
{
int n = arr.length;
System.out.println("munavvar popatiya");
System.out.println("sorted array ");
for (int i = 0; i < n; ++i)
System.out.print(arr[i] + " ");
System.out.println();
}
printArray(arr);
}
};
Output
3.Bubble Sort
class BubbleSort {
void bubbleSort(int arr[])
{
int n = arr.length;
for (int i = 0; i < n - 1; i++)
for (int j = 0; j < n - i - 1; j++)
if (arr[j] > arr[j + 1]) {
// swap arr[j+1] and arr[j]
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
int i = 0, j = 0;
int k = l;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k] = L[i];
i++;
}
else {
arr[k] = R[j];
j++;
}
k++;
}
System.out.println("Munavvar Popatiya");
System.out.println("Given Array");
printArray(arr);
5.Quick Sort
import java.io.*;
class Quicksort {
System.out.println();
}
public static void main(String[] args)
{
int[] arr = { 40, 27, 38, 79, 21, 51 };
int n = arr.length;
quickSort(arr, 0, n - 1);
System.out.println("Munavvar Popatiya");
System.out.println("Quick Sorted array: ");
printArray(arr, n);
}
}
OUTPUT:
Practical:2
Aim: Searching Algorithm: linear and binary search.
1.linera Search:
public class linear {
public static int search(int arr[], int x)
{
int N = arr.length;
for (int i = 0; i < N; i++) {
if (arr[i] == x)
return i;
}
return -1;
}
public static void main(String args[])
{
int arr[] = { 2, 3, 4, 10, 40 };
int x = 10;
System.out.println("Munavvar Popatiya");
System.out.println("Searched Element " + x);
int result = search(arr, x);
if (result == -1)
System.out.print(
"Element is not present in array");
else
System.out.print("Element is present at index "
+ result);
}
}
OUTPUT:
2.Binary Search
class BinarySearch{
public static void binarySearch(int arr[], int first, int
last, int key){
System.out.println("Munavvar Popatiya");
System.out.println("Element Searched " +key);
int mid = (first + last)/2;
while( first <= last ){
if ( arr[mid] < key ){
first = mid + 1;
}else if ( arr[mid] == key ){
System.out.println("Element is found at index:
" + mid);
break;
}else{
last = mid - 1;
}
mid = (first + last)/2;
}
if ( first > last ){
System.out.println("Element is not found!");
}
}
public static void main(String args[]){
int arr[] = {22,13,47,51};
int key = 47;
int last=arr.length-1;
binarySearch(arr,0,last,key);
}
}
OUTPUT:
Practical 3: implemention of max-heap sort algorithm .
Code:
public class HeapSort {
heapify(arr, i, 0);
}
}
if (largest != i) {
int swap = arr[i];
arr[i] = arr[largest];
arr[largest] = swap;
heapify(arr, n, largest);
}
}
1.Iterative
class Factorial
{
public static long factorial(int n)
{
long fact = 1;
return fact;
}
2.recursive
class Recursive {
static int factorial(int n)
{
int res = 1, i;
for (i = 2; i <= n; i++)
res *= i;
return res;
}
public static void main(String[] args)
{
int num = 6;
System.out.println("Munavvar Popatiya");
System.out.println(
"Factorial of " + num
+ " is " + factorial(5));
}
}
OUTPUT
Practical:5
Aim: Knapsack problem using dynamic programming
class Knapsack {
class CoinChange {
static long countWays(int coins[], int n, int sum)
{
Arrays.fill(table, 0);
table[0] = 1;
return table[sum];
}
class FractionalKnapSack {
private static double getMaxValue(ItemValue[] arr,
int capacity)
{
Arrays.sort(arr, new Comparator<ItemValue>() {
@Override
public int compare(ItemValue item1,
ItemValue item2)
{
double cpr1
= new Double((double)item1.value
/ (double)item1.weight);
double cpr2
= new Double((double)item2.value
/ (double)item2.weight);
return totalValue;
}
static class ItemValue {
// Function call
System.out.println(maxValue);
}
}
OUTPUT:
Practical 9: Implementation of graph and searching (DFS AND BFS)
Code for BFS :
import java.util.*;
class Graph<T> {
private Map<T, List<T> > map = new HashMap<>();
// This function adds a new vertex to the graph
public void addVertex(T s)
{
map.put(s, new LinkedList<T>());
}
}
builder.append("\n");
}
return (builder.toString());
}
}
class Graph1 {
public static void main(String args[])
{
System.out.println("Munavvar Popatiya");
OUTPUT:
DFS (Depth First Search) :
CODE:
import java.util.*;
class graphDfs {
public static final graphDfs G = new graphDfs(4);
private int V;
private LinkedList<Integer> adj[];
// Constructor
@SuppressWarnings("unchecked")
graphDfs(int v)
{
V = v;
adj = new LinkedList[v];
for (int i = 0; i < v; ++i)
adj[i] = new LinkedList();
}
void addEdge(int v, int w)
{
adj[v].add(w); // Add w to v's list.
}
void DFSUtil(int v, boolean visited[])
{
visited[v] = true;
System.out.print(v + " ");
Iterator<Integer> i = adj[v].listIterator();
while (i.hasNext()) {
int n = i.next();
if (!visited[n])
DFSUtil(n, visited);
}
}
void DFS(int v)
{
boolean visited[] = new boolean[V];
DFSUtil(v, visited);
}
public static void main(String args[])
{
System.out.println("Munavvar Popatiya");
graphDfs G = new graphDfs(4);
G.addEdge(0, 1);
G.addEdge(0, 2);
G.addEdge(1, 2);
G.addEdge(2, 0);
G.addEdge(2, 3);
G.addEdge(3, 3);
System.out.println(
"Following is Depth First Traversal "
+ "(starting from vertex 1)");
G.DFS(1);}}
OUTPUT
Practical 10: Implementation of prim’s algorithm
CODE:
import java.io.*;
import java.lang.*;
import java.util.*;
class MST {
// Number of vertices in the graph
private static final int V = 5;
int minKey(int key[], Boolean mstSet[])
{
int min = Integer.MAX_VALUE, min_index = -1;
for (int v = 0; v < V; v++)
if (mstSet[v] == false && key[v] < min) {
min = key[v];
min_index = v;
}
return min_index;
}
void printMST(int parent[], int graph[][])
{
System.out.println("Edge \tWeight");
for (int i = 1; i < V; i++)
System.out.println(parent[i] + " - " + i + "\t"
+ graph[i][parent[i]]);
}
void primMST(int graph[][])
{
printMST(parent, graph);
}
subsets[i].parent
= find(subsets, subsets[i].parent);
return subsets[i].parent;
}
void Union(subset subsets[], int x, int y)
{
int xroot = find(subsets, x);
int yroot = find(subsets, y);
if (subsets[xroot].rank < subsets[yroot].rank)
subsets[xroot].parent = yroot;
else if (subsets[xroot].rank > subsets[yroot].rank)
subsets[yroot].parent = xroot;
else {
subsets[yroot].parent = xroot;
subsets[xroot].rank++;
}
void KruskalMST()
{
Edge result[] = new Edge[V];
int e = 0;
// An index variable, used for sorted edges
int i = 0;
for (i = 0; i < V; ++i)
result[i] = new Edge();
Arrays.sort(edge);
// Allocate memory for creating V subsets
subset subsets[] = new subset[V];
for (i = 0; i < V; ++i)
subsets[i] = new subset();
// Create V subsets with single elements
class LCSExample1 {
public static String findLengthOfLCS(String str1, String
str2, int p, int q) {
int[][] tableForLCS = new int[p + 1][q + 1];
for (int i = 0; i <= p; i++) {
for (int j = 0; j <= q; j++) {
if (i == 0 || j == 0)
tableForLCS[i][j] = 0;
else if (str1.charAt(i - 1) == str2.charAt(j -
1))
tableForLCS[i][j] = tableForLCS[i - 1][j -
1] + 1;
else
tableForLCS[i][j] = Math.max(tableForLCS[i
- 1][j], tableForLCS[i][j - 1]); }
}
int index = tableForLCS[p][q];
int temp = index;
char[] longestCommonSubsequence = new char[index + 1];
longestCommonSubsequence[index] = '\0';
int i = p, j = q;
String lcs ="";
while (i > 0 && j > 0) {
if (str1.charAt(i - 1) == str2.charAt(j - 1)) {
longestCommonSubsequence[index - 1] =
str1.charAt(i - 1);
i--;
j--;
index--;
}
else if (tableForLCS[i - 1][j] > tableForLCS[i][j
- 1])
i--;
else
j--;
}
return lcs;
}
public static void main(String[] args) {
String str1, str2, LCS;
str1 = "KLMNO";
str2 = "LMNOP";
int p = str1.length();
int q = str2.length();
LCS = findLengthOfLCS(str1, str2, p, q);
System.out.print("Sequence1: " + str1 + "\nSequence2:
" + str2);
System.out.println("\nLCS: "+LCS);
}
}
OUTPUT: