快速排序的Partition函数
2024-09-06 01:45:27
1 //数组中两个数的交换
2 static void swap(int[] nums, int pos1, int pos2){
3 int temp = nums[pos1];
4 nums[pos1] = nums[pos2];
5 nums[pos2] = temp;
6 }
7 /**
8 * 快速排序中,在数组中选择一个数字,将数组中的数字分为两部分
9 * start, end 介于 0 与 nums.length之间
10 */
11 static int partition(int[] nums, int start, int end){
12
13 int index = new Random().nextInt(end + 1 - start) + start;//范围是[start end]闭区间
14 swap(nums, index, end);
15
16 int small = start - 1;
17 for (index = start; index < end; ++index) {
18
19 if (nums[index] < nums[end]) {
20 ++small;
21 if (small != index) {
22 swap(nums, index, small);
23 }
24 }
25
26 }
27
28 ++ small;
29 swap(nums, small, end);
30 return small;
31 }
最新文章
- Reverse-Daily(3)-DotNetCrackMe1
- 在Linux下用源码编译安装apache2
- 用Python和摄像头制作简单的延时摄影
- android中获取string字符串的方法
- 使用U盘安装Ubuntu系统的实践小结
- 第三章 传奇的开始--Delphi(附读书笔记)
- SQL viewId 比较好看的 Id
- python学习视频整理
- js 性能优化整理之 高频优化
- Jquery对话框基本配置
- iOS学习——自动定位
- [论文阅读] A Discriminative Feature Learning Approach for Deep Face Recognition (Center Loss)
- 解决win10 VC++6.0 应用程序无法正常运行 0xc0000142
- ReSharper2018破解详细方法
- [Swift]LeetCode858. 镜面反射 | Mirror Reflection
- python3 第二十九章 - 内置函数之tuple相关
- scrapy模拟用户登录
- Ant+Jmeter自动化接口测试的部署 及 部署过程中的坑
- HDU 1251 统计难题(Trie)
- 【原创】Nginx+PHP-FPM优化技巧总结(转)
热门文章
- 计算机毕业设计选题大合集,含ssm,springboot,小程序,php,python
- 【16位RAW图像处理三】直方图均衡化及局部直方图均衡用于16位图像的细节增强。
- [源码解析] 深度学习分布式训练框架 horovod (18) --- kubeflow tf-operator
- Python (paramiko) 连接Linux服务器
- 【剑指offer】55 - I. 二叉树的深度
- 【连载】微服务网格Istio(一)
- 【LeetCode】142. 环形链表 II
- 最大网络流dinic
- Springboot中mybatis执行逻辑源码分析
- 异地远程访问群晖NAS中的文件