算法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(resultj<resultj-1){

int tmp=resultj;

resultj=resultj-1;

resultj-1=tmp;

flag=1;

}

}

if(flag==0)

break;

}

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

cout<<resulti<<" ";

}

}

快速排序:

#include <stdio.h>

#include <vector>

#include <iostream>

using namespace std;

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

int tmp=alow;

while(low<high){

for(;low<high&&ahigh>tmp;high--){

}

swap(alow,ahigh);

for(;low<high&&alow<=tmp;low++){

}

swap(ahigh,alow);

}

alow=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<<resulti<<" ";

}

}

相关推荐
linux-hzh7 分钟前
百日算法修炼 · Day 12
算法·深度优先
ZJU_统一阿萨姆9 分钟前
【推理优化进阶】图编译与运行时:动态形状、CUDA Graph 与内存规划
开发语言·人工智能·语言模型·架构·开源
@呱呱爱学习10 分钟前
C++大成之路_ STL容器_ Vector
开发语言·c++
额额额对了17 分钟前
数据结构:二叉树
c语言·数据结构·算法
码匠许师傅28 分钟前
【C++ 面试真题】19. 聊聊 C++ 的迭代器
开发语言·c++·面试
iNeuOS工业互联网31 分钟前
iNeuOS_Vision_视觉分析,增加SAM3模型对实例分割和目标检测AI自动标注图片样本功能
人工智能·算法·目标检测
AI情绪识别开源32 分钟前
检信ALLEMOTION VibrationAI 2.4.0 12维度情绪识别开源源代码
开发语言·python
Dovis(誓平步青云)33 分钟前
DevEco Studio 6.1.1 Windows 安装实录:从下载校验到首次启动
android·开发语言·数据库·人工智能·windows·harmonyos
qeen8740 分钟前
【数据结构】红黑树的算法原理解析与实现
开发语言·数据结构·c++·算法·红黑树
码匠许师傅1 小时前
【C++ 面试真题】20. 聊聊 C++ 的标准库常用算法
c++·算法·面试