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

Daa_Assignment 3

The document contains multiple programming examples demonstrating various algorithms, including Merge Sort for counting inversion pairs, Job Sequencing with deadlines, finding the second largest and smallest numbers, counting occurrences in a sorted array, and implementing shortest path algorithms like Floyd-Warshall, Prim's, and Kruskal's. Each section includes source code in C or Java, along with explanations of the algorithms' functionality. The examples cover a range of data structures and algorithms, showcasing practical applications in programming.

Uploaded by

patarisuvrajit01
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)
7 views8 pages

Daa_Assignment 3

The document contains multiple programming examples demonstrating various algorithms, including Merge Sort for counting inversion pairs, Job Sequencing with deadlines, finding the second largest and smallest numbers, counting occurrences in a sorted array, and implementing shortest path algorithms like Floyd-Warshall, Prim's, and Kruskal's. Each section includes source code in C or Java, along with explanations of the algorithms' functionality. The examples cover a range of data structures and algorithms, showcasing practical applications in programming.

Uploaded by

patarisuvrajit01
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

Title: Apply Merge Sort to count inversion pairs in an array.

Two elements a[i] and a[j] form


an inversion pair if a[i] > a[j] and i < j.

Source Code:
#include <stdio.h> count += mergeSort(a,low,mid);
int merge(int* a,int left,int mid,int right){ count += mergeSort(a,mid+1,high);
int b[100];int k=left; count += merge(a,low,mid,high);
int i=left,j=mid+1; return count;
int count=0; }
while(i<=mid && j<=right){ return count;
if(a[i]<=a[j]){ }
b[k++]=a[i++]; int main(){
} int n;
else{ printf("Enter number of terms: ");
b[k++]=a[j++]; scanf("%d",&n);
count+=mid-i+1; int a[n];
} printf("Enter the array elements: ");
} for (int i = 0; i < n; i++){
while(i<=mid){ scanf("%d",&a[i]);
b[k++]=a[i]; }
i++; printf("The input array is: [");
} for (int i = 0; i < n; i++){
while(j<=right){ printf(" %d ",a[i]);
b[k++]=a[j]; }
j++; printf("]\n");
} int c = mergeSort(a,0,n-1);
for (int idx= left; idx <= right; idx++) { printf("The sorted array is: [");
a[idx] = b[idx]; for (int i = 0; i < n; i++){
} printf(" %d ",a[i]);
return count; }
} printf("]\n");
int mergeSort(int* a,int low,int high){ printf("The number of inversions are: %d",c);
int count=0; return 0;
if(low<high){ }
int mid = (high+low)/2;
Output:
Title: Implement the greedy algorithm to solve the problem of the Job
Sequencing with deadlines.

Source Code:
import java.util.Arrays; System.out.print("Enter number of
import java.util.Scanner; jobs: ");
int n = s.nextInt();
class JobSequencing { System.out.println("\nEnter job
details in the format <Profit>
public static void swap(int[] arr, int i, <Deadline>: ");
int j) {
int temp = arr[i]; int[] jobs = new int[n];
arr[i] = arr[j]; int[] profit = new int[n];
arr[j] = temp; int[] deadline = new int[n];
} int maxDeadline =
Integer.MIN_VALUE;
public static void bubbleSort(int[] arr,
int[]... comp) { for (int i = 0; i < n; i++) {
boolean swapped; System.out.print("Job " + (i + 1) +
for (int i = 0; i < arr.length - 1; i++) ": ");
{ jobs[i] = i + 1;
swapped = false; profit[i] = s.nextInt();
for (int j = 1; j < arr.length - i; deadline[i] = s.nextInt();
j++) { maxDeadline =
if (arr[j] > arr[j - 1]) { Math.max(maxDeadline, deadline[i]); //
swap(arr, j, j - 1); Track max deadline
for (int[] k : comp) { }
swap(k, j, j - 1);
} int[] slots = new int[maxDeadline];
swapped = true; Arrays.fill(slots, -1); // Initialize
} slots to -1
}
if (!swapped) break; // Exit early // Sort jobs by profit (descending
if already sorted order)
} bubbleSort(profit, jobs, deadline);
}
int totalProfit = 0;
public static void main(String[] args) { boolean[] selectedJobs = new
Scanner s = new boolean[n];
Scanner(System.in);
for (int i = 0; i < n; i++) { System.out.println("Selected jobs in
for (int j = sequence:");
Math.min(maxDeadline, deadline[i]) - 1; for (int job : slots) {
j >= 0; j--) { if (job != -1) System.out.print(job
if (slots[j] == -1) { // Find an + " ");
empty slot }
slots[j] = jobs[i];
totalProfit += profit[i]; System.out.println("\n\nDiscarded
selectedJobs[i] = true; Jobs:");
break; for (int i = 0; i < n; i++) {
} if (!selectedJobs[i])
} System.out.print(jobs[i] + " ");
} }

System.out.println("\nTotal profit: " s.close();


+ totalProfit); }
}

Output:
Title: Find the second largest and second smallest number simultaneously in an array
using Divide & Conquer Principle.

Source Code:
import java.util.Scanner; res[1] = left[0] == res[0] ?
public class SecondMinMax { Math.min(left[1], right[0]) : Math.min(left[0],
public static int[] findSecondMinMax(int[] right[1]);
arr, int low, int high) { res[3] = left[2] == res[2] ?
int[] res = new int[4]; Math.max(left[3], right[2]) : Math.max(left[2],
if (low == high) { right[3]);
res[0] = res[2] = arr[low]; return res;
res[1] = res[3] = Integer.MAX_VALUE; }
return res; public static void main(String[] args) {
} Scanner s = new Scanner(System.in);
if (high == low + 1) { System.out.print("Enter count: ");
if (arr[low] < arr[high]) { int n = s.nextInt();
res[0] = arr[low];res[2] = arr[high]; int[] arr = new int[n];
res[1] = arr[high];res[3] = arr[low]; System.out.println("Enter numbers:");
} else { for (int i = 0; i < n; i++) arr[i] = s.nextInt();
res[0] = arr[high];res[1] = arr[low]; if (n < 2) {
res[2] = arr[low];res[3] = arr[high]; System.out.println("At least two numbers
} needed!");
return res; return;
} }
int mid = (low + high) / 2; int[] res = findSecondMinMax(arr, 0, n - 1);
int[] left = findSecondMinMax(arr, low, System.out.println("Second Smallest: " +
mid); res[1]);
int[] right = findSecondMinMax(arr, mid + System.out.println("Second Largest: " +
1, high); res[3]);
res[0] = Math.min(left[0], right[0]); }
res[2] = Math.max(left[2], right[2]); }

Output:
Title: Given a sorted array and a number x, write a function that counts the occurrences
of x in the array.

Source Code:
import java.util.Scanner; low = mid + 1;
public class CountOccurrences { } else {
public static int firstOccurrence(int[] arr, int x) high = mid - 1;
{ }
int low = 0, high = arr.length - 1, result = - }
1; return result;
while (low <= high) { }
int mid = low + (high - low) / 2; public static int countOccurrences(int[] arr, int
if (arr[mid] == x) { x) {
result = mid; int first = firstOccurrence(arr, x);
high = mid - 1; if (first == -1) return 0;
} else if (arr[mid] < x) { int last = lastOccurrence(arr, x);
low = mid + 1; return last - first + 1;
} else { }
high = mid - 1; public static void main(String[] args) {
} Scanner s = new Scanner(System.in);
} System.out.print("Enter array size: ");
return result; int n = s.nextInt();
} int[] arr = new int[n];
public static int lastOccurrence(int[] arr, int x) System.out.println("Enter sorted array:");
{ for (int i = 0; i < n; i++) arr[i] = s.nextInt();
int low = 0, high = arr.length - 1, result = - System.out.print("Enter number to count:
1; ");
while (low <= high) { int x = s.nextInt();
int mid = low + (high - low) / 2; System.out.println("Occurrences of " + x +
if (arr[mid] == x) { ": " + countOccurrences(arr, x));
result = mid; }
low = mid + 1; }
} else if (arr[mid] < x) {

Output:
Title: Implement all pairs of the shortest path algorithms for a graph using
FloydWarshall’s strategy.

Source Code:
#include <stdio.h> {
int main() for(int i = 0; i<V; i++)
{ {
int V; for(int j = 0; j<V; j++)
printf("Enter no. of vertices: "); {
scanf("%d",&V); if(A1[i][k]+A1[k][j] < A1[i][j])
int A0[V][V], A1[V][V]; A1[i][j] = A1[i][k]+A1[k][j];
printf("Enter the edge weights in }
matrix form:\n"); }
for(int i = 0; i<V; i++) }
{ printf("Shortest distance matrix:\n");
for(int j = 0; j<V; j++) for(int i = 0; i<V; i++)
scanf("%d",&A0[i][j]); {
} for(int j = 0; j<V; j++)
for(int i = 0; i<V; i++) printf("%d ",A1[i][j]);
{ printf("\n");
for(int j = 0; j<V; j++) }
A1[i][j] = A0[i][j]; return 0;
} }
for(int k = 0; k<V; k++)

Output:
Title: Implement Prim’s algorithm and Kruskal’s Algorithm using greedy approach.
Source Code: (Prim’s)
import java.util.*; }
class PrimMST { }
private static int selectMinVertex(int[] value, }
boolean[] setMST, int V) { for (int i = 1; i < V; i++)
int minimum = Integer.MAX_VALUE, vertex = System.out.println("U->V: " + parent[i] + "-
-1; >" + i + " wt = " + graph[parent[i]][i]);
for (int i = 0; i < V; i++) { }
if (!setMST[i] && value[i] < minimum) { public static void main(String[] args) {
vertex = i; Scanner sc = new Scanner(System.in);
minimum = value[i]; System.out.print("Enter number of vertices: ");
} int V = sc.nextInt();
} int[][] graph = new int[V][V];
return vertex; System.out.println("Enter adjacency matrix of
} the graph -->");
private static void findMST(int[][] graph, int V) { for(int i=0; i<V; i++){
int[] parent = new int[V]; for(int j=0; j<V; j++){
int[] value = new int[V]; graph[i][j] = sc.nextInt();
boolean[] setMST = new boolean[V]; }
Arrays.fill(value, Integer.MAX_VALUE); }
parent[0] = -1; System.out.println("The adjacency Matrix ->");
value[0] = 0; for(int i=0; i<V; i++){
for (int i = 0; i < V - 1; i++) { for(int j=0; j<V; j++){
int U = selectMinVertex(value, setMST, V); System.out.print(graph[i][j] + " ");
setMST[U] = true; }
for (int j = 0; j < V; j++) { System.out.println();
if (graph[U][j] != 0 && !setMST[j] && }
graph[U][j] < value[j]) { findMST(graph, V);
value[j] = graph[U][j]; }
parent[j] = U; }

Output:
Source Code: (Kruskal’s)
import java.util.*; }
class KruskalMST { }
static int V, E; }
static int[] parent = new int[100]; if (a == -1 || b == -1) break;
static int[][] cost = new int[1000][10000]; unionOp(a, b);
System.out.println("Edge " + edgeCount++ +
private static int find(int i) { ":(" + a + " " + b + ") cost: " + min);
while (parent[i] != i) mincost += min;
i = parent[i]; }
return i; System.out.println("\nMinimum cost= " +
} mincost);
private static void unionOp(int i, int j) { }
int a = find(i); public static void main(String[] args) {
int b = find(j); Scanner sc = new Scanner(System.in);
parent[a] = b; V = sc.nextInt();
} E = sc.nextInt();
private static void kruskalMST() { for (int i = 0; i < V; i++)
int mincost = 0; Arrays.fill(cost[i], Integer.MAX_VALUE);
int edgeCount = 0;
while (edgeCount < V - 1) { for (int j = 0; j < E; j++) {
int min = Integer.MAX_VALUE, a = -1, b = - int m = sc.nextInt(), n = sc.nextInt();
1; cost[m][n] = sc.nextInt();
for (int i = 0; i < V; i++) { }
for (int j = 0; j < V; j++) { for (int i = 0; i < V; i++)
if (find(i) != find(j) && cost[i][j] < min) parent[i] = i;
{ kruskalMST();
min = cost[i][j]; }
a = i; }
b = j;

Output:

You might also like