# Find the order of execution of given N processes in Round Robin Scheduling

• Last Updated : 11 Oct, 2021

Given an array arr[] representing burst time of N processes scheduled using the Round Robin Algorithm with given quantum time Q. Assuming that all the process arrive at time t = 0, the task is to find the order in which the process execute.

Examples:

Input: arr[] = {3, 7, 4}, q = 3
Output: 0 2 1
Explanation:
The order of execution is as follows P0, P1, P2, P1, P2, P1
Since, P0 has burst time of 3 and quantum time is also 3, it gets completed first.
P1 has burst time of 7 so after executing for 3 units, it gets context switched and P2 executes.
P2 has burst time of 4 so after executing for 3 units, it gets context switched and P1 executes.
Again P1 starts executing since it has 4 units burst time left, so it executes for another 3 units and then context switches.
Now process P2 executes for 1 unit and gets completed.
In the end process P1 is completed.
They complete the execution in the order P0, P2, P1.

Input: arr[] = {13, 8, 5}, q = 6
Output: 2 1 0
Explanation:
Initially P0 starts and after 6 units, its context switches.
P1 executes for 6 units and context switches.
Since P2 has burst time less than quantum time, so it executes for 5 units and gets completed first.
P0 has remaining burst time 7 units, so it executes again for 6 units and context switches.
P1 has remaining burst time as 2 units and it gets completed second.
In the end process P0 gets completed.
They complete the execution in the order P2, P1, P0.

Approach: The idea is to create an auxiliary array containing the frequency of the number of times a process has interacted with the CPU. Then freq[] array can be sorted in the increasing order of frequencies. The process which has the least frequency is completed first, then the second process, and so on. The sorted array gives the order completion of the process. Below are the steps:

• Initialize an array freq[], where freq[i] is the number of times the ith process has interacted with CPU.
• Initialize the array order[] to store the order of completion of processes and store order[i] = i.
• Sort the array order[] in the increasing order of freq[] such that freq[order[i]] ≤ freq[order[i + 1]].
• Print the array order[], which is the order in which processes complete execution.

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach` `#include ``using` `namespace` `std;` `// Function to sort the array order[]``// on the basis of the array freq[]``void` `merge(``int``* order, ``int``* freq, ``int` `i,``           ``int` `mid, ``int` `j)``{``    ``int` `tempOrder[j - i + 1];``    ``int` `temp = mid + 1, index = -1;` `    ``while` `(i <= mid && temp <= j) {` `        ``// If order[i]th is less than``        ``// order[temp]th process``        ``if` `(freq[order[i]]``            ``<= freq[order[temp]]) {``            ``tempOrder[++index] = order[i++];``        ``}` `        ``// Otherwise``        ``else` `{``            ``tempOrder[++index] = order[temp++];``        ``}``    ``}` `    ``// Add the left half to tempOrder[]``    ``while` `(i <= mid) {``        ``tempOrder[++index] = order[i++];``    ``}` `    ``// Add right half to tempOrder[]``    ``while` `(temp <= j) {``        ``tempOrder[++index] = order[temp++];``    ``}` `    ``// Copy the tempOrder[] array to``    ``// order[] array``    ``for` `(index; index >= 0; index--) {``        ``order[j--] = tempOrder[index];``    ``}``}` `// Utility function to sort the array``// order[] on the basis of freq[]``void` `divide(``int``* order, ``int``* freq,``            ``int` `i, ``int` `j)``{``    ``// Base Case``    ``if` `(i >= j)``        ``return``;` `    ``// Divide array into 2 parts``    ``int` `mid = i / 2 + j / 2;` `    ``// Sort the left array``    ``divide(order, freq, i, mid);` `    ``// Sort the right array``    ``divide(order, freq, mid + 1, j);` `    ``// Merge the sorted arrays``    ``merge(order, freq, i, mid, j);``}` `// Function to find the order of``// processes in which execution occurs``void` `orderProcesses(``int` `A[], ``int` `N, ``int` `q)``{``    ``int` `i = 0;` `    ``// Store the frequency``    ``int` `freq[N];` `    ``// Find elements in array freq[]``    ``for` `(i = 0; i < N; i++) {``        ``freq[i] = (A[i] / q)``                  ``+ (A[i] % q > 0);``    ``}` `    ``// Store the order of completion``    ``// of processes``    ``int` `order;` `    ``// Initialize order[i] as i``    ``for` `(i = 0; i < N; i++) {``        ``order[i] = i;``    ``}` `    ``// Function Call to find the order``    ``// of execution``    ``divide(order, freq, 0, N - 1);` `    ``// Print order of completion``    ``// of processes``    ``for` `(i = 0; i < N; i++) {``        ``cout << order[i] << ``"  "``;``    ``}``}` `// Driver Code``int` `main()``{``    ``// Burst Time of the processes``    ``int` `arr[] = { 3, 7, 4 };` `    ``// Quantum Time``    ``int` `Q = 3;` `    ``int` `N = ``sizeof``(arr) / ``sizeof``(arr);` `    ``// Function Call``    ``orderProcesses(arr, N, Q);` `    ``return` `0;``}`

## Java

 `// Java program for the above approach``import` `java.util.*;``class` `GFG``{` `// Function to sort the array order[]``// on the basis of the array freq[]``static` `void` `merge(``int` `order[], ``int` `freq[], ``int` `i,``           ``int` `mid, ``int` `j)``{``    ``int` `tempOrder[] = ``new` `int``[j - i + ``1``];``    ``int` `temp = mid + ``1``, index = -``1``;` `    ``while` `(i <= mid && temp <= j)``    ``{` `        ``// If order[i]th is less than``        ``// order[temp]th process``        ``if` `(freq[order[i]]``            ``<= freq[order[temp]])``        ``{``            ``tempOrder[++index] = order[i++];``        ``}` `        ``// Otherwise``        ``else``        ``{``            ``tempOrder[++index] = order[temp++];``        ``}``    ``}` `    ``// Add the left half to tempOrder[]``    ``while` `(i <= mid)``    ``{``        ``tempOrder[++index] = order[i++];``    ``}` `    ``// Add right half to tempOrder[]``    ``while` `(temp <= j)``    ``{``        ``tempOrder[++index] = order[temp++];``    ``}` `    ``// Copy the tempOrder[] array to``    ``// order[] array``  ``int` `ind= index;``    ``for` `(index= ind; index >= ``0``; index--)``    ``{``        ``order[j--] = tempOrder[index];``    ``}``}` `// Utility function to sort the array``// order[] on the basis of freq[]``static` `void` `divide(``int` `order[], ``int` `freq[],``            ``int` `i, ``int` `j)``{``    ``// Base Case``    ``if` `(i >= j)``        ``return``;` `    ``// Divide array into 2 parts``    ``int` `mid = i / ``2` `+ j / ``2``;` `    ``// Sort the left array``    ``divide(order, freq, i, mid);` `    ``// Sort the right array``    ``divide(order, freq, mid + ``1``, j);` `    ``// Merge the sorted arrays``    ``merge(order, freq, i, mid, j);``}` `// Function to find the order of``// processes in which execution occurs``static` `void` `orderProcesses(``int` `A[], ``int` `N, ``int` `q)``{``    ``int` `i = ``0``;` `    ``// Store the frequency``    ``int` `freq[] = ``new` `int``[N];` `    ``// Find elements in array freq[]``    ``for` `(i = ``0``; i < N; i++)``    ``{``        ``freq[i] = (A[i] / q);``        ``if` `(A[i] % q > ``0``)``              ``freq[i] += ``1``;``    ``}` `    ``// Store the order of completion``    ``// of processes``    ``int` `order[] = ``new` `int``[``4``];` `    ``// Initialize order[i] as i``    ``for` `(i = ``0``; i < N; i++) {``        ``order[i] = i;``    ``}` `    ``// Function Call to find the order``    ``// of execution``    ``divide(order, freq, ``0``, N - ``1``);` `    ``// Print order of completion``    ``// of processes``    ``for` `(i = ``0``; i < N; i++) {``        ``System.out.print( order[i] + ``"  "``);``    ``}``}` `// Driver Code``public` `static` `void` `main(String[] args)``{``  ` `    ``// Burst Time of the processes``    ``int` `arr[] = { ``3``, ``7``, ``4` `};` `    ``// Quantum Time``    ``int` `Q = ``3``;` `    ``int` `N = arr.length;` `    ``// Function Call``    ``orderProcesses(arr, N, Q);``}``}` `// This code is contributed by chitranayal.`

## C#

 `// C# program for the above approach``using` `System;` `class` `GFG{`` ` `// Function to sort the array order[]``// on the basis of the array freq[]``static` `void` `merge(``int``[] order, ``int``[] freq, ``int` `i,``                  ``int` `mid, ``int` `j)``{``    ``int``[] tempOrder = ``new` `int``[j - i + 1];``    ``int` `temp = mid + 1, index = -1;`` ` `    ``while` `(i <= mid && temp <= j)``    ``{``        ` `        ``// If order[i]th is less than``        ``// order[temp]th process``        ``if` `(freq[order[i]] <= freq[order[temp]])``        ``{``            ``tempOrder[++index] = order[i++];``        ``}`` ` `        ``// Otherwise``        ``else``        ``{``            ``tempOrder[++index] = order[temp++];``        ``}``    ``}`` ` `    ``// Add the left half to tempOrder[]``    ``while` `(i <= mid)``    ``{``        ``tempOrder[++index] = order[i++];``    ``}`` ` `    ``// Add right half to tempOrder[]``    ``while` `(temp <= j)``    ``{``        ``tempOrder[++index] = order[temp++];``    ``}`` ` `    ``// Copy the tempOrder[] array to``    ``// order[] array``    ``int` `ind = index;``    ``for``(index = ind; index >= 0; index--)``    ``{``        ``order[j--] = tempOrder[index];``    ``}``}`` ` `// Utility function to sort the array``// order[] on the basis of freq[]``static` `void` `divide(``int``[] order, ``int``[] freq,``                   ``int` `i, ``int` `j)``{``    ` `    ``// Base Case``    ``if` `(i >= j)``        ``return``;`` ` `    ``// Divide array into 2 parts``    ``int` `mid = i / 2 + j / 2;`` ` `    ``// Sort the left array``    ``divide(order, freq, i, mid);`` ` `    ``// Sort the right array``    ``divide(order, freq, mid + 1, j);`` ` `    ``// Merge the sorted arrays``    ``merge(order, freq, i, mid, j);``}`` ` `// Function to find the order of``// processes in which execution occurs``static` `void` `orderProcesses(``int``[] A, ``int` `N, ``int` `q)``{``    ``int` `i = 0;``    ` `    ``// Store the frequency``    ``int``[] freq = ``new` `int``[N];`` ` `    ``// Find elements in array freq[]``    ``for``(i = 0; i < N; i++)``    ``{``        ``freq[i] = (A[i] / q);``        ` `        ``if` `(A[i] % q > 0)``              ``freq[i] += 1;``    ``}`` ` `    ``// Store the order of completion``    ``// of processes``    ``int``[] order = ``new` `int``;`` ` `    ``// Initialize order[i] as i``    ``for``(i = 0; i < N; i++)``    ``{``        ``order[i] = i;``    ``}`` ` `    ``// Function Call to find the order``    ``// of execution``    ``divide(order, freq, 0, N - 1);`` ` `    ``// Print order of completion``    ``// of processes``    ``for``(i = 0; i < N; i++)``    ``{``        ``Console.Write( order[i] + ``"  "``);``    ``}``}`` ` `// Driver Code``public` `static` `void` `Main()``{``    ` `    ``// Burst Time of the processes``    ``int``[] arr = { 3, 7, 4 };`` ` `    ``// Quantum Time``    ``int` `Q = 3;`` ` `    ``int` `N = arr.Length;`` ` `    ``// Function Call``    ``orderProcesses(arr, N, Q);``}``}` `// This code is contributed by sanjoy_62`

## Javascript

 ``

Output:

`0  2  1`

Time Complexity: O(N*log N)
Auxiliary Space: O(N)

My Personal Notes arrow_drop_up