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

# Bubble Sort

## Algorithm

```cpp
// OJ: https://leetcode.com/problems/sort-an-array/
// Author: github.com/lzl124631x
// Time: O(N^2)
// Space: O(1)
class Solution {
public:
    vector<int> sortArray(vector<int>& nums) {
        for (int i = 0, N = nums.size(); i < N; ++i) {
            for (int j = N - 1; j > i; --j) {
                if (nums[j] < nums[j - 1]) swap(nums[j], nums[j - 1]);
            }
        }
        return nums;
    }
};
```
