博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
快速排序
阅读量:4308 次
发布时间:2019-06-06

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

快速排序是冒泡排序的改进,效率比较高,基本思想也是两两相比较。

public class QuickSort {
public static void main(String[] args) {
int[] arr = {1, 4, 11, 63, 92, 2, -2, -3, -6, 0}; quickSort(arr, 0, arr.length - 1); System.out.println(Arrays.toString(arr)); } public static void quickSort(int[] arr, int low, int high) {
//当low和high是一个数的时候就结束 if (low >= high) {
return; } //i和j从两头往中间靠,直到相等即结束 int i = low; int j = high; int key = arr[i];//基准值 while (i < j) {
while (arr[j] >= key && i < j) {
j--; } if (i < j) {//交换 int t; t = arr[i]; arr[i] = arr[j]; arr[j] = t; } while (arr[i] <= key && i < j) {
i++; } if (i < j) {//交换 int t; t = arr[i]; arr[i] = arr[j]; arr[j] = t; } } //对基准左侧集合重复执行 quickSort(arr, low, i - 1); //对基准右侧集合重复执行 quickSort(arr, i + 1, high); } } 排序结果:[-6, -3, -2, 0, 1, 2, 4, 11, 63, 92]

转载于:https://www.cnblogs.com/jasonboren/p/10779150.html

你可能感兴趣的文章
uni-app跨页面、跨组件通讯
查看>>
springmvc-helloworld(idea)
查看>>
JDK下载(百度网盘)
查看>>
idea用得溜,代码才能码得快
查看>>
一篇掌握python魔法方法详解
查看>>
数据结构和算法5-非线性-树
查看>>
数据结构和算法6-非线性-图
查看>>
数据结构和算法7-搜索
查看>>
数据结构和算法8-排序
查看>>
windows缺少dll解决办法
查看>>
JPA多条件动态查询
查看>>
JPA自定义sql
查看>>
BigDecimal正确使用了吗?
查看>>
joplin笔记
查看>>
JNDI+springmvc使用
查看>>
vue+springboot分页交互
查看>>
vue+springboot打包发布
查看>>
XSL 开发总结
查看>>
beta阶段第六次scrum meeting
查看>>
SpringBoot+MybatisPlus实现批量添加的两种方式
查看>>