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

}

}

相关推荐
robch13 分钟前
golang container/heap 是一个为任意类型实现堆(优先队列)接口的包
数据结构·算法·golang
967714 分钟前
力扣面试经典150 88. 合并两个有序数组 归并排序的merge函数
算法·leetcode·面试
Ronin30520 分钟前
【Qt常用控件】容器类控件和布局管理器
开发语言·qt·常用控件·布局管理器·容器类控件
啵啵鱼爱吃小猫咪8 小时前
机械臂阻抗控制github项目-mujoco仿真
开发语言·人工智能·python·机器人
似水明俊德8 小时前
02-C#
开发语言·c#
放下华子我只抽RuiKe58 小时前
算法的试金石:模型训练、评估与调优的艺术
人工智能·深度学习·算法·机器学习·自然语言处理·数据挖掘·线性回归
oem1108 小时前
C++中的享元模式实战
开发语言·c++·算法
流云鹤8 小时前
每日一题0316
算法
似水明俊德8 小时前
01-C#.Net-泛型-面试题
java·开发语言·面试·c#·.net
leonkay8 小时前
Golang语言闭包完全指南
开发语言·数据结构·后端·算法·架构·golang