![](https://i-blog.csdnimg.cn/direct/e75251dc80d04acbad080b9fb27fbd0e.png)
一丶插入排序
![](https://i-blog.csdnimg.cn/direct/cecdbfade11c40f4ad363daad54277b6.png)
二丶希尔排序
![](https://i-blog.csdnimg.cn/direct/9ac2fc792ad345f0a8120eaa86cba26f.png)
三丶冒泡排序
1.0数组版
![](https://i-blog.csdnimg.cn/direct/788ef6ac246d428fa8a96d5fc8685934.png)
2.0链表版
![](https://i-blog.csdnimg.cn/direct/8300e37d289d466e8e9cfe6f6a21b5ef.png)
源代码
#include<iostream>
#include<vector>
#include<ctime>
#include<random>
#include<list>
#include<functional>
#include<algorithm>
//插入排序
void InsertSort(std::vector<int>&array,int n){
int i , j , temp;
for(i=1;i<n;i++){
if(array[i]<array[i-1]){
temp = array[i];
for(j = i-1;j>=0&&array[j]>temp;j--){
array[j+1] = array[j];
}
array[j+1] = temp;
}
}
}
//希尔排序
void ShellSort(std::vector<int>&array,int n){
int d,i,j,temp;
for(d = n/2;d >=1;d=d/2){
for(i = d;i<n;++i){
temp = array[i];
for(j = i-d; j>= 0&&temp<array[j];j-=d){
array[j+d] = array[j];
}
array[j+d] = temp;
}
}
}
//冒泡排序
void BubbleSort_array(std::vector<int>&array,int n){
// //1.0
// int i,num;
// //用num来确定每次排序比较次数,每次循环,比较次数-1,因为,每次排序都将最小数置于最前方,因此开头不需要对比
// for(num = n-1;num>0;num--){
// int t = num;
// bool change = false;//标志,当某次排序不移动任意一位元素时,整体有序
// for(i = n-1;t>0;i--,t--){
// if(array[i]<array[i-1]){
// std::swap(array[i-1],array[i]);
// change = true;
// }
// }
// if(change == false)
// break;
// }
//2.0
int i , j;
for(i = 0;i<n-1;i++){
bool flag = false;
for(j = n-1;j>i;j--){
if(array[j-1]>array[j]){
std::swap(array[j-1],array[j]);
flag = true;
}
}
if(flag==false)
return ;
}
}
bool compareison(const int a,const int b){
return a>b;
}
void BubbleSort_list(std::list<int>&list,int n){
bool swapped;
do{
swapped = false;
auto it = list.begin();
while(it!=list.end()&&std::next(it)!=list.end()){
auto next = std::next(it);
if(*it>*next){
std::swap(*it,*next);
swapped = true;
}
it++;
}
}while(swapped);
}
int main()
{
std::vector<int>array;
std::uniform_int_distribution<int>u(1,50);
std::default_random_engine e;
std::list<int>L;
e.seed(time(0));
L.push_back(u(e));
L.push_back(u(e));
L.push_back(u(e));
L.push_back(u(e));
L.push_back(u(e));
L.push_back(u(e));
L.push_back(u(e));
L.push_back(u(e));
L.push_back(u(e));
L.push_back(u(e));
L.push_back(u(e));
// BubbleSort_list(L,L.size());
L.sort(compareison);
array.push_back(u(e));
array.push_back(u(e));
array.push_back(u(e));
array.push_back(u(e));
array.push_back(u(e));
array.push_back(u(e));
array.push_back(u(e));
array.push_back(u(e));
array.push_back(u(e));
array.push_back(u(e));
array.push_back(u(e));
// // InsertSort(array,array.size());
// // ShellSort(array,array.size());
BubbleSort_array(array,array.size());
for(auto p:array){
std::cout << p <<std::endl;
}
for(auto p : L){
std::cout << p<<std::endl;
}
return 0;
}