# 氣泡排序法（Bubble Sort）

```php
function bubbleSort($nums) {
    if (count($nums) <= 1) {
        return $nums;
    }
    // 為每個值去比對所有值
    foreach($nums as $currentKey => $currentNum) {
        foreach($nums as $nextKey => $nextNum) {
            // 如果 `當前值` 比 `被比較的值` 大，則與該值交換位置，已達到遞增排序。如要遞減，則改成小於即可。
            if ($nums[$currentKey] < $nums[$nextKey]) {
                [$nums[$currentKey], $nums[$nextKey]] = [$nums[$nextKey], $nums[$currentKey]];
            }
        }
    }

    return $nums;
}

// 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
print_r(bubbleSort([3, 2, 1, 5, 4, 6, 7, 8, 9, 10]));
```


---

# 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://algorithm.national-cat.me/sort/bubble-sort.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.
