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

}

}

相关推荐
cike_y1 分钟前
Spring整合Mybatis:dao层
java·开发语言·数据库·spring·mybatis
tobias.b3 分钟前
408真题解析-2009-10-数据结构-排序
数据结构·算法·排序算法·408考研·408真题·真题解析
松涛和鸣4 分钟前
45、无依赖信息查询系统(C语言+SQLite3+HTML)
c语言·开发语言·数据库·单片机·sqlite·html
Zachary_zlc7 分钟前
有向无环图检测算法和关键路径算法
算法
feifeigo1238 分钟前
基于C#实现即时通讯工具
开发语言·c#
你撅嘴真丑8 分钟前
素数回文数的个数 与 求分数序列和
算法
这是程序猿12 分钟前
基于java的SpringBoot框架医院药品管理系统
java·开发语言·spring boot·后端·spring·医院药品管理系统
yousuotu14 分钟前
基于Python实现水果新鲜度分类
开发语言·python·分类
Wuliwuliii16 分钟前
贡献延迟计算DP
数据结构·c++·算法·动态规划·dp
苦藤新鸡18 分钟前
2.字母异位词分组
c语言·c++·力扣·哈希算法