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

}

}

相关推荐
断剑zou天涯2 分钟前
【算法笔记】Manacher算法
java·笔记·算法
monster000w1 小时前
大模型微调过程
人工智能·深度学习·算法·计算机视觉·信息与通信
曼巴UE51 小时前
UE5 C++ 动态多播
java·开发语言
小小晓.1 小时前
Pinely Round 4 (Div. 1 + Div. 2)
c++·算法
SHOJYS1 小时前
学习离线处理 [CSP-J 2022 山东] 部署
数据结构·c++·学习·算法
biter down1 小时前
c++:两种建堆方式的时间复杂度深度解析
算法
zhishidi1 小时前
推荐算法优缺点及通俗解读
算法·机器学习·推荐算法
WineMonk1 小时前
WPF 力导引算法实现图布局
算法·wpf
steins_甲乙1 小时前
C++并发编程(3)——资源竞争下的安全栈
开发语言·c++·安全
煤球王子2 小时前
学而时习之:C++中的异常处理2
c++