> For the complete documentation index, see [llms.txt](https://maksimdan.gitbook.io/interview-practice-problems/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://maksimdan.gitbook.io/interview-practice-problems/coding_practice_questions/stacks_and_queues/sort-stack.md).

# Sort Stack

**3.5 Sort Stack:** Write a program to sort a stack such that the smallest items are on the top. You can use an additional temporary stack, but you may not copy the elements into any other data structure (such as an array). The stack supports the following operations: push, pop, peek, and isEmpty.

* The main idea is to hold a temporary variable for the current element on the top, and then continuing pushing off elements from the second stack until you find an appropriate spot for the temp var.
* O(n^2) time, O(N) space

```cpp
template <typename T>
stack<T> sortStack(stack<T> s1) {
    if (s1.size() <= 1) return s1;

    // start us off
    stack<T> temp;
    int pop_count = 0;
    temp.push(s1.top());
    s1.pop();

    while (!s1.empty()) {
        // grab top element of s1
        T temp2 = s1.top();
        s1.pop();

        // find correct position in temp
        while (!temp.empty() && temp.top() < temp2) {
            s1.push(temp.top());
            temp.pop();
            pop_count++;
        }
        temp.push(temp2);

        // place back
        for (int i = 0; i < pop_count; i++) {
            temp.push(s1.top());
            s1.pop();
        }
        pop_count = 0;
    }

    return temp;
}


int main() {
    vector<int> v = { 1,5,2,10,3,3,3,3,3,100,1001,232 };
    stack<int> s;
    for (auto i : v) {
        s.push(i);
    }

    stack<int> s2 = sortStack(s);
    while (!s2.empty()) {
        cout << s2.top() << " ";
        s2.pop();
    }
    pause();
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://maksimdan.gitbook.io/interview-practice-problems/coding_practice_questions/stacks_and_queues/sort-stack.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
