> For the complete documentation index, see [llms.txt](https://syjohnson11.gitbook.io/leetcode/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://syjohnson11.gitbook.io/leetcode/11_sorting.md).

# 11. Sorting

## [8大排序算法图解](http://www.cricode.com/3212.html)

{% embed url="<https://visualgo.net/en/sorting>" %}

{% embed url="<https://leetcode.com/discuss/study-guide/1091763/Must-do-all-required-Sorting-Algorithms%3A-Complete-Guide>" %}

##

## Bubble sort

1. 2 for loops
2. every time compare and swap 2 adjacent elements
3. find the largest element and move to the end
4. Time: O(N^2) and space: O(1)

## Selection Sort

1. 2 for loops
2. every loop find the smallest element and move to the beginning
3. split the arrays into 2 parts, sorted subarray and unsorted subarray
4. Time: O(N^2) and space: O(1)

## [快排为何这么快（比较快排和堆排）](http://mindhacks.cn/2008/06/13/why-is-quicksort-so-quick/)

## Largest Number

* 给一个int\[] nums, 返回一个用nums里的num组成的最大的数
* custom sort: basically implement a String comparator to decide which String should come first during concatenation

```
public String largestNumber(int[] nums) {
        String[] arr = new String[nums.length];
        for (int i = 0; i < nums.length; i++) {
            arr[i] = String.valueOf(nums[i]);
        }
        Arrays.sort(arr, new Comparator<String>() {
                @Override
                public int compare(String a, String b) {
                    String s1 = a + b;
			        String s2 = b + a;
			        return s2.compareTo(s1); // reverse order here, so we can do append() later
                }
            });
        if (arr[0].charAt(0) == '0') {
            return "0"; // edge case, list of "0"
        }
        StringBuilder sb = new StringBuilder();
        for (String s : arr) {
            sb.append(s);
        }
        return sb.toString();
    }
```


---

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

```
GET https://syjohnson11.gitbook.io/leetcode/11_sorting.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.
