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

}

}

相关推荐
城管不管1 分钟前
rabbitmq如何保证消息不丢失?解决方案又是什么?
开发语言·ai·面试·职场和发展·rabbitmq·php·agent
为啥全要学2 分钟前
在大语言模型上使用 PPO 算法
人工智能·算法·语言模型
程序员良辰2 分钟前
【TongWeb7】启动接近两分钟问题排查
java·开发语言·中间件
深色風信子5 分钟前
Kotlin Bytedeco OpenCV 图像图像58 无缝克隆
开发语言·opencv·kotlin·bytedeco
朝阳3916 分钟前
react19【系列实用教程】实用组件封装
开发语言·前端·javascript
我是苏苏23 分钟前
C#基础:使用System.Speech.Synthesis离线播放/朗读文字内容
开发语言·c#
j7~34 分钟前
【Linux】二十八.线程篇五《Linux多线程编程:线程同步之条件变量》---详解
linux·运维·c++·学习·条件变量·线程同步
王老师青少年编程34 分钟前
2026年全国青少年信息素养大赛算法应用主题赛C++赛项【决赛】模拟卷3:文末附答案
c++·答案·模拟题·2026年·青少年信息素养大赛·算法应用主题赛·决赛
Patrick在香港37 分钟前
Python依赖管理从踩坑到选型:pip/poetry/uv四种方案全面实测
开发语言·python·数据分析·scikit-learn·ai编程·pip·uv
zander25838 分钟前
35. 搜索插入位置:从边界语义理解二分查找
数据结构·算法·leetcode