Posts

Showing posts from April, 2018

Java program for Safe Sequence (Banker's Algorithm).

//Java program for safe sequence of Banker's algorithm. import java.util.*; class SafeAlgo { public static void main(String args[]) { Scanner sc=new Scanner(System.in); System.out.println("Enter number of processes:"); int n=sc.nextInt(); int p[]=new int[n]; int flag[]=new int[n]; System.out.println("Enter number of resource variables"); int m=sc.nextInt(); int max[][]=new int[n][m]; int allo[][]=new int[n][m]; int need[][]=new int[n][m]; int avail[]=new int[m]; for(int x=0;x<n;x++) { System.out.println("Enter process number:"); p[x]=sc.nextInt(); flag[x]=0; for(int y=0;y<m;y++) { System.out.println("Max -> P"+p[x]+" ->"+y+" :"); max[x][y]=sc.nextInt(); } for(int y=0;y<m;y++) { System.out.println("Allocation -> P"+p[x]+" ->"+y+" :"); allo[x][y]=sc.nextInt(); need[x][y]=max[x][y]-allo[x]...

SJF Scheduling Algorithm Java Implementation.

//Java program for Shortest Job First Scheduling algorithm. import java.io.*; public class SJF{ public static void main(String args[]) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n; System.out.println("Please enter the number of Processes: "); n = Integer.parseInt(br.readLine()); int proc[][] = new int[n + 1][4]; for(int i = 1; i <= n; i++) { System.out.println("Please enter the Arrival Time for Process " + i + ": "); proc[i][0] = Integer.parseInt(br.readLine()); System.out.println("Please enter the Burst Time for Process " + i + ": "); proc[i][1] = Integer.parseInt(br.readLine()); } System.out.println(); int total_time = 0; for(int i = 1; i <= n; i++) { total_time += proc[i][1]; } int time_chart[] = new int[total_time]; for(int i = 0; i ...

Round Robin Scheduling Algorithm Java Implementation.

//Java Program for Round Robin scheduling algorithm. import java.io.*; class rr { public static void main(String args[])throws IOException { DataInputStream in=new DataInputStream(System.in); int i,j,k,q,sum=0; System.out.println("Enter number of process:"); int n=Integer.parseInt(in.readLine()); int bt[]=new int[n]; int wt[]=new int[n]; int tat[]=new int[n]; int a[]=new int[n]; System.out.println("Enter brust Time:"); for(i=0;i<n;i++) { System.out.println("Enter brust Time for "+(i+1)); bt[i]=Integer.parseInt(in.readLine()); } System.out.println("Enter Time quantum:"); q=Integer.parseInt(in.readLine()); for(i=0;i<n;i++) a[i]=bt[i]; for(i=0;i<n;i++) wt[i]=0; do { for(i=0;i<n;i++) { if(bt[i]>q) { bt[i]-=q; for(j=0;j<n;j++) { if((j!=i)&&(bt[j]!=0)) wt[j]+=q; } } else { for(j=0;j<n;j++) { if((j!=i)&&(bt[j]!=0)) wt[j]+=bt[i]; } bt[i]=0; } } sum=0; for(k=0;k<n;k++) sum=sum+bt[k]; } while(sum!=0); for(i=0;i<...

Priority Scheduling Algorithm Java Program.

import java.util.Scanner; public class priority{ public static void main(String args[]) { Scanner s = new Scanner(System.in); int x,n,p[],pp[],bt[],w[],t[],awt,atat,i; p = new int[10]; pp = new int[10]; bt = new int[10]; w = new int[10]; t = new int[10]; //n is number of process //p is process //pp is process priority //bt is process burst time //w is wait time // t is turnaround time //awt is average waiting time //atat is average turnaround time System.out.print("Enter the number of process : "); n = s.nextInt(); System.out.print("\n\t Enter burst time : time priorities \n"); for(i=0;i<n;i++) { System.out.print("\nProcess["+(i+1)+"]:"); bt[i] = s.nextInt(); pp[i] = s.nextInt(); p[i]=i+1; } //sorting on the basis of priority for(i=0;i<n-1;i++) { for...

Optimal Page Replacement algorithm in Java.

import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class OptimalReplacement { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int frames, pointer = 0, hit = 0, fault = 0,ref_len; boolean isFull = false; int buffer[]; int reference[]; int mem_layout[][]; System.out.println("Enter the number of Frames: "); frames = Integer.parseInt(br.readLine()); System.out.println("Enter the length of the Reference string: "); ref_len = Integer.parseInt(br.readLine()); reference = new int[ref_len]; mem_layout = new int[ref_len][frames]; buffer = new int[frames]; for(int j = 0; j < frames; j++) buffer[j] = -1; System.out.println("Enter the reference string: "...

LRU Page Replacement algorithm in Java.(Least Recently Used)

import java.util.Scanner; public class lru {     public static int min(int counter[],int nFrames) {     int minimum = counter[0];     int pos = 0;     for(int i=0;i<nFrames;i++) { if(minimum > counter[i])           pos = i;     }  return pos;  }         public static void main(String[] args) {         // TODO code application logic here         Scanner s = new Scanner(System.in);     int n,recent = 0,pageFault = 0,nFrames;           System.out.print("Enter the number of pages: ");       n = s.nextInt();     int pageString[] = new int[n];     System.out.print("Enter the page reference string: ");     for(int i=0;i<n;i++)         pageString[i]=s.nextInt();            System.out.prin...

FIFO Page Replacement algorithm in Java.

import java.io.*; public class FIFO { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int frames, pointer = 0, hit = 0, fault = 0,ref_len; int buffer[]; int reference[]; int mem_layout[][]; System.out.println("Enter the number of Frames: "); frames = Integer.parseInt(br.readLine()); System.out.println("Enter the length of the Reference string: "); ref_len = Integer.parseInt(br.readLine()); reference = new int[ref_len]; mem_layout = new int[ref_len][frames]; buffer = new int[frames]; for(int j = 0; j < frames; j++) buffer[j] = -1; System.out.println("Enter the reference string: "); for(int i = 0; i < ref_len; i++) { reference[i] = Integer.parseInt(br.readLine()); ...

FCFS Scheduling Algorithm Java implementation.

//Java program for FCFS scheduling algorithm. import java.util.*; class FCFS { public static void main(String args[]) { int n, at[], bt[], wt[], tat[]; float avgT; float awt; Scanner sc=new Scanner(System.in); System.out.println("Enter the number of processes to be scheduled: "); at=new int[10]; bt=new int[10]; wt=new int[10]; tat=new int[10]; n=sc.nextInt(); System.out.println("Enter arrival times of the processes:"); for(int i=0; i<n; i++) { at[i]=sc.nextInt(); } System.out.println("Enter burst times of the processes: "); for(int i=0; i<n; i++) { bt[i]=sc.nextInt(); } wt[0]=0; for(int i=1; i<n; i++) { wt[i]=wt[i-1]+bt[i-1]; wt[i]=wt[i]-at[i]; } avgT=0; awt=0; for(int i=0; i<n; i++) { tat[i]=wt[i]+bt[i]; avgT=avgT+tat[i]; awt=awt+wt[i]; } System.out.println("AT BT WT TAT"); for(int i=0; i...

Java program for binary search.

//Binary search example in Java. import java.util.Scanner ;   class BinarySearch { public static void main ( String args [ ] ) { int c, first, last, middle, n, search, array [ ] ;   Scanner in = new Scanner ( System . in ) ; System . out . println ( "Enter number of elements" ) ; n = in. nextInt ( ) ; array = new int [ n ] ;   System . out . println ( "Enter " + n + " numbers" ) ;     for ( c = 0 ; c < n ; c ++ ) array [ c ] = in. nextInt ( ) ;   System . out . println ( "Enter the number to be searched" ) ; search = in. nextInt ( ) ;   first = 0 ; last = n - 1 ; middle = ( first + last ) / 2 ;   while ( first <= last ) { if ( array [ middle ] < search ) first = middle + 1 ; else if ( array [ middle ] == search ) { System . out . println ( search + " found at l...

Java program for Linear search.

//Linear search example in Java. import java.util.Scanner ;   class LinearSearch { public static void main ( String args [ ] ) { int c, n, search, array [ ] ;   Scanner in = new Scanner ( System . in ) ; System . out . println ( "Enter elements of array" ) ; n = in. nextInt ( ) ; array = new int [ n ] ;   System . out . println ( "Enter " + n + " elements" ) ;   for ( c = 0 ; c < n ; c ++ ) array [ c ] = in. nextInt ( ) ;   System . out . println ( "Enter value to be searched" ) ; search = in. nextInt ( ) ;   for ( c = 0 ; c < n ; c ++ ) { if ( array [ c ] == search ) /* Searching element if it is there */ { System . out . println ( search + " found at location " + ( c + 1 ) + "." ) ; break ; } } if ( c == n ) System . out . println ( search + ...

Implement Merge Sort in Java.

Image
*Merge Sort:             Merge sort is a divide and conquer algorithm. Steps to implement Merge Sort: 1) Divide the unsorted array into n partitions, each partition contains 1 element. Here the one element is considered as sorted. 2) Repeatedly merge partitioned units to produce new sublists until there is only 1 sublist remaining. This will be the sorted list at the end. //Write a Java program to implement merge sort. public class MyMergeSort {             private int [] array;      private int [] tempMergArr;      private int length;        public static void main(String a[]){                     int [] inputArr = { 45 , 23 , 11 , 89 , 77 , 98 , 4 , 28 , 65 , 43 };         ...