Priority scheduling algorithm using std::vector I'm learning priority scheduling algorithm and trying to implement it using std::vector. But the current performance of my code that's partially implemented is not that good since I'm performing sort operation every time I add a new process to std::vector list. Can somebody suggest ways I can improve my current partially implemented algorithm. This is my code:
#include &lt;iostream&gt;
#include &lt;vector&gt;
#include &lt;utility&gt;
#include &lt;algorithm&gt;

void print_list(const std::vector&lt;std::pair&lt;int, int&gt;&gt;&amp; pair_list) {
    for (const auto&amp; pa : pair_list)
        std::cout &lt;&lt; &quot;process_ID: &quot; &lt;&lt; pa.first &lt;&lt; &quot;, &quot; &lt;&lt; &quot;priority: &quot; &lt;&lt; pa.second &lt;&lt; &quot;\n&quot;;
    std::cout &lt;&lt; std::endl;
}

void add_pro(int pro_ID, int pro_pri, std::vector&lt;std::pair&lt;int, int&gt;&gt;&amp; pair_list) {
    pair_list.emplace_back(pro_ID, pro_pri);

    std::sort(pair_list.begin(), pair_list.end(), [=](std::pair&lt;int, int&gt;&amp; a, std::pair&lt;int, int&gt;&amp; b) {
        return a.second &lt; b.second;
    });
}

bool check_pro_is_present(int pro_ID, const std::vector&lt;std::pair&lt;int, int&gt;&gt;&amp; pair_list) {
    for (const auto&amp; pa : pair_list) {
        if (pa.first == pro_ID)
            return true;
    }

    return false;
}

bool is_higher_pri_pro_present(int pro_ID, const std::vector&lt;std::pair&lt;int, int&gt;&gt;&amp; pair_list) {
    if (pair_list.front().first == pro_ID)
        return false;

    return true;
}

int main() {
    std::vector&lt;int&gt; process_ID_list { 1345, 45, 5646, 52525, 55757, 23424, 68696, 55878, 667, 252 };
    std::vector&lt;int&gt; process_pri_list { 41, 334, 747, 41, 47, 7, 75, 544, 42, 6 };
    
    std::vector&lt;std::pair&lt;int, int&gt;&gt; pair_list;
    for (int i = 0; i &lt; process_ID_list.size(); ++i) {
        int new_pro_ID = process_ID_list.at(i);
        int new_pro_pri = process_pri_list.at(i);

        if (!check_pro_is_present(new_pro_ID, pair_list))
            add_pro(new_pro_ID, new_pro_pri, pair_list);
        else
            std::cout &lt;&lt; &quot;process is already in list&quot; &lt;&lt; std::endl;

        print_list(pair_list);
    }

    int query_pro_ID = 1345;
    std::cout &lt;&lt; std::boolalpha &lt;&lt; is_higher_pri_pro_present(query_pro_ID, pair_list) &lt;&lt; std::endl;

    return (EXIT_SUCCESS);
}
While comments have suggested more suitable types than std::vector, I'm going to assume that the use of this underlying type is a given, and suggest straightforward improvements without changing that.
The main performance concern is here:

void add_pro(int pro_ID, int pro_pri, std::vector&lt;std::pair&lt;int, int&gt;&gt;&amp; pair_list) {
    pair_list.emplace_back(pro_ID, pro_pri);

    std::sort(pair_list.begin(), pair_list.end(), [=](std::pair&lt;int, int&gt;&amp; a, std::pair&lt;int, int&gt;&amp; b) {
        return a.second &lt; b.second;
    });
}


It's wasteful to sort the vector every time we add an element, when we know it was sorted before we called this function.  Instead, we need to insert the new element in the correct position.
void add_pro(int pro_ID, int pro_pri, std::vector&lt;std::pair&lt;int, int&gt;&gt;&amp; pair_list)
{
    auto position = std::upper_bound(pair_list.begin(), pair_list.end(), pro_pri,
                                     [](auto pri, auto const&amp; ele) { return pri &lt; ele.second; });
    pair_list.emplace(position, pro_ID, pro_pri);
}


We use the type std::vector&lt;std::pair&lt;int, int&gt;&gt; a lot.  It's worth giving it a name:
using priority_queue = std::vector&lt;std::pair&lt;int, int&gt;&gt;;

That makes changing the code easier, e.g. if we decide that process ids actually need to be unsigned long or something.
However, I observe that every function takes one of these as an argument.  That suggests that we want a class with this vector as member, then convert those free functions to be members of that class.

Thank you for including the test program - that's always helpful in a review.  There's a couple of small points:

for (int i = 0; i &lt; process_ID_list.size(); ++i) {


We're mixing signed and unsigned types in this comparison (int and std::size_t).  Obviously we should change int i to std::size_t i - and enable more compiler warnings.

       std::cout &lt;&lt; &quot;process is already in list&quot; &lt;&lt; std::endl;


Prefer to use std::cerr for error messages.

The pattern if (condition) return true; else return false; can simply be replaced with return condition.  So:
bool is_higher_pri_pro_present(int pro_ID, const std::vector&lt;std::pair&lt;int, int&gt;&gt;&amp; pair_list)
{
    return pair_list.front().first != pro_ID;
}


The loop in check_pro_is_present can be replaced with std::any_of:
bool check_pro_is_present(int pro_ID, const std::vector&lt;std::pair&lt;int, int&gt;&gt;&amp; pair_list)
{
    return std::any_of(pair_list.begin(), pair_list.end(),
                       [pro_ID](auto const&amp; e){ return e.first == pro_ID; });
}


Modified code
This is just a beginning - we'll probably want an initialiser-list constructor, and replace print() with operator&lt;&lt;(), but it's a start.
#include &lt;algorithm&gt;
#include &lt;iostream&gt;
#include &lt;utility&gt;
#include &lt;utility&gt;
#include &lt;vector&gt;

template&lt;typename T, typename Priority = int&gt;
class priority_queue
{
    std::vector&lt;std::pair&lt;T, Priority&gt;&gt; values = {};

public:
    void print(std::ostream &amp;os) const {
        for (const auto&amp; pa : values) {
            os &lt;&lt; &quot;process_ID: &quot; &lt;&lt; pa.first &lt;&lt; &quot;, &quot;
               &lt;&lt; &quot;priority: &quot; &lt;&lt; pa.second &lt;&lt; '\n';
        }
        std::cout &lt;&lt; '\n';
    }

    void add(T t, Priority pri) {
        auto const position
            = std::upper_bound(values.begin(), values.end(),
                               pri,
                               [](auto const&amp; pri, auto const&amp; ele) { return pri &lt; ele.second; });
        values.emplace(position, std::move(t), std::move(pri));
    }

    bool contains(int pro_ID) const
    {
        return std::any_of(values.begin(), values.end(),
                           [pro_ID](auto const&amp; e){ return e.first == pro_ID; });
    }

    bool has_higher(int pro_ID) const
    {
        return values.front().first != pro_ID;
    }
};

int main()
{
    const std::pair&lt;int,int&gt; processes[]
        = { { 1345, 41 }, { 45, 334 }, { 5646, 747 },
            { 52525, 41 }, { 55757, 47 },
            { 23424, 7 }, { 68696, 75 }, { 55878, 544 },
            { 667, 42 }, { 252, 6 } };

    priority_queue&lt;int, int&gt; queue;
    for (auto const&amp; p: processes) {
        if (queue.contains(p.first)) {
            std::cerr &lt;&lt; p.first &lt;&lt; &quot; is already in list\n&quot;;
        } else {
            queue.add(p.first, p.second);
        }
        queue.print(std::cout);
    }

    int query_pro_ID = 1345;
    std::cout &lt;&lt; std::boolalpha &lt;&lt; queue.has_higher(query_pro_ID) &lt;&lt; '\n';
}