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

}

}

相关推荐
爱理财的程序媛16 小时前
openclaw 盯盘实践
算法
端平入洛17 小时前
auto有时不auto
c++
MobotStone20 小时前
Google发布Nano Banana 2:更快更便宜,图片生成能力全面升级
算法
颜酱1 天前
队列练习系列:从基础到进阶的完整实现
javascript·后端·算法
用户5757303346241 天前
两数之和:从 JSON 对象到 Map,大厂面试官到底在考察什么?
算法
程序猿追1 天前
“马”上行动:手把手教你基于灵珠平台打造春节“全能数字管家”
算法
郑州光合科技余经理2 天前
代码展示:PHP搭建海外版外卖系统源码解析
java·开发语言·前端·后端·系统架构·uni-app·php
feifeigo1232 天前
matlab画图工具
开发语言·matlab
dustcell.2 天前
haproxy七层代理
java·开发语言·前端
norlan_jame2 天前
C-PHY与D-PHY差异
c语言·开发语言