0% found this document useful (0 votes)
68 views4 pages

Implement A Min Binary Heap and Use It To Implement: (A) Heap Sort (B) Process Scheduler For An Operating System

This document provides instructions for implementing a min binary heap and using it to implement heap sort and a process scheduler for an operating system. It includes sample code for a MinBinaryHeap class and outlines steps to take heap sort and process scheduling: 1. Write code for a MinBinaryHeap class using an ArrayList to store elements and include methods for insertion, getting the minimum, and helper functions. 2. Write a heapSort method that takes an integer array as input and returns the sorted array using the heap. 3. Write a process scheduler that takes an array of Process objects as input where Process implements Comparable based on priority. The scheduler outputs processes in order of execution based on priority.

Uploaded by

Crystal Taylor
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views4 pages

Implement A Min Binary Heap and Use It To Implement: (A) Heap Sort (B) Process Scheduler For An Operating System

This document provides instructions for implementing a min binary heap and using it to implement heap sort and a process scheduler for an operating system. It includes sample code for a MinBinaryHeap class and outlines steps to take heap sort and process scheduling: 1. Write code for a MinBinaryHeap class using an ArrayList to store elements and include methods for insertion, getting the minimum, and helper functions. 2. Write a heapSort method that takes an integer array as input and returns the sorted array using the heap. 3. Write a process scheduler that takes an array of Process objects as input where Process implements Comparable based on priority. The scheduler outputs processes in order of execution based on priority.

Uploaded by

Crystal Taylor
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Implement a min binary heap and use it to implement:

(a) Heap sort


(b) Process scheduler for an operating system.
The heap should be implemented using generic types. This allows the heap
class to be reused for storing different types of elements

Road map
Step 1: Write the code for a heap using ArrayLists.
Heres some sample code to start with. Assume that the generic type E implements
the comparable interface.
// class MinBinaryHeap can be instantiated for a specific type.
// Example: MinBinaryHeap<int> h = new MinBinaryHeap<int>();
public class MinBinaryHeap<E extends Comparable<E> {
/**
array list to store a heap.
Arraylist is preferred to an array since it can expand
dynamically.
**/
private ArrayList<E> heapArray;
MinBinaryHeap() {
heapArray = new ArrayList(E)();
}
private bubbleUp( ) { } ;
private bubbleDown() {};
public void insert(E value) { } ;
public void getMin() {};

// Add any other helper functions of your choice.


}

Step 2: Implement heap sort.


Write a method heap sort that takes as input an array of integers and returns
the same array sorted.

Step 3: Implement a process scheduler for an operating system.


(i)
Input: The process scheduler takes as input an array of objects of
class Process defined below. Note that this class implements the
comparable interface, so processes can be compared and ordered
w.r.t their priorities.
public class Process implements Comparable<Process> {
int pid; // process id
/**
priority of a process.
Higher the number lower the priority. E.g., 2 has higher priority than
12. Every operating system uses its own techniques to assign a priority
to each process. As an example: Linux computes a goodness value for each
process. Heres goodness() sample code:
http://www.cs.miami.edu/~burt/learning/Csc521.061/notes/goodness_c.txt.
**/
int priority;
String processName; // name of the process (e.g., word or skype etc.).
// constructor
Process(int pid_, int priority_, String processName_) {
pid = pid_; priority = priority_; processName = processName_;
}
/**
compareTo compares two processes according to their priorities.
Read the comparable interface
https://docs.oracle.com/javase/7/docs/api/java/lang/Comparable.html
Also, here is some example code that demonstrates implementation of
compareTo
http://stackoverflow.com/questions/3718383/java-class-implements-comparable
**/
public int compareTo(Process other) {

}
}

(ii)

Output: The scheduler outputs the list of processes in the order in


which they will be executed starting with the process with the
highest priority (lowest priority number). If two or more processes
have the same priority, then they are scheduled according to their
location in the input array. The format of the output includes: the
order number, the pid and the process name of the process.

Sample input:
Process[10] proc = new Process[10];
procs[0] = new Process(2, 12, MS word);
proc[1] = new Process(1, 10, Powerpoint);
proc[2] = new Process(12, 10, Skype);
proc[3] = new Process(5, 10, Chrome);
Sample output:
Order
Process id
Process Name
1
1
Powerpoint
2
12
Skype
3
5
Chrome
4
2
MS Word

FAQs:
(1)
What is the comparable interface?
Any class that implements the methods from the comparable interface
supports the compareTo method. Using the compareTo method, you
can compare two objects of the same class. Example:
http://stackoverflow.com/questions/3718383/java-class-implementscomparable
(2)
Why use a priority queue, not just a sort for process
scheduling?
In an operating system (OS), the list of processes is a dynamic list. New
processes keep getting added all the time. Hence, it is not possible to
pre-sort the list of all processes and then start scheduling. The OS has
to dynamically keep adding the processes to priority queues in a
dynamic manner. In our project we are assume that the list of
processes is known. However, our goal is to play with heaps not with
other sorting algorithms. If that doesnt convince you, how about:
because the instructor said so and he will be grading this project.
(3)My output doesnt match the sample output exactly.

As long as your output contains the information on separate lines as in


the sample output it is fine. You do not have to align the columns.
(4)Where do I go for help?
Post a question on the discussion board. Do not post questions asking for the
code. However, you can post questions that ask for help with a specific line of
code or using a specific interface etc.

You might also like