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

}

}

相关推荐
We་ct几秒前
LeetCode 138. 随机链表的复制:两种最优解法详解
前端·算法·leetcode·链表·typescript
llz_11210 分钟前
蓝桥杯备赛-搜索(DFS/BFS)
c++·算法·蓝桥杯·深度优先·宽度优先
康小庄14 分钟前
Java读写锁降级
java·开发语言·spring boot·python·spring·java-ee
山顶夕景15 分钟前
【Math】数学知识点串联
人工智能·数学·算法·机器学习
毕设源码-钟学长17 分钟前
【开题答辩全过程】以 基于Java的停车场信息管理系统设计与实现为例,包含答辩的问题和答案
java·开发语言
Mr -老鬼18 分钟前
基于 Go 的脚本平台 APP 云控系统
开发语言·后端·golang
hewence121 分钟前
Kotlin CoroutineScope解密
android·开发语言·kotlin
咩图22 分钟前
VSCode+Python创建项目
开发语言·python
Hag_2023 分钟前
LeetCode Hot100 42.接雨水
算法·leetcode·职场和发展