使用c/c++实现冒泡排序算法和比较排序算法的效率
实现目的
通过对比冒泡法排序和普通的比较排序,阐述冒泡法的排序效率,从而达到更好的掌握冒泡法的目的
冒泡法排序和比较排序的实现方法
普通比较排序算法
#include <iostream>
#include <vector>
using namespace std;
/**
* @brief normal_sort
*
* normal compare algorithm to sort data sequence form smallest ---> biggest
*
* @param vec
*/
void normal_sort(vector<int> &vec)
{
int i, j;
int size;
int temp;
size = vec.size();
for(auto v : vec)
{
cout << v << " ";
}
cout << endl;
cout << endl;
for(i=0; i<size-1; ++i)
{
for(j=i+1; j<size; ++j)
{
if(vec.at(i) > vec.at(j) )
{
temp = vec.at(i);
vec.at(i) = vec.at(j);
vec.at(j) = temp;
}
}
for(auto v : vec)
{
cout << v << " ";
}
cout << endl;
}
}
int main(int argc, char **argv)
{
vector<int> intvec;
vector<int> intvec1;
vector<int>().swap(intvec);
vector<int>().swap(intvec1);
intvec.push_back(4);
intvec.push_back(5);
intvec.push_back(7);
intvec.push_back(1);
intvec.push_back(8);
intvec.push_back(3);
intvec1.push_back(1);
intvec1.push_back(2);
intvec1.push_back(3);
intvec1.push_back(4);
intvec1.push_back(5);
intvec1.push_back(6);
cout << "num: the bigger is down to bottom, and the smaller is up to the top" << endl;
normal_sort(intvec);
cout << "num1: if sequence is alredy sort, just run once time" << endl;
normal_sort(intvec1);
return 0;
}
执行结果
tony@Tech:~/linux/fight$ ./normal_sort
num: the bigger is down to bottom, and the smaller is up to the top
4 5 7 1 8 3
1 5 7 4 8 3
1 3 7 5 8 4
1 3 4 7 8 5
1 3 4 5 8 7
1 3 4 5 7 8
num1: if sequence is alredy sort, just run once time
1 2 3 4 5 6
1 2 3 4 5 6
1 2 3 4 5 6
1 2 3 4 5 6
1 2 3 4 5 6
1 2 3 4 5 6
冒泡排序算法
#include <iostream>
#include <vector>
using namespace std;
/**
* @brief bubble_sort
*
* bubble algorithm to sort data sequence form smallest ---> biggest
*
* @param vec
*/
void bubble_sort(vector<int> &vec)
{
bool flag;
int i, j;
int size;
int temp;
size = vec.size();
for(auto v : vec)
{
cout << v << " ";
}
cout << endl;
cout << endl;
for(i=size-1, flag=true; i>1 && flag; --i)
{
flag = false;
for(j=0; j<i; ++j)
{
if(vec.at(j) > vec.at(j+1) )
{
temp = vec.at(j);
vec.at(j) = vec.at(j+1);
vec.at(j+1) = temp;
flag = true;
}
}
for(auto v : vec)
{
cout << v << " ";
}
cout << endl;
}
}
int main(int argc, char **argv)
{
vector<int> intvec;
vector<int> intvec1;
vector<int>().swap(intvec);
vector<int>().swap(intvec1);
intvec.push_back(4);
intvec.push_back(5);
intvec.push_back(7);
intvec.push_back(1);
intvec.push_back(8);
intvec.push_back(3);
intvec1.push_back(1);
intvec1.push_back(2);
intvec1.push_back(3);
intvec1.push_back(4);
intvec1.push_back(5);
intvec1.push_back(6);
cout << "num: the bigger is down to bottom, and the smaller is up to the top" << endl;
bubble_sort(intvec);
cout << "num1: if sequence is alredy sort, just run once time" << endl;
bubble_sort(intvec1);
return 0;
}
执行结果
tony@Tech:~/linux/fight$ ./bubble_sort
num: the bigger is down to bottom, and the smaller is up to the top
4 5 7 1 8 3
4 5 1 7 3 8
4 1 5 3 7 8
1 4 3 5 7 8
1 3 4 5 7 8
num1: if sequence is alredy sort, just run once time
1 2 3 4 5 6
1 2 3 4 5 6
tony@Tech:~/linux/fight$ gedit normal_sort.cpp
tony@Tech:~/linux/fight$
结论
通过比较两种算法的差异得知:
-
冒泡排序算法:先把最大的值沉到最低下,最小的值一步一步以冒泡的方式向前移动,而如果已经排序好的序列,只需要执行一次,效率是最高的。
-
比较排序:不管原来是否排好序,都是按照固定的次数排序,执行次数是固定的
c o u n t e r = n ∗ ( n − 1 ) 2 counter = \frac{n*(n-1)}{2} counter=2n∗(n−1)