Basic Algorithm Assignment
Basic Algorithm Assignment
Basic Algorithm Assignment
Submitted to:
Department: CSE
Submitted by:
Name: Ashraful Goni
Roll: 36004
Level: 2-2
Department: CSE
Date: 03-Nov-2022
Answer to Question number 1:
ara[j] = ara[j+1];
ara[j+1] = temp;
System.out.println();
int[] ara = {120, 12, 18, 201, 291, 0, 1, 23, 11, 2};
printArray(ara);
bubbleSort(ara, ara.length);
printArray(ara);
arr[i] = arr[j];
arr[j] = temp;
}
i++;
swap(arr, i, j);
}
swap(arr, i + 1, high);
return (i + 1);
quickSort(arr, pi + 1, high);
System.out.println();
int[] ara = {120, 12, 18, 201, 291, 0, 1, 23, 11, 2};
printArray(ara);
quickSort(ara, 0,ara.length-1);
printArray(ara);
int n1 = m - l + 1;
int n2 = r - m;
int i = 0, j = 0;
int k = l;
i++;
else {
arr[k] = R[j];
j++;
k++;
arr[k] = L[i];
i++;
k++;
arr[k] = R[j];
j++;
k++;
if (l < r) {
int m = l + (r - l) / 2;
mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);
merge(arr, l, m, r);
System.out.println();
int[] ara = {120, 12, 18, 201, 291, 0, 1, 23, 11, 2};
printArray(ara);
mergeSort(ara, 0,ara.length-1);
printArray(ara);
Stack:
Application of Stack Data Structure (Geeksforgeeks,
Applications, Advantages and Disadvantages of Stack, 2022):
Queue:
Stack:
Stack()
top = -1;
System.out.println("Stack Overflow");
return false;
}
else {
a[++top] = x;
return true;
if (top < 0) {
System.out.println("Stack Underflow");
return 0;
else {
int x = a[top--];
return x;
}
if (top < 0) {
System.out.println("Stack Underflow");
return 0;
else {
int x = a[top];
return x;
s.push(10);
s.push(20);
s.push(30);
s.print();
}
Queue:
Queue(int c)
front = rear = 0;
capacity = c;
if (capacity == rear) {
System.out.printf("\nQueue is full\n");
return;
else {
queue[rear] = data;
rear++;
return;
if (front == rear) {
System.out.printf("\nQueue is empty\n");
return;
}
else {
queue[rear] = 0;
rear--;
return;
{
int i;
if (front == rear) {
System.out.printf("\nQueue is Empty\n");
return;
return;
if (front == rear) {
System.out.printf("\nQueue is Empty\n");
return;
queue[front]);
return;
q.queueDisplay();
q.queueEnqueue(20);
q.queueEnqueue(30);
q.queueEnqueue(40);
q.queueEnqueue(50);
q.queueDisplay();
q.queueEnqueue(60);
q.queueDisplay();
q.queueDequeue();
q.queueDequeue();
System.out.printf(
q.queueDisplay();
q.queueFront();
}
}
int key;
key = item;
if (root == null) {
return root;
return root;
if (root == null)
return root;
else {
if (root.left == null)
return root.right;
else if (root.right == null)
return root.left;
root.key = minValue(root.right);
return root;
minv = root.left.key;
root = root.left;
return minv;
}
return root;
if (root != null) {
inorderRec(root.left);
inorderRec(root.right);
}
}
bst.inorderRec(root);
System.out.println();
System.out.println("\n\nAfter deleting 10");
bst.deleteRec(root,10);
bst.inorderRec(root);
System.out.println();
if(searhItm != null){
System.out.println("Item is found");
else {
class Edge {
Edge[] edge;
Graph(int vertices, int edges) {
this.vertices = vertices;
this.edges = edges;
int noVertices = 5;
int noEdges = 8;
g.edge[0].dest = 2;
g.edge[1].src = 1;
g.edge[1].dest = 3;
g.edge[2].src = 1;
g.edge[2].dest = 4;
g.edge[3].src = 2;
g.edge[3].dest = 4;
g.edge[4].src = 2;
g.edge[4].dest = 5;
g.edge[5].src = 3;
g.edge[5].dest = 4;
g.edge[6].src = 3;
g.edge[6].dest = 5;
g.edge[7].src = 4;
g.edge[7].dest = 5;
// print graph
import java.util.*;
private int V;
public Dijkstra(int V)
this.V = V;
this.adj = adj;
dist[i] = Integer.MAX_VALUE;
dist[src] = 0;
while (settled.size() != V) {
if (pq.isEmpty())
return;
int u = pq.remove().node;
if (settled.contains(u))
continue;
settled.add(u);
e_Neighbours(u);
Node v = adj.get(u).get(i);
if (!settled.contains(v.node)) {
edgeDistance = v.cost;
dist[v.node] = newDistance;
int V = 5;
int source = 0;
List<List<Node> > adj
adj.add(item);
+ dpq.dist[i]);
public Node() {}
this.node = node;
this.cost = cost;
return -1;
return 1;
return 0;
}
(iii) Prim’s Algorithm Implementation:
min = key[v];
min_index = v;
return min_index;
System.out.println("Edge \tWeight");
+ graph[i][parent[i]]);
key[i] = Integer.MAX_VALUE;
mstSet[i] = false;
}
key[0] = 0;
parent[0] = -1;
mstSet[u] = true;
parent[v] = u;
key[v] = graph[u][v];
}
printMST(parent, graph);
{ 2, 0, 3, 8, 5 },
{ 0, 3, 0, 0, 7 },
{ 6, 8, 0, 0, 9 },
{ 0, 5, 7, 9, 0 } };
p.primMST(graph);
};
class subset {
};
int V, E;
Edge edge[];
Kruskal(int v, int e)
V = v;
E = e;
if (subsets[i].parent != i)
subsets[i].parent
= find(subsets, subsets[i].parent);
return subsets[i].parent;
}
void Union(subset subsets[], int x, int y)
subsets[xroot].parent = yroot;
subsets[yroot].parent = xroot;
else {
subsets[yroot].parent = xroot;
subsets[xroot].rank++;
void KruskalMST()
{
Edge result[] = new Edge[V];
int e = 0;
int i = 0;
Arrays.sort(edge);
subsets[v].parent = v;
subsets[v].rank = 0;
}
i = 0;
while (e < V - 1) {
if (x != y) {
result[e++] = next_edge;
Union(subsets, x, y);
int minimumCost = 0;
for (i = 0; i < e; ++i) {
+ result[i].dest
minimumCost += result[i].weight;
+ minimumCost);
int V = 4;
int E = 5;
graph.edge[0].src = 0;
graph.edge[0].dest = 1;
graph.edge[0].weight = 10;
graph.edge[1].src = 0;
graph.edge[1].dest = 2;
graph.edge[1].weight = 6;
graph.edge[2].src = 0;
graph.edge[2].dest = 3;
graph.edge[2].weight = 5;
graph.edge[3].src = 1;
graph.edge[3].dest = 3;
graph.edge[3].weight = 15;
graph.edge[4].src = 2;
graph.edge[4].dest = 3;
graph.edge[4].weight = 4;
graph.KruskalMST();
END