快速排序的实现
来源:互联网 发布:lapse it 编辑:程序博客网 时间:2024/05/23 22:49
public class Main { public static void main(String[] args) { int[] testData = {8, 5, 7, 1, 99, 44, 78, 22}; int[] dataSorted = quickSort(testData, 0, testData.length-1); for(int a : dataSorted) { System.out.print(a + " "); } System.out.println("比较次数:" + cnt); } /** * 快速排序 */ static int cnt = 0; private static int partition(int[] data, int low, int high) { int key = data[low]; while(low < high) { while(low<high && data[high]>=key) { high--; cnt++; } data[low] = data[high];//(此时因low=high或data[high]<key)将high下标处的数赋给low下标处的数,保证data[low]<key while(low<high && data[low]<=key) { low++; cnt++; } data[high] = data[low];//(此时因low=high或data[low]>key)将low下标处的数赋给high下标处的数,保证data[high]>key } data[low] = key; return low; } public static int[] quickSort(int[] data, int low, int high) { if(low < high) { int result = partition(data, low, high); quickSort(data, low, result-1);//对low到result-1下标间数进行排序 quickSort(data, result+1, high);//对result+1到high下标间数进行排序 } return data; } }
0 0
- 快速排序的实现
- 快速排序的实现
- 快速排序的实现
- 快速排序的实现
- 快速排序的实现
- 快速排序的实现
- 快速排序的实现
- 快速排序的实现
- 快速排序的实现
- 快速排序的实现
- 快速排序的实现
- 快速排序的实现
- 快速排序的实现
- 快速排序的实现
- 快速排序的实现
- 快速排序的实现
- 快速排序的实现
- 快速排序的实现
- 在iOS平台上使用TensorFlow教程(下)
- jQuery实现web备忘录-3
- 使用VS CODE+PlantUML高效画图
- MySql 启动与连接
- ElasticSearch学习资料
- 快速排序的实现
- maven配置完环境变量后无法识别mvn -v命令,或者重启后无法识别
- Object pool
- 题目1041:Simple Sorting
- 【编程】大鱼吃小鱼
- int类型数组转换成string数组
- 无锁队列的实现
- Python学习之字符串(二)
- Oracle 数据库表操作整理