算法DAY66

今天是去实现了一下排序算法

先是交换排序

最简单的冒泡排序:

#include <stdio.h>

#include <vector>

#include <iostream>

using namespace std;

int main(){

int n;

vector<int> result;

while(cin>>n){

result.push_back(n);

}

int a=0;

for(int i=0;i<result.size();i++){

int flag=0;

for(int j=result.size()-1;j>i;j--){

if(result[j]<result[j-1]){

int tmp=result[j];

result[j]=result[j-1];

result[j-1]=tmp;

flag=1;

}

}

if(flag==0)

break;

}

for(int i=0;i<result.size();i++){

cout<<result[i]<<" ";

}

}

快速排序:

#include <stdio.h>

#include <vector>

#include <iostream>

using namespace std;

int pattern(vector<int> &a,int low,int high){

int tmp=a[low];

while(low<high){

for(;low<high&&a[high]>tmp;high--){

}

swap(a[low],a[high]);

for(;low<high&&a[low]<=tmp;low++){

}

swap(a[high],a[low]);

}

a[low]=tmp;

return low;

}

void quicksort(vector<int> &a,int low,int high){

if(low>=high) return;

int tmp=pattern(a,low,high);

quicksort(a,low,tmp-1);

quicksort(a,tmp+1,high);

}

int main(){

int n;

vector<int> result;

while(cin>>n){

result.push_back(n);

}

quicksort(result,0,result.size()-1);

for(int i=0;i<result.size();i++){

cout<<result[i]<<" ";

}

}

相关推荐
努力冲冲20 分钟前
常用排序算法
java·算法·排序算法
芥子沫1 小时前
VSCode添加Python、Java注释技巧、模板
开发语言·前端·javascript
小陈又菜2 小时前
【C++】类和对象--类中6个默认成员函数(2) --运算符重载
开发语言·c++·运算符重载
yuezhilangniao2 小时前
关于开发语言的一些效率 从堆栈角度理解一部分c java go python
java·c语言·开发语言
Ares-Wang2 小时前
Node.js 》》bcryptjs 加密
开发语言·javascript·node.js
vvilkim3 小时前
深入理解Java访问修饰符:封装的艺术
java·开发语言
最爱吃南瓜3 小时前
JS逆向实战案例之----【通姆】252个webpack模块自吐
开发语言·javascript·爬虫·webpack·js逆向·算法模拟
夜斗小神社3 小时前
【LeetCode 热题 100】(六)矩阵
算法·leetcode·矩阵
天地一流殇4 小时前
SimBA算法实现过程
深度学习·算法·对抗攻击·黑盒
hqxstudying4 小时前
java分布式定时任务
java·开发语言·分布式