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

}

}

相关推荐
qq_283720052 分钟前
C++ STL 容器选型实战:vector/list/map/unordered_map 性能对比与选型指南
c++·list·map·性能对比
Dev7z2 分钟前
基于SVM与HOG算法的行人检测系统设计与实现
算法·机器学习·支持向量机·行人检测·hog算法
捧 花4 分钟前
全面掌握数据结构:Java 与 Go 定义方式 + 原理 + 使用场景
java·开发语言·数据结构·golang
王家视频教程图书馆5 分钟前
【保姆级】Go Fyne GUI 开发环境搭建(Windows 全流程避坑)
开发语言·windows·golang
郝学胜-神的一滴6 分钟前
Pytorch张量拼接秘籍:cat与stack的深度解析与实战
人工智能·pytorch·python·深度学习·程序人生·算法·机器学习
17(无规则自律)13 分钟前
【华为机考真题】动态规划之遗迹探险家小红(含C++源码)
c++·华为·动态规划
jzlhll12313 分钟前
kotlin flow去重distinctUntilChanged vs distinctUntilChangedBy
android·开发语言·kotlin
Hello eveybody15 分钟前
二叉树简述+考试要点(C++)
java·c++·算法
HIT_Weston18 分钟前
36、【Agent】【OpenCode】本地代理(JavaScript 脚本)
开发语言·javascript·ecmascript
做cv的小昊18 分钟前
【TJU】应用统计学——第四周作业(2.3 C-R不等式、2.4区间估计)
c语言·人工智能·算法·机器学习·数学建模·r语言·概率论