算法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]<<" ";

}

}

相关推荐
ghie9090几秒前
基于学习的模型预测控制(LBMPC)MATLAB实现指南
开发语言·学习·matlab
ysa0510303 分钟前
斐波那契上斐波那契【矩阵快速幂】
数据结构·c++·笔记·算法
咚为4 分钟前
Rust 经典面试题255道
开发语言·面试·rust
十六年开源服务商11 分钟前
家庭装修公司网站方案策划2026
java·开发语言
Mr_Xuhhh13 分钟前
深入理解Java高级特性:反射、枚举与Lambda表达式实战指南
开发语言·python
XiYang-DING16 分钟前
【Java】TOP-K问题
java·开发语言
CHANG_THE_WORLD17 分钟前
模拟解析:宽度数组 `[1,2,1]`,10个条目的 XRef 流
java·前端·算法
枫叶丹417 分钟前
【HarmonyOS 6.0】Navigation组件新特性
开发语言·华为·harmonyos
格林威21 分钟前
GigE Vision 多相机同步终极检查清单(可直接用于项目部署)
开发语言·人工智能·数码相机·机器学习·计算机视觉·视觉检测·工业相机
lixinnnn.21 分钟前
多源BFS:矩阵距离
算法·宽度优先