算法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 分钟前
线程的概念和控制
java·开发语言·jvm
John.Lewis17 分钟前
Python小课(1)认识Python
开发语言·python
一只大袋鼠25 分钟前
MyBatis 入门详细实战教程(一):从环境搭建到查询运行
java·开发语言·数据库·mysql·mybatis
sonnet-102939 分钟前
函数式接口和方法引用
java·开发语言·笔记
Bat U43 分钟前
JavaEE|多线程(二)
java·开发语言
YIN_尹1 小时前
【Linux系统编程】进程地址空间
linux·c++
烤麻辣烫1 小时前
JS基础
开发语言·前端·javascript·学习
EverestVIP1 小时前
C++中空类通常大小为1的原理
c++
froginwe111 小时前
C++ 文件和流
开发语言
Dxy12393102162 小时前
Python在图片上画矩形:从简单边框到复杂标注的全攻略
开发语言·python