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

}

}

相关推荐
谁动了我的代码?7 分钟前
QT<34> 利用线程池处理耗时任务以及回调函数的使用
开发语言·qt
柒.梧.8 分钟前
数据结构:二叉排序树构建与遍历的解析与代码实现
java·开发语言·数据结构
李迟10 分钟前
Golang实践录:接口文档字段转结构体定义
开发语言·golang
一个不知名程序员www14 分钟前
算法学习入门---priority_queue(C++)
c++·算法
徐同保35 分钟前
js 点击按钮 把文本转成文件并下载下来
开发语言·javascript·ecmascript
TL滕1 小时前
从0开始学算法——第十八天(分治算法)
笔记·学习·算法
LYFlied1 小时前
【每日算法】LeetCode 84. 柱状图中最大的矩形
前端·算法·leetcode·面试·职场和发展
2501_930707781 小时前
使用C#代码更改 PowerPoint 幻灯片大小
开发语言·c#·powerpoint
Pafey1 小时前
C++的左值引用、右值引用以及转发和完美转发
c++
CoderCodingNo1 小时前
【GESP】C++三级真题 luogu-B4414 [GESP202509 三级] 日历制作
开发语言·c++·算法