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

}

}

相关推荐
小曹要微笑2 分钟前
C#的运算符重载
开发语言·c#·运算符重载·c#运算符重载
梯度下降中2 分钟前
CNN原理精讲
人工智能·算法·机器学习
Qt程序员3 分钟前
基于 C++ 实现自定义字符串 string 类
linux·c++·容器·指针·内存管理·运算符重载
我是唐青枫3 分钟前
C#.NET Channel 深入解析:高性能异步生产者消费者模型实战
开发语言·c#·.net
Ivanqhz5 分钟前
活跃范围重写(Live Range Rewriting)
开发语言·c++·后端·算法·rust
人道领域8 分钟前
【绝对干货】C语言常量,变量,内存全方位总结:从入门到精通,这一篇就够了!
c语言·开发语言
yuyuxun19 分钟前
基于JSP购物网站系统的设计与实现 毕业设计-附源码03645
java·开发语言·python·django·flask·课程设计·pygame
xiaoye-duck10 分钟前
《算法题讲解指南:优选算法-链表》--51.两数相加,52.两两交换链表中的节点
数据结构·算法·链表
Cosolar14 分钟前
阿里CoPaw进阶使用手册:从新手到高手的完整指南
人工智能·后端·算法
牢七19 分钟前
Slim-4.x php审计 报错分析
android·开发语言·ide·安全·php