0% found this document useful (0 votes)
9 views8 pages

Mohit Ee

Uploaded by

Mohit Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views8 pages

Mohit Ee

Uploaded by

Mohit Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

G.L.

BAJAJ INSTITUTE OF TECHNOLOGY & MANAGEMENT, GREATER


NOIDA

Advanced Algorithm Lab


(MTCS-152)

EXPERIMENT FILE
ACADEMIC SESSION 2023-24

COURSE : M.TECH (CSE)


SEM: Ist

Submitted by: Submitted to:


Mohit Singh Ms.Sheenam Naaz

Dept. of Computer Science & Engineering

G. L. BAJAJ INSTITUTE OF TECHNOLOGY AND MANAGEMENT


Plot no. 2, Knowledge Park III, Gr. Noida
List of Experiments

S. NO. EXPERIMENT LIST DATE OF DATE OF SIGNATURE


PRACTICAL SUBMISSION

1 Write Java programs that use both recursive and non-


recursive functions for implementing the following
searching methods: a) Linear search b) Binary search
2 Write a Java program to implement all the functions of a
dictionary (ADT) using Hashing.

3 Write a Java program to implement Dijkstra's algorithm


for Single source shortest path problem.

.
4
Write Java programs that use recursive and non-recursive
functions to traverse the given binary tree in a) Preorder b)
Inorder c) Postorder.

Insertion sort c) Quick sort d) Merge sort e) Heap sort f)


Radix sort g) Binary tree sort
5 Write Java programs for the implementation of bfs and dfs
for a given graph

6 Write Java programs for implementing the following


sorting methods: a) Bubble sort b)

7 Write a Java program to perform the following operations:


a) Insertion into a B-tree b) Searching in a B-tree

8 Write a Java program that implements Kruskal's algorithm


to generate minimum cost spanning tree. 9. Write a Java
program that implements KMP algorithm for
pattern matching
9 Write a Java program that implements KMP algorithm for
pattern matching.
Experiment-01
Aim- Write Java programs that use both recursive and non-recursive functions for
implementing the following searching methods: a) Linear search b) Binary search

Source Code: Linear Search (Non-Recursive):

public class LinearSearchNonRecursive { public static int linearSearch (int[] arr, int target) { for (int i = 0; i
< arr.length; i+

+) {

if (arr[i] == target) { return i; // Return the index of the target element }

return -1; // Return -1 if the

target element is not found

public static void main(String[] args)

int[] arr = {1, 2, 3, 4, 5}; int target = 3;

int result = linear Search(arr,

target);

if (result != -1) { System.out.println("Element found at index " + result);

} else { System.out.println("Element not found in the array."); }


}

Linear Search (Recursive):

public class Linear SearchRecursive { public static int linearSearch(int[] arr, int target, int index) { if
(index> arr.length) { return -1; // Element not found

if (arr[index] target) { return index; // Element found at the current index

return linear Search (arr, target, index + 1); // Recursively search the next

index }

} } { target, 0); if (result != -1) {


public static void main(String[] args).

int[] arr (1, 2, 3, 4, 5); int target 3; int result = linear Search (arr,

System.out.println("Element found at index " + result);

} else { System.out.println("Element not found in the array."); }

Linear Search (Recursive):

public class Linear SearchRecursive { public static int linearSearch(int[] arr, int target, int index) { if
(index> arr.length) { return -1; // Element not found

if (arr[index] target) { return index; // Element found at the current index

return linear Search (arr, target, index + 1); // Recursively search the next

index }

} } { target, 0); if (result != -1) {

public static void main(String[] args).

int[] arr (1, 2, 3, 4, 5); int target 3; int result = linear Search (arr,

System.out.println("Element found at index " + result);

} else { System.out.println("Element not found in the array."); }

Binary Search (Recursive):

public class BinarySearchRecursive { public static int binarySearch(int[] arr, int target, int left, int right) {
if (left <= right) { int mid left + (right)

left) / 2;

if (arr[mid] == target) { return mid; // Element found at the middle

if (arr[mid] < target) {


return binarySearch(arr,

target, mid 1, right); // Search the

right half

} else { return binarySearch(arr, target, left, mid 1); // Search the left

half

} return -1; // Element not found

public static void main(String[] args)

int[] arr = {1, 2, 3, 4, 5, 6, 7,

int target = 5;

int result = binarySearch(arr,

8, 9};

0, arr.length-1); if (result = -1) {

target, System.out.println("Element found at index " + result);

} else { System.out.println("Element not found in the array."); }

}
}
EXPERIMENT-02
Aim- Write a Java program to implement all the functions of a dictionary (ADT) using
Hashing.
Source Code:

java
import java.util.LinkedList;

class Dictionary {
private int capacity; // The capacity of the hash table
private LinkedList<Entry>[] table; // Array of linked lists for collision handling

// Entry class to store key-value pairs


private static class Entry {
String key;
String value;

Entry(String key, String value) {


this.key = key;
this.value = value;
}
}

public Dictionary(int capacity) {


this.capacity = capacity;
table = new LinkedList[capacity];
for (int i = 0; i < capacity; i++) {
table[i] = new LinkedList<>();
}
}

// Hash function to calculate the index for a key


private int hash(String key) {
int hashCode = key.hashCode();
return Math.abs(hashCode % capacity);
}

// Insert a key-value pair into the dictionary


public void insert(String key, String value) {
int index = hash(key);
for (Entry entry : table[index]) {
if (entry.key.equals(key)) {
// Update the value if the key already exists
entry.value = value;
return;
}
}
// Insert a new entry if the key doesn't exist
table[index].add(new Entry(key, value));
}

// Get the value associated with a key


public String get(String key) {
int index = hash(key);
for (Entry entry : table[index]) {
if (entry.key.equals(key)) {
return entry.value;
}
}
return null; // Key not found
}

// Remove a key-value pair from the dictionary


public void remove(String key) {
int index = hash(key);
table[index].removeIf(entry -> entry.key.equals(key));
}
}

public class DictionaryDemo {


public static void main(String[] args) {
Dictionary dictionary = new Dictionary(10);

// Insert key-value pairs


dictionary.insert("apple", "a fruit");
dictionary.insert("banana", "another fruit");
dictionary.insert("car", "a vehicle");

// Retrieve values
System.out.println("Meaning of 'apple': " + dictionary.get("apple"));
System.out.println("Meaning of 'banana': " + dictionary.get("banana"));

// Update value
dictionary.insert("apple", "a tech company");
System.out.println("Updated meaning of 'apple': " + dictionary.get("apple"));
// Remove a key-value pair
dictionary.remove("banana");
System.out.println("Meaning of 'banana' after removal: " + dictionary.get("banana"));
}
}

You might also like