mirror of
https://github.com/huihut/interview.git
synced 2024-03-22 13:10:48 +08:00
18 lines
364 B
C
18 lines
364 B
C
|
// 冒泡排序(跳过有序的改进版)
|
||
|
|
||
|
void BubbleSort_orderly(vector<int>& v) {
|
||
|
int temp;
|
||
|
bool orderly = false;
|
||
|
for (int i = 0; i < v.size() - 1 && !orderly; ++i) {
|
||
|
orderly = true;
|
||
|
for (int j = 0; j < v.size() - 1 - i; ++j) {
|
||
|
if (v[j] > v[j + 1])
|
||
|
{
|
||
|
orderly = false;
|
||
|
temp = v[j];
|
||
|
v[j] = v[j + 1];
|
||
|
v[j + 1] = temp;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|