AIM: Write A Program in Java To Analyze Time Complexity of Recursive
AIM: Write A Program in Java To Analyze Time Complexity of Recursive
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: ");
OUTPUT-1
OUTPUT-2
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();
}
OUTPUT-1
OUTPUT-2
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
CODE:
import java.util.*;
public class Selection {
OUTPUT-1
OUTPUT-2
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);
}
OUTPUT-1
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;
OUTPUT-1
OUTPUT-2
CODE:
import java.util.Scanner;
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];
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);
}
OUTPUT-2
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-----------------------------");
}
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
-----------------------------
CODE:
import java.util.Scanner;
class edge
{
int cost;
int sv;
int ev;
}
parent[i] = j;
else
parent[j] = i;
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;
}
{
array[i] = new edge();
array[i].sv = in.nextInt();
array[i].ev = in.nextInt();
array[i].cost = in.nextInt();
}
kruskalsMethod(array);
}
}
OUTPUT-2: