> For the complete documentation index, see [llms.txt](https://blog.yushunchen.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://blog.yushunchen.com/algo/sorting/sort-integers.md).

# Sort Integers

{% embed url="<https://www.lintcode.com/problem/463/>" %}

## Solution 1 (Java)

```java
public class Solution {
    /**
     * @param A: an integer array
     * @return: nothing
     */
    public void sortIntegers(int[] A) {
        for (int i = 0; i < A.length; i++) {
            int minIndex = i;
            for (int j = i; j < A.length; j++) {
                if (A[j] < A[minIndex]) {
                    minIndex = j;
                }
            }
            int temp = A[i];
            A[i] = A[minIndex];
            A[minIndex] = temp;
        }
    }
}
```

### Notes

* **Selection sort**
* Each time, find the minimum number and swap it with the first number of the unsorted list.

## Solution 2 (Java)

```java
public class Solution {
    /**
     * @param A: an integer array
     * @return: nothing
     */
    public void sortIntegers(int[] A) {
        for (int i = 0; i < A.length; i++) {
            int num = A[i];
            int j = i - 1;
            while (j >= 0 && A[j] > num) {
                A[j + 1] = A[j];
                j--;
            }
            A[j + 1] = num;
        }
    }
}
```

### Notes

* **Insertion sort**
* This is similar to playing card and sorting them. If the current number is larger than its previous number, we make space and move it to a proper position to the left.

## Solution 3 (Java)

```java
public class Solution {
    /**
     * @param A: an integer array
     * @return: nothing
     */
    public void sortIntegers(int[] A) {
        while (true) {
            boolean swap = false;
            for (int i = 0; i < A.length - 1; i++) {
                if (A[i] > A[i + 1]) {
                    int temp = A[i];
                    A[i] = A[i + 1];
                    A[i + 1] = temp;
                    swap = true;
                }
            }
            if (!swap) break;
        }
    }
}
```

### Notes

* Bubble sort
* We look at the numbers in pairs. If there is an inversion in the pair, we swap the two numbers. We keep swapping until we cannot swap.


---

# 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://blog.yushunchen.com/algo/sorting/sort-integers.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.
