cpp-interview/Algorithm/BubbleSort.h
2018-02-11 00:20:34 +08:00

13 lines
265 B
C

// 冒泡排序
void BubbleSort(vector<int>& v) {
int temp;
for (int i = 0; i < v.size() - 1; ++i) {
for (int j = 0; j < v.size() - 1 - i; ++j) {
if (v[j] > v[j + 1]) { // 从小到大
temp = v[j];
v[j] = v[j + 1];
v[j + 1] = temp;
}
}
}
}