算法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(resultj<resultj-1){

int tmp=resultj;

resultj=resultj-1;

resultj-1=tmp;

flag=1;

}

}

if(flag==0)

break;

}

for(int i=0;i<result.size();i++){

cout<<resulti<<" ";

}

}

快速排序:

#include <stdio.h>

#include <vector>

#include <iostream>

using namespace std;

int pattern(vector<int> &a,int low,int high){

int tmp=alow;

while(low<high){

for(;low<high&&ahigh>tmp;high--){

}

swap(alow,ahigh);

for(;low<high&&alow<=tmp;low++){

}

swap(ahigh,alow);

}

alow=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<<resulti<<" ";

}

}

相关推荐
u0111026751 分钟前
工具页案例图如何编写 alt让图片说明与处理场景对应
java·前端·javascript·图像处理·人工智能·算法·ai作画
ZealSinger5 分钟前
Go slog生产落地LevelVar与共存
开发语言·后端·golang·go
阿狗童鞋5 分钟前
数据结构与算法实战指南
数据结构·算法
落魄实习生14 分钟前
Agent Scope Java 2.x 系列【11】AgentState 与状态存储
java·开发语言·ai
HEJOO914 分钟前
C++ 虚函数表(vtable)深度解析
开发语言·c++
老王爱玩车17 分钟前
深入理解指针5
c语言·开发语言·学习
Navigator_Z27 分钟前
LeetCode //C - 1275. Find Winner on a Tic Tac Toe Game
c语言·算法·leetcode
未来之窗软件服务30 分钟前
递归时间复杂度演算T (n)=aT (n/b)+f (n)-东方仙盟
算法·仙盟创梦ide·东方仙盟
丹宇码农31 分钟前
Go 与 Python 协程(Coroutine)对比演示项目
开发语言·python·golang
ttwuai44 分钟前
Go开源后台管理系统推荐:3个官方仓库怎么按技术栈和适用边界比较?
开发语言·golang·开源