-
Notifications
You must be signed in to change notification settings - Fork 10
/
queuesorting.cpp
82 lines (75 loc) · 1.75 KB
/
queuesorting.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// C++ program to implement sorting a
// queue data structure
#include <bits/stdc++.h>
using namespace std;
// Queue elements after sortedIndex are
// already sorted. This function returns
// index of minimum element from front to
// sortedIndex
int minIndex(queue<int> &q, int sortedIndex)
{
int min_index = -1;
int min_val = INT_MAX;
int n = q.size();
for (int i=0; i<n; i++)
{
int curr = q.front();
q.pop(); // This is dequeue() in C++ STL
// we add the condition i <= sortedIndex
// because we don't want to traverse
// on the sorted part of the queue,
// which is the right part.
if (curr <= min_val && i <= sortedIndex)
{
min_index = i;
min_val = curr;
}
q.push(curr); // This is enqueue() in
// C++ STL
}
return min_index;
}
// Moves given minimum element to rear of
// queue
void insertMinToRear(queue<int> &q, int min_index)
{
int min_val;
int n = q.size();
for (int i = 0; i < n; i++)
{
int curr = q.front();
q.pop();
if (i != min_index)
q.push(curr);
else
min_val = curr;
}
q.push(min_val);
}
void sortQueue(queue<int> &q)
{
for (int i = 1; i <= q.size(); i++)
{
int min_index = minIndex(q, q.size() - i);
insertMinToRear(q, min_index);
}
}
// driver code
int main()
{
queue<int> q;
q.push(30);
q.push(11);
q.push(15);
q.push(4);
// Sort queue
sortQueue(q);
// Print sorted queue
while (q.empty() == false)
{
cout << q.front() << " ";
q.pop();
}
cout << endl;
return 0;
}