# Shortest Job First (Scheduling) Algorithm

``````#include <stdio.h>
int main()
{
int A;
int i, j, n, total = 0, index, temp;
float avg_wt, avg_tat;
printf("Enter number of process: ");
scanf("%d", &n);
printf("Enter Burst Time:\n");
for (i = 0; i < n; i++) {
printf("P%d: ", i + 1);
scanf("%d", &A[i]);
A[i] = i + 1;
}
for (i = 0; i < n; i++) {
index = i;
for (j = i + 1; j < n; j++)
if (A[j] < A[index])
index = j;
temp = A[i];
A[i] = A[index];
A[index] = temp;
temp = A[i];
A[i] = A[index];
A[index] = temp;
}
A = 0;
for (i = 1; i < n; i++) {
A[i] = 0;
for (j = 0; j < i; j++)
A[i] += A[j];

total += A[i];
}
avg_wt = (float)total / n;
total = 0;
printf("P BT WT TAT\n");
for (i = 0; i < n; i++) {
A[i] = A[i] + A[i];
total += A[i];
printf("P%d %d %d %d\n", A[i],
A[i], A[i], A[i]);

}
avg_tat = (float)total / n;
printf("Average Waiting Time= %f", avg_wt);
printf("\nAverage Turnaround Time= %f", avg_tat);
}``````

ALGORITHM:
Step 1: Inside the structure declare the variables.
Step 2: Declare the variable i,j as integer,totwtime and totttime is equal to zero.
Step 3: Get the value of „n‟ assign pid as I and get the value of p[i].btime.
Step 4: Assign p wtime as zero and tot time as btime and inside the loop calculate wait time
and turnaround time.
Step 5: Calculate total wait time and total turnaround time by dividing by total number of
process.
Step 6: Print total wait time and total turnaround time.
Step 7: Stop the program.

This site uses Akismet to reduce spam. Learn how your comment data is processed.