js 优先级队列

当我们希望某个优先级权重比较高的数据,展现在最前面,插入数据的时候,应该按照优先级高低来排序,js的优先级队列最为合适。
比如有这样一组数据
校长 院长 老师 学生 他们的权重依次为 0 1 2 3,在乱序的情况下如何,按照权重依次插入

      //优先级队列
      class QueueElement { // 专门用来初始化数据
        constructor(element, priority) {
          this.element = element
          this.priority = priority
        }
      }
      // 插入
      class PriorityQueue {
        constructor() {
          this.list = []
        }
        enqueue(element, priority) {
          if (this.size() == 0) {// 最开始直接插入
            this.list.push(new QueueElement(element, priority))
          }
          else{
            for(let i = 0; i < this.list.length; i++){
              if(priority <  this.list[i].priority){//按照权重插入
                this.list.splice(i,0,new QueueElement(element, priority))
                return 
              }
            }
            this.list.push(new QueueElement(element, priority))
            return
          }
        }
        size() {
          return this.list.length
        }
        string(){
          const result = this.list.map(item => {
            return item.element
          })
          return result.toString()
        }
      }

来测试下数据

      const priorityQueue = new PriorityQueue()
      priorityQueue.enqueue('院长',1)
      priorityQueue.enqueue('老师',2)
      priorityQueue.enqueue('学生',3)
      priorityQueue.enqueue('校长',0)
      console.log(priorityQueue.string())

结果为 校长,院长,老师,学生

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,多级反馈队列调度算法是一种进程调度算法,它是基于时间片轮转算法的变体。它将进程按照优先级分成多个队列,然后给每个队列分配不同的时间片,高优先级队列的时间片较短,低优先级队列的时间片较长。如果一个进程在一个队列中运行了一个时间片后仍然没有完成,那么它会被移到下一个队列,并将其时间片重置为原始值。这个过程将一直持续到进程完成或者到达最后一个队列。 以下是使用 JavaScript 实现多级反馈队列调度算法的伪代码: ``` // 定义进程类 class Process { constructor(name, priority, time) { this.name = name; // 进程名称 this.priority = priority; // 进程优先级 this.time = time; // 进程需要执行的时间 } } // 定义多级反馈队列调度算法函数 function multiLevelFeedbackQueue(processes) { const queues = [[], [], []]; // 三个队列,分别存放高、中、低优先级进程 const timeQuantum = [4, 8, 16]; // 不同队列对应的时间片 let currentTime = 0; // 当前时间 let completedProcesses = []; // 完成执行的进程 while (true) { let currentProcess = null; // 从高优先级队列开始查找有没有需要执行的进程 for (let i = 0; i < queues.length; i++) { if (queues[i].length > 0) { currentProcess = queues[i].shift(); break; } } // 如果没有需要执行的进程,退出循环 if (!currentProcess) { break; } // 执行当前进程 for (let i = 0; i < timeQuantum[currentProcess.priority]; i++) { currentProcess.time--; currentTime++; // 如果进程已经执行完毕,将其加入完成队列 if (currentProcess.time === 0) { completedProcesses.push(currentProcess); break; } // 如果进程执行时间超过了当前队列的时间片,将其移到下一个队列 if (i === timeQuantum[currentProcess.priority] - 1) { if (currentProcess.priority < queues.length - 1) { currentProcess.priority++; } queues[currentProcess.priority].push(currentProcess); break; } } } // 返回完成执行的进程 return completedProcesses; } // 测试 const processes = [ new Process('P1', 0, 20), new Process('P2', 1, 10), new Process('P3', 2, 30), new Process('P4', 0, 15), new Process('P5', 1, 5), ]; const completedProcesses = multiLevelFeedbackQueue(processes); console.log(completedProcesses); ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值