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

}

}

相关推荐
blog_wanghao2 小时前
基于Qt的串口调试助手
开发语言·qt
CS创新实验室2 小时前
CS实验室行业报告:AI算法工程师就业分析报告
人工智能·算法
XiYang-DING3 小时前
【LeetCode】Hash | 136.只出现一次的数字
算法·leetcode·哈希算法
wayz113 小时前
Day 3:逻辑回归与分类预测
算法·分类·逻辑回归
tankeven3 小时前
HJ176 【模板】滑动窗口
c++·算法
果汁华3 小时前
Typer:基于类型提示的现代Python CLI框架
开发语言·网络·python
赵药师3 小时前
多进程-生产者消费者C++实现
java·开发语言·jvm
OxyTheCrack3 小时前
【C++】一文详解C++智能指针自定义删除器(以Redis连接池为例)
c++·redis
雾岛听蓝3 小时前
Linux线程基础
linux·开发语言·经验分享
zhangzeyuaaa3 小时前
Python 异常机制深度剖析
开发语言·python