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

}

}

相关推荐
紫金修道4 小时前
【DeepAgent】概述
开发语言·数据库·python
Via_Neo4 小时前
JAVA中以2为底的对数表示方式
java·开发语言
书到用时方恨少!4 小时前
Python multiprocessing 使用指南:突破 GIL 束缚的并行计算利器
开发语言·python·并行·多进程
cch89184 小时前
PHP五大后台框架横向对比
开发语言·php
天真萌泪5 小时前
JS逆向自用
开发语言·javascript·ecmascript
野生技术架构师5 小时前
一线大厂Java面试八股文全栈通关手册(含源码级详解)
java·开发语言·面试
Q一件事5 小时前
R语言制图-相关性及关系网络图
开发语言·r语言
坊钰5 小时前
Java 死锁问题及其解决方案
java·开发语言·数据库
小月球~6 小时前
天梯赛 · 并查集
数据结构·算法
仍然.6 小时前
算法题目---模拟
java·javascript·算法