Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added new firstcome_firstserve.py #883

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions Algorithms/WaveSort/waveSort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Python function to sort the array arr[0..n-1] in wave form,
# i.e., arr[0] >= arr[1] <= arr[2] >= arr[3] <= arr[4] >= arr[5]
def sortInWave(arr, n):

#sort the array
arr.sort()

# Swap adjacent elements
for i in range(0,n-1,2):
arr[i], arr[i+1] = arr[i+1], arr[i]

# Driver program
arr = [10, 90, 49, 2, 1, 5, 23]
sortInWave(arr, len(arr))
for i in range(0,len(arr)):
print (arr[i],end=" ")


80 changes: 80 additions & 0 deletions Algorithms/cpu_scheduling_algorithms/firstcome_firstserve.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# Python3 program for implementation
# of FCFS scheduling

# Function to find the waiting
# time for all processes
def findWaitingTime(processes, n,
bt, wt):

# waiting time for
# first process is 0
wt[0] = 0

# calculating waiting time
for i in range(1, n ):
wt[i] = bt[i - 1] + wt[i - 1]

# Function to calculate turn
# around time
def findTurnAroundTime(processes, n,
bt, wt, tat):

# calculating turnaround
# time by adding bt[i] + wt[i]
for i in range(n):
tat[i] = bt[i] + wt[i]

# Function to calculate
# average time
def findavgTime( processes, n, bt):

wt = [0] * n
tat = [0] * n
total_wt = 0
total_tat = 0

# Function to find waiting
# time of all processes
findWaitingTime(processes, n, bt, wt)

# Function to find turn around
# time for all processes
findTurnAroundTime(processes, n,
bt, wt, tat)

# Display processes along
# with all details
print( "Processes Burst time " +
" Waiting time " +
" Turn around time")

# Calculate total waiting time
# and total turn around time
for i in range(n):

total_wt = total_wt + wt[i]
total_tat = total_tat + tat[i]
print(" " + str(i + 1) + "\t\t" +
str(bt[i]) + "\t " +
str(wt[i]) + "\t\t " +
str(tat[i]))

print( "Average waiting time = "+
str(total_wt / n))
print("Average turn around time = "+
str(total_tat / n))

# Driver code
if __name__ =="__main__":

# process id's
processes = [ 1, 2, 3]
n = len(processes)

# Burst time of all processes
burst_time = [10, 5, 8]

findavgTime(processes, n, burst_time)

# This code is contributed
# by ChitraNayal