通过两个指针, 来加快一趟冒泡(本质多加了个循环,两个指针)
#include <iostream>
#include <cstring>
using namespace std;
const int N = 100100;
int q[N];
void bubble_sort(int q[], int m)
{
int low = 0, high = m - 1;
for(int i = 0; i < m; i++)
{
bool flag = false;
for(int j = low; j < m - 1; j++)
{
if (q[j] > q[j + 1]) swap(q[j], q[j + 1]), flag = true;
}
high--;
for(int j = high; j >= 1; j--)
{
if (q[j - 1] > q[j]) swap(q[j - 1], q[j]), flag = true;
}
low++;
if (!flag) return;
}
}
int main()
{
int m;
scanf("%d", &m);
for(int i = 0; i < m; i++) scanf("%d", &q[i]);
bubble_sort(q, m);
for(int i = 0; i < m; i++) printf("%d ", q[i]);
}