num = []
print("Enter 3 numbers:")
for i in range(3):
n = int(input())
num.append(n)
for i in range(3):
print(num[i])
def sjf_scheduling(n, burst_times):
processes = list(range(1, n + 1))
# Sort processes by burst time using Selection Sort
for i in range(n):
pos = i
for j in range(i + 1, n):
if burst_times[j] < burst_times[pos]:
pos = j
burst_times[i], burst_times[pos] = burst_times[pos], burst_times[i]
processes[i], processes[pos] = processes[pos], processes[i]
waiting_time = [0] * n
total_wt = 0
# Calculate waiting time
for i in range(1, n):
waiting_time[i] = sum(burst_times[:i])
total_wt += waiting_time[i]
avg_wt = total_wt / n
# Calculate turnaround time
turnaround_time = [burst_times[i] + waiting_time[i] for i in range(n)]
total_tat = sum(turnaround_time)
avg_tat = total_tat / n
# Print results
print("\nProcess\t Burst Time\t Waiting Time\t Turnaround Time")
for i in range(n):
print(f"P[{processes[i]}]\t {burst_times[i]}\t\t {waiting_time[i]}\t\t {turnaround_time[i]}")
print(f"\nAverage Waiting Time: {avg_wt:.2f}")
print(f"Average Turnaround Time: {avg_tat:.2f}")
if __name__ == "__main__":
n = int(input("Enter number of processes: "))
burst_times = []
print("Enter burst times:")
for i in range(n):
bt = int(input(f" P[{i+1}]: "))
burst_times.append(bt)
sjf_scheduling(n, burst_times)
n = int(input("Enter Total No. of Processes: "))
bt = []
wt = [0] * n
tat = [0] * n
print("Enter Process Burst Time:")
for i in range(n):
bt.append(int(input(f"P[{i + 1}]: ")))
wt[0] = 0
for i in range(1, n):
wt[i] = 0
for j in range(i):
wt[i] += bt[j]
awt = 0
avtat = 0
print("\nProcess\tBurst Time\tWaiting Time\tTurnaround Time")
for i in range(n):
tat[i] = bt[i] + wt[i]
awt += wt[i]
avtat += tat[i]
print(f"P[{i + 1}]\t\t{bt[i]}\t\t{wt[i]}\t\t{tat[i]}")
awt /= n
avtat /= n
print(f"\nAverage Waiting Time: {awt:.2f}")
print(f"Average Turnaround Time: {avtat:.2f}")
public class linkedlistqueue {
public static void main(String[] args) {
Queue queue = new Queue();
queue.enqueue(10);
queue.enqueue(20);
queue.enqueue(30);
System.out.println("Dequeued:" + queue.dequeue());
System.out.println("Dequeued:" + queue.dequeue());
System.out.println("peek:" + queue.peek());
System.out.println("Dequeued:" + queue.dequeue());
}
}
class Node {
int data;
Node next;
public Node(int data) {
this.data = data;
this.next = null;
}
}
class Queue {
private Node front;
private Node rear;
public Queue() {
this.front = null;
this.rear = null;
}
public void enqueue(int data) {
Node newNode = new Node(data);
if (rear == null) {
front = newNode;
rear = newNode;
} else {
rear.next = newNode;
rear = newNode;
}
}
public int dequeue() {
if (front == null) {
throw new IllegalStateException("Queue is empty");
}
int data = front.data;
front = front.next;
if (front == null) {
rear = null;
}
return data;
}
public boolean isEmpty() {
return front == null;
}
public int peek() {
if (front == null) {
throw new IllegalStateException("Queue is empty");
}
return front.data;
}
}
import java.util.Stack;
class GfG {
int prec(char c) {
if (c == '^')
return 3;
else if (c == '/' || c == '*')
return 2;
else if (c == '+' || c == '-')
return 1;
else
return -1;
}
void infixToPostfix(String s) {
Stack<Character> st = new Stack<>();
StringBuilder result = new StringBuilder();
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9')) {
result.append(c);
} else if (c == '(') {
st.push('(');
} else if (c == ')') {
while (st.peek() != '(') {
result.append(st.pop());
}
st.pop();
} else {
while (!st.isEmpty() && (prec(c) < prec(st.peek()) || prec(c) == prec(st.peek()))) {
result.append(st.pop());
}
st.push(c);
}
}
while (!st.isEmpty()) {
result.append(st.pop());
}
System.out.println("PostFix expression: " + result.toString());
}
public static void main(String[] args) {
GfG gfg = new GfG();
String exp = "L+M+N+O";
System.out.println("Infix Expression: " + exp);
gfg.infixToPostfix(exp);
}
}
public class BinaryTreeTraversal {
public static void main(String[] args) {
BinaryTree tree = new BinaryTree();
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);
System.out.println("Inorder traversal: ");
tree.inOrder(tree.root);
System.out.println("\nPreorder traversal: ");
tree.preOrder(tree.root);
System.out.println("\nPostorder traversal:");
tree.postOrder(tree.root);
}
}
class Node {
int data;
Node left;
Node right;
public Node(int data) {
this.data = data;
this.left = null;
this.right = null;
}
}
class BinaryTree {
Node root;
public BinaryTree() {
this.root = null;
}
public void inOrder(Node node) {
if (node != null) {
inOrder(node.left);
System.out.print(node.data + " ");
inOrder(node.right);
}
}
public void preOrder(Node node) {
if (node != null) {
System.out.print(node.data + " ");
preOrder(node.left);
preOrder(node.right);
}
}
public void postOrder(Node node) {
if (node != null) {
postOrder(node.left);
postOrder(node.right);
System.out.print(node.data + " ");
}
}
}
public class LinearSearch1 {
public static int linearSearch(int[] arr, int key) {
for (int i = 0; i < arr.length; i++) {
if (arr[i] == key) {
return i;
}
}
return -1;
}
public static void main(String[] args) {
int[] arr = {10, 20, 30, 40, 50};
int key = 30;
int result = linearSearch(arr, key);
if (result == -1) {
System.out.println("Element not found in the array:");
} else {
System.out.println("Element found at index :" + result);
}
}
}
public class BinarySearch {
public static int binarySearch(int[] arr, int target) {
int low = 0;
int high = arr.length - 1;
while (low <= high) {
int mid = low + (high - low) / 2;
if (arr[mid] == target) {
return mid;
} else if (arr[mid] < target) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return -1;
}
public static void main(String[] args) {
int[] array = {10, 20, 30, 40, 50, 60, 70, 80};
int target = 40;
int result = binarySearch(array, target);
if (result != -1) {
System.out.println("Element found at index: " + result);
} else {
System.out.println("Element not found in the array.");
}
}
}
import java.util.Arrays;
public class Main {
public static void insertionSort(int[] arr) {
for (int i = 1; i < arr.length; i++) {
int key = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = key;
}
}
public static void main(String[] args) {
int[] arr = {5, 3, 8, 6, 2};
System.out.println("Before Sorting: " + Arrays.toString(arr));
insertionSort(arr);
System.out.println("After Sorting: " + Arrays.toString(arr));
}
}
import java.util.Arrays;
public class Main {
public static void quickSort(int[] arr, int low, int high) {
if (low >= high) return;
int pivot = arr[high], i = low;
for (int j = low; j < high; j++) {
if (arr[j] < pivot) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i++;
}
}
arr[high] = arr[i];
arr[i] = pivot;
quickSort(arr, low, i - 1);
quickSort(arr, i + 1, high);
}
public static void main(String[] args) {
int[] arr = {5, 3, 8, 6, 2};
System.out.println("Before Sorting: " + Arrays.toString(arr));
quickSort(arr, 0, arr.length - 1);
System.out.println("After Sorting: " + Arrays.toString(arr));
}
}
import java.util.Arrays;
public class Main {
public static void mergeSort(int[] arr) {
if (arr.length < 2) return;
int mid = arr.length / 2;
int[] left = Arrays.copyOfRange(arr, 0, mid);
int[] right = Arrays.copyOfRange(arr, mid, arr.length);
mergeSort(left);
mergeSort(right);
merge(arr, left, right);
}
private static void merge(int[] arr, int[] left, int[] right) {
int i = 0, j = 0, k = 0;
while (i < left.length && j < right.length)
arr[k++] = (left[i] < right[j]) ? left[i++] : right[j++];
while (i < left.length) arr[k++] = left[i++];
while (j < right.length) arr[k++] = right[j++];
}
public static void main(String[] args) {
int[] arr = {15, 3, 28, 6, 12};
System.out.println("Before Sorting: " + Arrays.toString(arr));
mergeSort(arr);
System.out.println("After Sorting: " + Arrays.toString(arr));
}
}
import java.util.*;
class GraphIterative {
private int V;
private LinkedList<Integer>[] adj;
@SuppressWarnings("unchecked")
public GraphIterative(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);
}
void DFS(int start) {
boolean[] visited = new boolean[V];
Stack<Integer> stack = new Stack<>();
stack.push(start);
while (!stack.isEmpty()) {
int v = stack.pop();
if (!visited[v]) {
System.out.print(v + " ");
visited[v] = true;
}
for (int neighbor : adj[v]) {
if (!visited[neighbor]) {
stack.push(neighbor);
}
}
}
}
public static void main(String[] args) {
GraphIterative g = new GraphIterative(5);
g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(1, 3);
g.addEdge(1, 4);
System.out.println("Depth-First Search using Stack:");
g.DFS(0);
}
}
import java.util.*;
class Graph {
private int V;
private LinkedList<Integer>[] adj;
@SuppressWarnings("unchecked")
public Graph(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);
}
void BFS(int start) {
boolean[] visited = new boolean[V];
Queue<Integer> queue = new LinkedList<>();
visited[start] = true;
queue.add(start);
System.out.print("BFS Traversal: ");
while (!queue.isEmpty()) {
int v = queue.poll();
System.out.print(v + " ");
for (int neighbor : adj[v]) {
if (!visited[neighbor]) {
visited[neighbor] = true;
queue.add(neighbor);
}
}
}
System.out.println();
}
public static void main(String[] args) {
Graph g = new Graph(6);
g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(1, 3);
g.addEdge(1, 4);
g.addEdge(2, 5);
g.BFS(0);
}
}
import java.util.*;
class Graph {
private int V;
private LinkedList<Edge>[] adj;
static class Edge {
int dest, weight;
Edge(int dest, int weight) {
this.dest = dest;
this.weight = weight;
}
}
@SuppressWarnings("unchecked")
public Graph(int v) {
V = v;
adj = new LinkedList[v];
for (int i = 0; i < v; i++) {
adj[i] = new LinkedList<>();
}
}
void addEdge(int src, int dest, int weight) {
adj[src].add(new Edge(dest, weight));
adj[dest].add(new Edge(src, weight));
}
void dijkstra(int src) {
int[] dist = new int[V];
Arrays.fill(dist, Integer.MAX_VALUE);
dist[src] = 0;
PriorityQueue<Edge> pq = new PriorityQueue<>(Comparator.comparingInt(e -> e.weight));
pq.add(new Edge(src, 0));
while (!pq.isEmpty()) {
Edge current = pq.poll();
int u = current.dest;
for (Edge neighbor : adj[u]) {
int v = neighbor.dest;
int weight = neighbor.weight;
if (dist[u] != Integer.MAX_VALUE && dist[u] + weight < dist[v]) {
dist[v] = dist[u] + weight;
pq.add(new Edge(v, dist[v]));
}
}
}
System.out.println("Vertex \t Distance from Source " + src);
for (int i = 0; i < V; i++) {
System.out.println(i + "\t" + dist[i]);
}
}
public static void main(String[] args) {
Graph g = new Graph(6);
g.addEdge(0, 1, 4);
g.addEdge(0, 2, 2);
g.addEdge(1, 2, 5);
g.addEdge(1, 3, 10);
g.addEdge(2, 4, 3);
g.addEdge(3, 5, 6);
g.addEdge(4, 5, 8);
g.dijkstra(0);
}
}