include<iostream>
#include<ctime>
using namespace std;
#include<vector>
// 稳定 时间复杂度 O(n^2)
void bubbleSort(vector<int> &dataVec)
{
int size = dataVec.size();
for (int i=1;i<size;i++)
{
bool ifSwapData = false;
for (int j=0;j<size-i;j++)
{
if (dataVec[j]>dataVec[j+1])
{
int temp = dataVec[j];
dataVec[j] = dataVec[j + 1];
dataVec[j + 1] = temp;
ifSwapData = true;
//or
//swap(dataVec[j],dataVec[j+1]);
}
}
if (!ifSwapData)
{
break;
}
}
return;
}
手撕一个冒泡排序
最新推荐文章于 2024-10-02 22:45:05 发布