参考别人的自己总结的。本人水平有限,有错误麻烦指出。
两种方法:(请忽略冒泡排序)
第一种:
#include <iostream>
#include <vector>
#include <time.h>
#include <sys/timeb.h>
#include <Windows.h>
using namespace std;
void bubble_sort1(vector<int> &t)
{
int length = t.size();
for (int i = 0; i < length - 1; i++)
{
for (int j = 0; j < length - 1 - i; j++)
{
if (t[j] > t[j + 1])
{
int temp = t[j];
t[j] = t[j + 1];
t[j + 1] = temp;
}
}
}
}
//升级版,增加了flag判断某一循环有没有进行交换,如果没有直接结束。
void bubble_sort2(vector<int> &t)
{
int length = t.size();
for (int i = 0; i < length - 1; i++)
{
bool flag = false;
for (int j = 0; j < length - 1 - i; j++)
{
if (t[j] > t[j + 1])
{
int temp = t[j];
t[j] = t[j + 1];
t[j + 1] = temp;