问题描述
使用一个排好序的线性表,升序排序,最后一个元素的优先级最高
难点分析:
提示:这里可以看源码:
优先队列是根据优先级出队列的,这里运用线性表来实现,只需要先将所有数据加入链表,之后根据优先级排序就好,建议直接调用Collections工具箱进行排序。
代码:
提示:这里代码列出了我想的几种方法,所以代码显得有些长:
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Random;
public class Lab24_6ForPriorityQueue<E extends Comparable> {
private ArrayList<E> priorityQueueArrayList = new ArrayList<>();
private Comparator<E> comparator;
private boolean flag = true;
public static void main(String[] args) {
Random random = new Random();
Integer[] arr = new Integer[5000000];
for (int i = 0; i < arr.length; i++)
arr[i] = random.nextInt(1000000);
//时间:
long startTime1 = System.currentTimeMillis();
//创建并且添加数据----》priorityQueueArrayList
Lab24_6ForPriorityQueue<Integer> priorityQueue
= new Lab24_6ForPriorityQueue<>();
for (int i = 0; i < arr.length; i++)
priorityQueue.enqueue(arr[i]);
while (priorityQueue.getSize() > 0)
priorityQueue.dequeue();
long endTime1 = System.currentTimeMillis();
long startTime2 = System.currentTimeMillis();
//创建并且添加数据----》MyPriorityQueue
MyPriorityQueue<Integer> MyPriorityQueue
= new MyPriorityQueue<>();
for (int i = 0; i < arr.length; i++)
MyPriorityQueue.enqueue(arr[i]);
while (priorityQueue.getSize() > 0)
MyPriorityQueue.dequeue();
long endTime2 = System.currentTimeMillis();
//输出
System.out.println("PriorityQueueUsingSortedArrayList添加与删除合计用时:" + (endTime1 - startTime1) + "ms");
System.out.println("MyPriorityQueue添加与删除合计用时:" + (endTime2 - startTime2) + "ms");
这个是之前的测试程序
// PatientForLab24_6 patient1 = new PatientForLab24_6("John", 2);
// PatientForLab24_6 patient2 = new PatientForLab24_6("Jim", 1);
// PatientForLab24_6 patient3 = new PatientForLab24_6("Tim", 5);
// PatientForLab24_6 patient4 = new PatientForLab24_6("Cindy", 7);
//
// Lab24_6ForPriorityQueue<PatientForLab24_6> priorityQueue
// = new Lab24_6ForPriorityQueue<>();
// priorityQueue.enqueue(patient1);
// priorityQueue.enqueue(patient2);
// priorityQueue.enqueue(patient3);
// priorityQueue.enqueue(patient4);
//
// while (priorityQueue.getSize() > 0)
// System.out.print(priorityQueue.dequeue() + " ");
//
//
// System.out.println();
//
// MyPriorityQueue<PatientForLab24_6> priorityQueue1
// = new MyPriorityQueue<>();
// priorityQueue1.enqueue(patient1);
// priorityQueue1.enqueue(patient2);
// priorityQueue1.enqueue(patient3);
// priorityQueue1.enqueue(patient4);
//
// while (priorityQueue1.getSize() > 0)
// System.out.print(priorityQueue1.dequeue() + " ");
}
public Lab24_6ForPriorityQueue() {
comparator = (e1, e2) -> ((Comparable<E>) e1).compareTo(e2);
}
public Lab24_6ForPriorityQueue(Comparator<E> c) {
this.comparator = c;
}
public void enqueue(E newObject) {
flag = true;
priorityQueueArrayList.add(newObject);
// 因为是线性表,所以每次根据优先级排序即可,默认升序
// Collections.sort(priorityQueueArrayList);
这个是之前写的,不可取
// int currentIndex = priorityQueueArrayList.size() - 1;
//
// while (currentIndex > 0) {
// int parentIndex = currentIndex - 1;
// if (comparator.compare(priorityQueueArrayList.get(currentIndex), priorityQueueArrayList.get(parentIndex)) < 0) {
// E temp = priorityQueueArrayList.get(currentIndex);
// priorityQueueArrayList.set(currentIndex, priorityQueueArrayList.get(parentIndex));
// priorityQueueArrayList.set(parentIndex, temp);
// } else
// break;
// currentIndex = parentIndex;
// }
}
public E dequeue() {
if (flag) {
Collections.sort(priorityQueueArrayList);
flag = false;
}
//最后一个优先级最高,所以取最后一个
return priorityQueueArrayList.remove(priorityQueueArrayList.size() - 1);
}
public int getSize() {
return priorityQueueArrayList.size();
}
static class PatientForLab24_6 implements Comparable<PatientForLab24_6> {
private String name;
private int priority;
public PatientForLab24_6(String name, int priority) {
this.name = name;
this.priority = priority;
}
@Override
public String toString() {
return name + "(priority" + priority + ")";
}
@Override
public int compareTo(PatientForLab24_6 o) {
return this.priority - o.priority;
}
}
static class MyPriorityQueue<E extends Comparable<E>> {
private HeapForMyPriorityQueue<E> heap = new HeapForMyPriorityQueue<E>();
public void enqueue(E newObject) {
heap.add(newObject);
}
public E dequeue() {
return heap.remove();
}
public int getSize() {
return heap.getSize();
}
}
}
class HeapForMyPriorityQueue<E> {
private ArrayList<E> list = new ArrayList<>();
private Comparator<? super E> c;
public HeapForMyPriorityQueue() {
this.c = (e1, e2) -> ((Comparable<E>) e1).compareTo(e2);
}
public HeapForMyPriorityQueue(Comparator<E> c) {
this.c = c;
}
public HeapForMyPriorityQueue(E[] object) {
this.c = (e1, e2) -> ((Comparable<E>) e1).compareTo(e2);
for (int i = 0; i < object.length; i++) {
add(object[i]);
}
}
public void add(E newObject) {
list.add(newObject);
int currentIndex = list.size() - 1;
while (currentIndex > 0) {
int parentIndex = (currentIndex - 1) / 2;
if (c.compare(list.get(currentIndex), list.get(parentIndex)) > 0) {
E temp = list.get(currentIndex);
list.set(currentIndex, list.get(parentIndex));
list.set(parentIndex, temp);
} else
break;
currentIndex = parentIndex;
}
}
public E remove() {
if (list.size() == 0) return null;
E removeObject = list.get(0);
list.set(0, list.get(list.size() - 1));
list.remove(list.size() - 1);
int currentIndex = 0;
while (currentIndex < list.size()) {
int leftChildIndex = 2 * currentIndex + 1;
int rightChildIndex = 2 * currentIndex + 2;
if (leftChildIndex >= list.size()) break;
int maxIndex = leftChildIndex;
if (rightChildIndex < list.size()) {
if (c.compare(list.get(maxIndex), list.get(rightChildIndex)) < 0) {
maxIndex = rightChildIndex;
}
}
//对比
if (c.compare(list.get(currentIndex), list.get(maxIndex)) < 0) {
E temp = list.get(maxIndex);
list.set(maxIndex, list.get(currentIndex));
list.set(currentIndex, temp);
currentIndex = maxIndex;
} else
break;
}
return removeObject;
}
public int getSize() {
return list.size();
}
public boolean isEmpty() {
return list.size() == 0;
}
}