diff --git a/DSA Essentials Solutions/Queues/SortAQueueWithConstantSpace.cpp b/DSA Essentials Solutions/Queues/SortAQueueWithConstantSpace.cpp new file mode 100644 index 0000000..3b65a22 --- /dev/null +++ b/DSA Essentials Solutions/Queues/SortAQueueWithConstantSpace.cpp @@ -0,0 +1,48 @@ +#include +using namespace std; + +int minIndex(queue &q, int sortedIndex) +{ + int min_index = -1; + int min_val = INT_MAX; + int n = q.size(); + for (int i=0; i &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); +} + +queue sortqueue(queue &q) +{ + for (int i = 1; i <= q.size(); i++) + { + int min_index = minIndex(q, q.size() - i); + insertMinToRear(q, min_index); + } + return q; +}