# Sort Using Two Stacks

Write a program to sort a stack using additional stacks but no other data structures

{% tabs %}
{% tab title="Scala" %}

```scala
import scala.collection.mutable.Stack

/**
  * 3.6
  * @param s1
  * @return
  */
def sort(s1:Stack[Int]): Stack[Int] = {
  val s2 = Stack[Int]()
  while (!s1.isEmpty) {
    val tmp = s1.pop()
    while (!s2.isEmpty && s2.head < tmp) {
      s1.push(s2.pop())
    }
    s2.push(tmp)
  }
  s2
}

val s = Stack[Int]()
s.push(3)
s.push(9)
s.push(5)

val s3 = sort(s)
while (!s3.isEmpty) {
  println(s3.pop())
}
```

{% endtab %}

{% tab title="Second Tab" %}

{% endtab %}
{% endtabs %}


---

# Agent Instructions: 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:

```
GET https://blog.bernardw.com/problems/sort-using-two-stacks.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
