简体   繁体   English

排序数组优先级队列

[英]Sorted Array Priority Queue

I am implementing a sorted array priority queue and sorting upon insert. 我实现了一个排序的数组优先级队列,并在插入时进行排序。 For some reason, the queue is not coming out completely sorted. 由于某种原因,队列未完全排序出来。 Am I implementing it correctly so far and how can i fix it from here? 到目前为止,我是否可以正确实施?如何从这里修复?

void insertInt(int numToIns)
{
    if (total == 0)
    {
        q[0] = numToIns;
        minimum = numToIns;
    }
    else if (total != 0)
    {
        if (numToIns <= minimum)
        {
            minimum = numToIns;
            for (int move = total; move >= 0; --move)
            {
                q[move + 1] = q[move];
            }
            q[0] = numToIns;
        }
        else if (numToIns < q[total])
        {
            bool numFound = false;
            for (int j = 0; numFound != true; ++j)
            {
                if (numToIns <= q[j])
                {
                    numFound = true;
                    for (int move = total; move >= j; --move)
                    {
                        q[move + 1] = q[move];
                    }
                    q[j] = numToIns;
                }
            }

        }
        else if (numToIns >= q[total - 1])
        {
            q[total] = numToIns;
        }
    }
    ++total;
}

Not sure what your current code looks like, but that should do it: 不知道您当前的代码是什么样,但是应该这样做:

#include <ctime>
#include <cstdlib>
#include <cstdio>
#include <iostream>

std::size_t total;
int q[100];
int minimum;

void print()
{
    for (std::size_t i{}; i < total; ++i)
        std::cout << q[i] << ' ';
    std::cout.put('\n');
}

void insertInt(int numToIns)
{
    if (total == 0)
    {
        q[0] = numToIns;
        minimum = numToIns;
    }
    else if (total != 0)
    {
        if (numToIns <= minimum)
        {
            minimum = numToIns;
            for (std::size_t move{ total }; move >= 0; --move)
            {
                q[move + 1] = q[move];
            }
            q[0] = numToIns;
        }
        else if (numToIns < q[total - 1])
        {
            bool numFound = false;
            for (std::size_t j{}; !numFound; ++j)
            {
                if (numToIns <= q[j])
                {
                    numFound = true;
                    for (std::size_t move{ total - 1 }; move >= j; --move)
                    {
                        q[move + 1] = q[move];
                    }
                    q[j] = numToIns;
                }
            }

        }
        else if (numToIns >= q[total - 1])
        {
            q[total] = numToIns;
        }
    }
    ++total;
}

int main()
{
    std::srand(static_cast<unsigned>(std::time(nullptr)));

    for (int i{}; i < 20; ++i) {
        insertInt(std::rand() % 20 + 1);
        print();
    }
}

Note that three kitties died because of me writing that code :( 请注意,由于我编写了该代码,三只小猫死了:(

No bs version: 没有bs版本:

void insertInt(int num)
{
    std::size_t pos{};
    while (pos < total && num > q[pos])
        ++pos;

    for (std::size_t k{ total }; k > pos; --k)
        q[k] = q[k - 1];

    q[pos] = num;
    ++total;
}

To avoid going through the array if the number to be inserted is bigger than the last element one could add one special case: 为了避免遍历数组,如果要插入的数字大于最后一个元素,可以添加一种特殊情况:

void insertInt(int num)
{
    if (num > q[total - 1]) {
        q[total++] = num;
        return;
    }

    std::size_t pos{};
    while (pos < total && num > q[pos])
        ++pos;

    for (std::size_t k{ total }; k > pos; --k)
        q[k] = q[k - 1];

    q[pos] = num;
    ++total;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM