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

}

}

相关推荐
炽烈小老头3 分钟前
【每天学习一点算法 2025/12/25】爬楼梯
学习·算法·动态规划
睡醒了叭4 分钟前
图像分割-传统算法-阈值分割原理与实践
opencv·算法·计算机视觉
夜泉_ly6 分钟前
期末速通 -Java程序设计基础 -理论
java·开发语言
m0_6113493113 分钟前
什么是副作用(Side Effects)
开发语言·前端·javascript
CoovallyAIHub15 分钟前
200亿美元“反向收购雇佣”?老黄天价应对谷歌TPU压力
深度学习·算法·计算机视觉
oioihoii16 分钟前
C++多线程中join与detach机制深度解析
java·jvm·c++
落尘29817 分钟前
Catlass 模板库调试调优经验与踩坑记录
算法
ytttr87317 分钟前
叠前同步反演纵波速度、横波速度和密度三参数
算法
妮妮分享22 分钟前
维智地图如何集成
开发语言·ios·swift
初圣魔门首席弟子26 分钟前
智能指针使用bug
c++·算法