求无序队列中第k小的元素-java

该博客详细介绍了如何使用Java中的优先队列数据结构来找到无序整数序列中的第k小元素。通过构造优先队列并依次插入数据,然后进行k次出队操作,可以高效地找到目标值。算法的时间复杂度为O(N)。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

求无序队列中第k小的元素

一、问题描述
说明:
  - 求无序序列中第K小的元素;编写一个实验程序,利用STL中的priority_queue(优先队列)求出一个无序整数序列中第K小的元素;
二、算法设计分析
1. 构造优先队列的数据结构
image-20210530095419375
2.将模拟数据加入优先队列中
**数据的优先级,就是数据本身的大小**
image-20210530095515876
3. 连续next k次 既目标值:
三、关键代码解释或注释
package DataStructures.queue;

import java.util.Scanner;

public class PriorityQueueDemo {
    public static void main(String[] args) {
        // 目的 求无序序列中第K小的元素;编写一个实验程序,利用STL中的priority_queue(优先队列)求出一个无序整数序列中第K小的元素;
        // 实现步骤:
        // 1. 将无序的数据 插入优先队列 中<按照由小到大顺序>
        // 2. 取出第k小的元素-既连续next k次
        PriorityQueue priorityQueue = new PriorityQueue();
        // 定义无序数据:
         int unorderData[] = {1,2,9,6,46,67,23,4,234};
         for (int i = 0;i < unorderData.length;i++){
             priorityQueue.push(unorderData[i],unorderData[i]);
         }
         priorityQueue.list();
        System.out.println("请输入 k");
        Scanner sc = new Scanner(System.in);
        int nextInt = sc.nextInt();
        priorityQueue.list(nextInt);
    }
}

class PriorityQueue {
    class Node {
        int value;     // 节点的值
        int priority;  // 节点的优先级
        Node next;     // 节点下一个位置

        public Node(int value, int priority) {
            this.value = value;
            this.priority = priority;
        }
    }

        Node head = null;

        // push :插入一个新的元素
        public void push(int value, int priority) {
            if (head == null) {
                head = new Node(value, priority);
                return;
            }
            // 过度节点
            Node cur = head;
            Node newNode = new Node(value, priority);
            if (head.priority > priority) {
                newNode.next = head;
                this.head = newNode;
            } else {
                while (cur.next != null && cur.next.priority < priority) {
                    cur = cur.next;
                }
                newNode.next = cur.next;
                cur.next = newNode;
            }
        }

        // pop :将优先级最高的元素弹出(删除)
        public Node pop() {
            if (head == null) {
                return null;
            }
            Node temp = head;
            head = head.next;
            return temp;
        }

    // 显示链表[遍历]
    public void list() {
        // 判断链表是否为空
        if (head == null) {
            System.out.println("链表为空");
            return;
        }
        // 因为 head 节点不能动,因此我们需要一个辅助遍历 temp 获得 头节点
        Node temp = head;
        while (true) {
            // 判断是否到链表最后
            if (temp == null){
                break;
            }
            // 输出节点的信息
            System.out.println(temp.value);
            // 将temp后移,一定小心
            temp = temp.next;
        }
    }

    // 显示链表[遍历]
    public void list(int k) {
        // 判断链表是否为空
        if (head == null) {
            System.out.println("链表为空");
            return;
        }
        // 因为 head 节点不能动,因此我们需要一个辅助遍历 temp 获得 头节点
        Node temp = head;
        for (int i=1;i<k;i++){
            // 判断是否到链表最后
            if (temp == null){
                System.out.println("没有第 k 小的元素");
                break;
            }
            // 输出节点的信息
            // 将temp后移,一定小心
            temp = temp.next;
        }
        System.out.printf("第%d小的元素是:%d",k,temp.value);

    }
        // peek: 查看优先级最高的元素数组
        public Node peek() {
            return head;
        }
        public boolean isEmpty() {
            return head == null;
        }

}
四、算法运行结果

image-20210530101309250

五、时间复杂度分析
由代码可知:无序数据插入优先列表中使用一个循环,遍历出目标值 k 又一循环,当数量级为 N 时
  时间复杂度---》O(N)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值