博客
关于我
快速排序
阅读量:225 次
发布时间:2019-02-28

本文共 577 字,大约阅读时间需要 1 分钟。

归并排序是每一次的递归调用会确定正确排序中的一个值。

然后这时只确定了个元素的位置。
再通过

fun(left,i-1);fun(i+1,right);

确定这个元素两边的正确排序的位置

#include 
using namespace std;int a[5005];int fun(int left,int right){ if(left >= right) return 0; int i = left; int j = right; int x = a[i]; while(i < j){ while(i < j && a[j] >= x) j--; if(i < j) a[i++] = a[j]; while(i < j && a[i] < x) i++; if(i < j) a[j--] = a[i]; } a[i] = x; fun(left,i-1); fun(i+1,right); return 0;}int main(){ int n; scanf("%d",&n); for(int i = 0;i < n;i++){ scanf("%d",a+i); } fun(0,n-1); for(int i = 0;i < n;i++){ printf("%d\n",a[i]); } return 0;}

转载地址:http://nfqp.baihongyu.com/

你可能感兴趣的文章
mysql 如何给SQL添加索引
查看>>
mysql 字段区分大小写
查看>>
MySQL 存储引擎
查看>>
mysql 更新子表_mysql 在update中实现子查询的方式
查看>>
mysql 自增id和UUID做主键性能分析,及最优方案
查看>>
Mysql 自定义函数
查看>>
mysql 表的操作
查看>>
MySQL 触发器
查看>>
mysql 让所有IP访问数据库
查看>>
MySQL 高可用性之keepalived+mysql双主
查看>>
mysql5.6.21重置数据库的root密码
查看>>
MySQL5.6忘记root密码(win平台)
查看>>
mysql5.7 for windows_MySQL 5.7 for Windows 解压缩版配置安装
查看>>
mysql5.7性能调优my.ini
查看>>
mysql5.7的安装和Navicat的安装
查看>>
MySQL8.0.29启动报错Different lower_case_table_names settings for server (‘0‘) and data dictionary (‘1‘)
查看>>
MySQL8修改密码报错ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
查看>>
mysql8的安装与卸载
查看>>
mysqlbinlog报错unknown variable ‘default-character-set=utf8mb4‘
查看>>
mysqldump 导出中文乱码
查看>>