Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

AIM: Write A Program in Java To Analyze Time Complexity of Recursive

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 29

AIM: Write a program in java to analyze time complexity of recursive

Binary Search.

CODE:

import java.util.*;
public class Binarysearch2 {
// Elements in the array are assumed to be in non-decreasing order.
// Count is a global variable which is used to count the no.of element
comparisons.
static int count=0;
public static int binary(int[] arr,int low,int high,int key)
{
int mid;
mid=(low+high)/2;
if(low<=high)
{
if(arr[mid]>key)
{
count=count+1;
return binary(arr,low,mid-1,key);
}
else if(arr[mid]<key)
{
count=count+1;
return binary(arr,mid+1,high,key);
}
else
{
return mid;
}
}
return -1;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
int arr[]=new int[20];
int size,key;
System.out.print("Enter the size of the array: ");

III CSE-A Design and Analysis of Algorithms Lab 19251A0524


A.Y. 2021-2022
size=sc.nextInt();
int low=0,high=size-1;
System.out.println("Enter the array elements: ");
for(int i=0;i<size;i++)
{
arr[i]=sc.nextInt();
}
System.out.print("Enter the key element to Search : ");
key=sc.nextInt();
int pos=binary(arr,low,high,key);
if(pos!=-1)
System.out.println("The element you are searching is found
at the position : "+pos);
else
System.out.println("The element you are searching is not
found");
System.out.println(" Element search Count is: "+count);
}
}

OUTPUT-1

Enter the size of the array: 7


Enter the array elements:
12 23 34 45 56 67 78
Enter the key element to Search : 67
The element you are searching is found at the position : 5
Element search Count is: 1

OUTPUT-2

Enter the size of the array: 8


Enter the array elements:
1 2 6 9 13 26 36 68
Enter the key element to Search : 8
The element you are searching is not found
Element search Count is: 3

III CSE-A Design and Analysis of Algorithms Lab 19251A0524


A.Y. 2021-2022
AIM: Write a program in java to analyze time complexity of Bubble
Sort.

CODE:

package lab;
import java.util.*;
class BubbleSort
{
static int count=0;
void bubbleSort(int arr[])
{
int n = arr.length;
for (int i = 0; i < n-1; i++)
{
count+=1;
for (int j = 0; j < n-i-1; j++)
{
count+=1;
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;
count+=1;
}
}
count+=1;
}
count+=1;
}
/* Prints the array */
void printArray(int arr[])
{
int n = arr.length;
for (int i=0; i<n; ++i)
System.out.print(arr[i] + " ");
System.out.println();
}

III CSE-A Design and Analysis of Algorithms Lab 19251A0524


A.Y. 2021-2022
public static void main(String args[])
{
BubbleSort ob = new BubbleSort();
System.out.println("Enter the number of elements ");
Scanner sc= new Scanner(System.in);
int n,i;
n=sc.nextInt();
int arr[] = new int[n];
System.out.println("Enter the elements ");
for(i=0;i<n;i++)
{
arr[i]=sc.nextInt();
}
ob.bubbleSort(arr);
System.out.println("Sorted array");
ob.printArray(arr);
System.out.println("Number of element comparisons = "+count);
}
}

OUTPUT-1

Enter the number of elements


5
Enter the elements
12 34 83 56 37
Sorted array
12 34 37 56 83
Number of element comparisons = 22

OUTPUT-2

Enter the number of elements


7
Enter the elements
56 923 67 238 81 34 17
Sorted array
17 34 56 67 81 238 923
Number of element comparisons = 49

III CSE-A Design and Analysis of Algorithms Lab 19251A0524


A.Y. 2021-2022
AIM: Write a program in java to analyze time complexity of fibonacci
series using recursion.
CODE:

import java.util.*;
public class fibonacii {
static int count=0;
static int fib(int n)
{
if (n <= 1){
count=count+2;
return n;
}
count=count+2;
return fib(n-1) + fib(n-2);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
int n ;
System.out.println("Enter the number: ");
n=sc.nextInt();
for(int i=0;i<n;i++)
{
System.out.print(fib(i)+" ");
}
System.out.println();
System.out.println("Count is : "+count);
}

}
OUTPUT-1
Enter the number:
6
0 1 1 2 3 5
Count is : 68
OUTPUT-2
Enter the number:
10
0 1 1 2 3 5 8 13 21 34
Count is : 552

III CSE-A Design and Analysis of Algorithms Lab 19251A0524


A.Y. 2021-2022
AIM: Write a program in java to analyze time complexity of Selection
Sort.

CODE:

import java.util.*;
public class Selection {

public static void main(String[] args) {


// TODO Auto-generated method stub
int n,i,j;
System.out.println("Enter the size of the array ");
Scanner sc=new Scanner(System.in);
n=sc.nextInt();
int[] arr=new int[n];
System.out.println("Enter the elements");
for(i=0;i<n;i++)
{
arr[i]=sc.nextInt();
}
int min;
for(i=0;i<n;i++)
{
min=i;
for(j=i;j<n;j++)
{
if(arr[j]<arr[min])
{
min=j;
}
}
int temp=arr[i];
arr[i]=arr[min];
arr[min]=temp;
}
System.out.println("Sorted array is");
for(i=0;i<n;i++)
{
System.out.println(arr[i]);
}

III CSE-A Design and Analysis of Algorithms Lab 19251A0524


A.Y. 2021-2022
}

OUTPUT-1

Enter the size of the array


6
Enter the elements
34 56 93 20 48 19
Sorted array is
19 20 34 48 56 93
Count = 86

OUTPUT-2

Enter the size of the array


7
Enter the elements
12 34 62 84 92 59 72
Sorted array is
12 34 59 62 72 84 92
Count = 103

III CSE-A Design and Analysis of Algorithms Lab 19251A0524


A.Y. 2021-2022
AIM: Write a java program to implement a Quick sort algorithm for
sorting a list of integers in ascending order.

CODE:

import java.util.*;
public class quicks
{
static int a[];
static int cc=0;
static int scc=0;
int partition(int a[],int start,int end)
{
int pivot=a[end];
int i=start-1;
for(int j=start;j<=end-1;j++)
{
if(a[j]<pivot)
{
i++;
System.out.println("swap("+i+","+j+")");
int t=a[i];
a[i]=a[j];
a[j]=t;
printa(a,a.length);
scc=scc+1;
}
}
System.out.println("swap("+(i+1)+","+end+")");
int t=a[i+1];
a[i+1]=a[end];
a[end]=t;
printa(a,a.length);
scc=scc+1;
return(i+1);
}

III CSE-A Design and Analysis of Algorithms Lab 19251A0524


A.Y. 2021-2022
void quick(int a[],int start,int end)
{
cc=cc+1;
if(start<end)
{
int p=partition(a,start,end);
System.out.println("quick("+start+","+(p-1)+")");
quick(a,start,p-1);
System.out.println("quick("+(p+1)+","+end+")");
quick(a,p+1,end);
}
}
void printa(int a[],int n)
{
int i;
for(i=0;i<n;i++)
{
System.out.print(a[i]+" ");
}
System.out.println();
}
public static void main(String[] args)
{
int i,n;
System.out.println("Enter the number of elements");
Scanner sc=new Scanner(System.in);
n=sc.nextInt();
a=new int[n];
System.out.println("Enter the elements");
for(i=0;i<n;i++)
{
a[i]=sc.nextInt();
}
quicks ob=new quicks();
System.out.println("Elements before sorting ");
ob.printa(a,n);
ob.quick(a,0,n-1);
System.out.println();
System.out.println("Elements after sorting ");

III CSE-A Design and Analysis of Algorithms Lab 19251A0524


A.Y. 2021-2022
ob.printa(a,n);
System.out.println("Comparisons count "+cc);
System.out.println("Swap count "+scc);
}

OUTPUT-1

Enter the number of elements


5
Enter the elements
34 12 45 26 72
Elements before sorting
34 12 45 26 72
swap(0,0)
34 12 45 26 72
swap(1,1)
34 12 45 26 72
swap(2,2)
34 12 45 26 72
swap(3,3)
34 12 45 26 72
swap(4,4)
34 12 45 26 72
quick(0,3)
swap(0,1)
12 34 45 26 72
swap(1,3)
12 26 45 34 72
quick(0,0)
quick(2,3)
swap(2,3)
12 26 34 45 72
quick(2,1)
quick(3,3)
quick(5,4)
Elements after sorting
12 26 34 45 72
Comparisons count 7
Swap count 8

III CSE-A Design and Analysis of Algorithms Lab 19251A0524


A.Y. 2021-2022
OUTPUT-2

Enter the number of elements


5
Enter the elements
34 45 56 67 78
Elements before sorting
34 45 56 67 78
swap(0,0)
34 45 56 67 78
swap(1,1)
34 45 56 67 78
swap(2,2)
34 45 56 67 78
swap(3,3)
34 45 56 67 78
swap(4,4)
34 45 56 67 78
quick(0,3)
swap(0,0)
34 45 56 67 78
swap(1,1)
34 45 56 67 78
swap(2,2)
34 45 56 67 78
swap(3,3)
34 45 56 67 78
quick(0,2)
swap(0,0)
34 45 56 67 78
swap(1,1)
34 45 56 67 78
swap(2,2)
34 45 56 67 78
quick(0,1)
swap(0,0)
34 45 56 67 78
swap(1,1)
34 45 56 67 78

III CSE-A Design and Analysis of Algorithms Lab 19251A0524


A.Y. 2021-2022
quick(0,0)
quick(2,1)
quick(3,2)
quick(4,3)
quick(5,4)

Elements after sorting


34 45 56 67 78
Comparisons count 9
Swap count 14

III CSE-A Design and Analysis of Algorithms Lab 19251A0524


A.Y. 2021-2022
AIM: Write a java program to implement a Merge sort algorithm for
sorting a list of integers in ascending order.

CODE:

import java.util.*;
public class msort
{
static int a[];
static int cc=0;
static int scc=0,mc=0;
void merge(int a[],int beg,int mid,int end)
{
mc=mc+1;
int l=mid-beg+1;
int r=end-mid;
int larr[]=new int[l];
int rarr[]=new int[r];
for(int i=0;i<l;i++)
{
larr[i]=a[beg+i];
}
for(int j=0;j<r;j++)
{
rarr[j]=a[mid+j+1];
}
int i=0,j=0,k=beg;
while(i<l && j<r)
{
if(larr[i]<=rarr[j])
{
a[k]=larr[i];
i++;
scc=scc+1;
}
else
{
a[k]=rarr[j];
j++;
scc=scc+1;

III CSE-A Design and Analysis of Algorithms Lab 19251A0524


A.Y. 2021-2022
}
k++;
}
while(i<l)
{
a[k]=larr[i];
i++;
k++;
scc=scc+1;
}
while(j<r)
{
a[k]=rarr[j];
j++;
k++;
scc=scc+1;
}
}
void sort(int a[],int beg,int end)
{
cc=cc+1;
if(beg<end)
{
int mid=(beg+end)/2;
sort(a,beg,mid);
sort(a,mid+1,end);
merge(a,beg,mid,end);
}
}
public static void main(String[] args)
{
// TODO Auto-generated method stub
int i,n;
System.out.println("Enter the number of elements");
Scanner sc=new Scanner(System.in);
n=sc.nextInt();
a=new int[n];
System.out.println("Enter the elements");

III CSE-A Design and Analysis of Algorithms Lab 19251A0524


A.Y. 2021-2022
for(i=0;i<n;i++)
{
a[i]=sc.nextInt();
}
msort ob=new msort();
ob.sort(a,0,n-1);
System.out.println("Sorted list");
for(i=0;i<n;i++)
{
System.out.println(a[i]+" ");
}
}
}

OUTPUT-1

Enter the number of elements


6
Enter the elements
12 24 871 264 182 812
Sorted list
12 24 182 264 812 871

OUTPUT-2

Enter the number of elements


8
Enter the elements
12 82 36 2 12 812 178 93
Sorted list
2 12 12 36 82 93 178 812

III CSE-A Design and Analysis of Algorithms Lab 19251A0524


A.Y. 2021-2022
AIM: Write a java program to implement greedy algorithm for job
sequencing with deadlines.

CODE:

import java.util.Scanner;

public class jobseq {

public static void main(String[] args)


{
// TODO Auto-generated method stub
int i,j,n;
Scanner in= new Scanner(System.in);
System.out.println("Enter the number of jobs : ");
n=in.nextInt();
int p[],d[],ord[];
ord=new int[n];
p=new int[n];
d=new int[n];
System.out.println("Enter the deadlines of the jobs : ");
for(i=0;i<n;i++)
{
d[i]=in.nextInt();
}
System.out.println("Enter the profits of the jobs : ");
for(int z=0;z<n;z++)
{
p[z]=in.nextInt();
ord[z]=z+1;
}

for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
{
if(p[j]<p[j+1])
{
int temp=p[j];
p[j]=p[j+1];

III CSE-A Design and Analysis of Algorithms Lab 19251A0524


A.Y. 2021-2022
p[j+1]=temp;

temp=d[j];
d[j]=d[j+1];
d[j+1]=temp;
}
}
}
int t=0,maxp=0,k=0;
int res[]=new int[n];
System.out.println("Jobs selected are : ");
for(i=0;i<n;i++)
{
int a=d[i]-1;
if(a==0 && res[a]==1)
continue;
if(res[a]==0){
res[a]=1;
System.out.print("J"+(i+1)+"");
maxp=maxp+p[i];
}
else
{
a=a-1;
if(res[a]==0){
res[a]=1;
System.out.print("J"+(i+1)+"");
maxp=maxp+p[i];
}
}
}
System.out.println("Maximum profit is: "+maxp);
}

III CSE-A Design and Analysis of Algorithms Lab 19251A0524


A.Y. 2021-2022
OUTPUT-1

Enter the number of jobs :


5
Enter the deadlines of the jobs :
2 2 1 3 3
Enter the profits of the jobs :
20 15 10 5 1
Jobs selected are :
J1 J2 J4
Maximum profit is: 40

OUTPUT-2

Enter the number of jobs :


4
Enter the deadlines of the jobs :
2 1 2 1
Enter the profits of the jobs :
100 10 15 27
Jobs selected are :
J1 J2
Maximum profit is: 127

III CSE-A Design and Analysis of Algorithms Lab 19251A0524


A.Y. 2021-2022
AIM: Write a java program to implement Dijkstra’s algorithm for the
Single source shortest path problem.

CODE:

import java.util.Scanner;
public class shortest {
public static void main(String[] args)
{
int i, j;
int dist[]=new int[10], visited[]=new int[10];
int cost[][]=new int[10][10], path[]=new int[10];
Scanner in = new Scanner(System.in);
System.out.println("DIJKSTRA'S ALGORITHM");
System.out.println("Enter the number of nodes: ");
int n= in.nextInt();
/* cost(i,i)=999 for 1<=i<=n and if edge <i,j> does not exist
cost(i,j)=999*/
System.out.println("Enter the cost matrix");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
cost[i][j] = in.nextInt();
System.out.println("The entered cost matrix is");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
System.out.print(cost[i][j]+"\t");
}
System.out.println();
}
System.out.println("Enter the source vertex: ");
int sv = in.nextInt();
dij(cost,dist,sv,n,path,visited);
printpath(sv,n,dist,path,visited );
System.out.println("\n-----------------------------");
}

III CSE-A Design and Analysis of Algorithms Lab 19251A0524


A.Y. 2021-2022
static void dij(int cost[][],int dist[],int sv,int n,int path[],int
visited[])
{
int count = 2,min,v=0;
for(int i=1; i<=n; i++)
{
visited[i]=0;
dist[i] = cost[sv][i];
if(cost[sv][i] == 999)
path[i] = 0;
else
path[i] = sv;
}
visited[sv]=1;
while(count<=n)
{
min = 999;
for(int w=1; w<=n; w++)
if((dist[w]< min) && (visited[w]==0))
{
min = dist[w];
v = w;
}
visited[v] = 1;
count++;
for(int w=1; w<=n; w++)
{
if((dist[w]) >(dist[v] + cost[v][w]))
{
dist[w] = dist[v] + cost[v][w];
path[w] = v;
}
}
}
}

III CSE-A Design and Analysis of Algorithms Lab 19251A0524


A.Y. 2021-2022
static void printpath(int sv,int n,int dist[],int path[],int
visited[])
{
for(int w=1; w<=n; w++)
{
if(visited[w] == 1 && w != sv)
{
System.out.println("The shortest distance between ");
System.out.println(sv+"-> ="+w+" is :"+ dist[w]);
int t=path[w];
System.out.println("The path is:");
System.out.print(" "+w);
while(t != sv)
{
System.out.print("<-->"+t);
t=path[t];
}
System.out.print("<-->"+sv);
}
}
}
}

III CSE-A Design and Analysis of Algorithms Lab 19251A0524


A.Y. 2021-2022
OUTPUT-1

DIJKSTRA'S ALGORITHM
Enter the number of nodes:
6
Enter the cost matrix
999 50 45 10 999 999
999 999 10 15 999 999
999 999 999 999 30 999
20 999 999 999 15 999
999 20 35 999 999 999
999 999 999 999 3 999
The entered cost matrix is
999 50 45 10 999 999
999 999 10 15 999 999
999 999 999 999 30 999
20 999 999 999 15 999
999 20 35 999 999 999
999 999 999 999 3 999
Enter the source vertex:
1
The shortest distance between
1-> =2 is :45
The path is:
2<-->5<-->4<-->1The shortest distance between
1-> =3 is :45
The path is:
3<-->1The shortest distance between
1-> =4 is :10
The path is:
4<-->1The shortest distance between
1-> =5 is :25
The path is:
5<-->4<-->1
-----------------------------

III CSE-A Design and Analysis of Algorithms Lab 19251A0524


A.Y. 2021-2022
OUTPUT-2
DIJKSTRA'S ALGORITHM
Enter the number of nodes:
4
Enter the cost matrix
999 3 999 7
3 999 4 2
999 4 999 5
7 2 5 999
The entered cost matrix is
999 3 999 7
3 999 4 2
999 4 999 5
7 2 5 999
Enter the source vertex:
1
The shortest distance between
1-> =2 is :3
The path is:
2<-->1The shortest distance between
1-> =3 is :7
The path is:
3<-->2<-->1The shortest distance between
1-> =4 is :5
The path is:
4<-->2<-->1
-----------------------------

III CSE-A Design and Analysis of Algorithms Lab 19251A0524


A.Y. 2021-2022
AIM: Write a java program that implements Kruskal’s algorithm to
generate minimum cost spanning trees.

CODE:

import java.util.Scanner;
class edge
{
int cost;
int sv;
int ev;
}

public class Krushkal {


static int n, n1;
static int t[][] = new int[50][3];
static int parent[] = new int[50];

static int find(int m)


{
int p = m;
while (parent[p] != -1)
p = parent[p];
return p;
}

static void union(int i, int j)


{
if (i < j)

parent[i] = j;
else
parent[j] = i;

III CSE-A Design and Analysis of Algorithms Lab 19251A0524


A.Y. 2021-2022
}

static void kruskalsMethod(edge array[])


{
edge x = new edge();
n1 = n;
for (int i = 1; i <= n; i++)
parent[i] = -1;
for (int i = n / 2; i >= 1; i--)
Adjust(array, i);
System.out.print("Min Heap Tree :");
for (int i = 1; i <= n; i++)
System.out.print(array[i].cost + " ");
System.out.println();
int i = 0, j, mincost = 0, k = 0;
System.out.println("Edges in the Min Heap :");
while (k < n - 1 && n1 > 0)
{
x.cost = array[1].cost;
x.sv = array[1].sv;
x.ev = array[1].ev;
i = find(x.sv);
j = find(x.ev);
if ((i != j) || (i == -1 && j == -1)) {

union(i, j);
k = k + 1;
t[k][1] = x.sv;
t[k][2] = x.ev;
System.out.print(array[1].cost + " ");
mincost += array[1].cost;
}

III CSE-A Design and Analysis of Algorithms Lab 19251A0524


A.Y. 2021-2022
array[1].cost = array[n1].cost;
array[1].sv = array[n1].sv;
array[1].ev = array[n1].ev;
n1--;
Adjust(array, 1);
}
System.out.println();
System.out.println("Edges in the minimum Spanning Tree: ");

for (int l = 1; l <= k; l++)


System.out.println(t[l][1] + " " + t[l][2]);
System.out.println("The cost of minimum spanning tree : " +
mincost);
}

static void Adjust(edge array[], int i)


{
int left = 2 * i;
int right = 2 * i + 1;
int max = i;
if (left <= n1 && array[left].cost < array[i].cost)
max = left;

if (right <= n1 && array[right].cost < array[max].cost)


max = right;
if (max != i) {
edge temp = new edge();
temp.cost = array[i].cost;
temp.sv = array[i].sv;
temp.ev = array[i].ev;
array[i].cost = array[max].cost;
array[i].sv = array[max].sv;

III CSE-A Design and Analysis of Algorithms Lab 19251A0524


A.Y. 2021-2022
array[i].ev = array[max].ev;
array[max].cost = temp.cost;
array[max].sv = temp.sv;
array[max].ev = temp.ev;
Adjust(array, max);
}

public static void main(String[] args)


{
Scanner in = new Scanner(System.in);
System.out.println("Enter no of Edges :");
n = in.nextInt();
edge array[] = new edge[50];
System.out.println("Enter " + n + " "
+ "start vertex , end vertex and cost");
for (int i = 1; i <= n; i++)

{
array[i] = new edge();
array[i].sv = in.nextInt();
array[i].ev = in.nextInt();
array[i].cost = in.nextInt();
}
kruskalsMethod(array);
}
}

III CSE-A Design and Analysis of Algorithms Lab 19251A0524


A.Y. 2021-2022
OUTPUT-1:
Enter no of Edges :
5
Enter 5 start vertex , end vertex and cost
1 2 10
1 3 6
1 4 5
2 4 15
3 4 4
Min Heap Tree :4 6 5 15 10
Edges in the Min Heap :
4 5 10
Edges in the minimum Spanning Tree:
3 4
1 4
1 2
The cost of minimum spanning tree : 19

OUTPUT-2:

III CSE-A Design and Analysis of Algorithms Lab 19251A0524


A.Y. 2021-2022
Enter no of Edges :
7
Enter 7 start vertex , end vertex and cost
1 2 2
2 3 3
1 4 6
2 4 8
2 5 5
4 5 9
3 5 7
Min Heap Tree :2 3 6 8 5 9 7
Edges in the Min Heap :
2 3 5 6
Edges in the minimum Spanning Tree:
1 2
2 3
2 5
1 4
The cost of minimum spanning tree : 16

III CSE-A Design and Analysis of Algorithms Lab 19251A0524


A.Y. 2021-2022

You might also like