冒泡排序
Algorithm | Time Complexity | Space Complexity | ||
---|---|---|---|---|
Best | Average | Worst | Worst | |
Bubble Sort | Ω(n) | Θ(n^2) | O(n^2) | O(1) |
//c++11: swap() defined in header <algorithm>
void bubbleSort(vector<int> &nums){
int range = nums.size()-1;
for (int i = 0; i < range; ++i){
for (int j = 0; j < range-i; ++j){
if (nums[j] > nums[j+1]){
swap(nums[j], nums[j+1]);
}
}
}
}