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

}

}

相关推荐
小小码农Come on5 分钟前
C++访问QML控件-----QML访问C++对象属性和方法
java·开发语言·c++
代码中介商22 分钟前
C语言函数完全指南:从基础到实践
c语言·开发语言
小章UPUP24 分钟前
2026年第十六届MathorCup数学应用挑战赛D题国奖思路
算法
Yungoal27 分钟前
项目层级结构
c++
hssfscv32 分钟前
软件设计师下午试题四——C语言(N皇后问题、分治、动态规划)
c语言·算法·动态规划
思茂信息35 分钟前
CST交叉cable的串扰(crosstalk)仿真
服务器·开发语言·人工智能·php·cst
lolo大魔王41 分钟前
Go语言的反射机制
开发语言·后端·算法·golang
白羊by1 小时前
Softmax 激活函数详解:从数学原理到应用场景
网络·人工智能·深度学习·算法·损失函数
那个失眠的夜1 小时前
AspectJ
java·开发语言·数据库·spring
程序员-King.1 小时前
【基础分析】—— 条件变量wait(lock, 谓词)
c++·c·多线程·条件变量