Professional Documents
Culture Documents
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() {};
}
}
(ii)
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.