快速排序 JAVA实现
2024-08-26 19:19:10
快速排序
每次排序的时候设置一个基准点,将小于等于基准点的数全部放到基准点的左边,将大于等于基准点的数全部放到基准点的右边。
快速排序是不稳定的,时间复杂度(平均):nlogn
public class QuickSort {
public static void Sort(int[] arr, int low, int high) {
int i, j, temp;
if (low > high) {
return;
}
i = low;//一定要注意是从low开始的
j = high;
// temp就是基准位
temp = arr[low];
while (i < j) {
//从右向左,找到小于基准位的数组成员位置
while (temp <= arr[j] && i < j) {
j--;
}
//从左向右,找到大于基准位的数组成员位置
while (temp >= arr[i] && i < j) {
i++;
}
//如果满足条件则交换arr[i],arr[j]
if (i < j) {
swap(arr, i, j);
}
}
//最后将基准为与i和j相等位置的数字交换(此时i j相等)
arr[low] = arr[i];
arr[i] = temp;
//分而治之
Sort(arr, low, i - 1);
Sort(arr, i + 1, high);
} public static void swap(int[] a, int i, int j) {
int temp = a[i];
a[i] = a[j];
a[j] = temp;
} public static void main(String[] args) {
int[] arr = { 4, 5, 9, 4, 7, 23, 3, 4, 2, 1, 8, 11, 19, 10 };
Sort(arr, 0, arr.length - 1);
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
}
最新文章
- 承接unity外包:2016年VR产业八大发展趋势
- hello 漂亮的小靓仔
- anaconda win10安装报错:UnicodeDecodeError解决方法
- oracle 监测数据库是否存在指定字段
- BZOJ1106: [POI2007]立方体大作战tet
- Linux oracle 11g r2 安装前检查及安装
- java 解析 json 遍历未知key
- C++第11周(春)项目1 - 存储班长信息的学生类
- Python网络编程学习_Day10
- mybatis只能模糊查询英文不能查询中文
- Git详解之三:Git分支
- 老男孩python学习之作业二---三级菜单
- NGUI制作可滚动的文本框(摘,如有侵权,联系删除)
- 【Android Studio安装部署系列】十六、Android studio在layout目录下新建子目录
- JMeter性能测试中控制业务比例
- httpd配置文件httpd.conf规则说明和一些基本指令
- git 每次push都需要输入用户和密码
- oracle中取得当前日期,前一天,当前月,前一个月
- 12.SolrCloud原理
- 如何使用URLOS进行docker应用开发
热门文章
- 「小程序JAVA实战」小程序的分享和下载功能(69)
- 建设银行网上银行MD5withRSA php版
- Linux 清除N天前的 日期文件夹(yyyy-MM-dd)
- nested exception is java.lang.IllegalStateException: No persistence units parsed from {classpath*:META-INF/persistence.xml}
- Node.js中的express框架获取http参数
- 实例: Java代码操作oracle数据库(JDBC+sevrlet+jsp+html)
- 第6章&#160;数组、指针与字符串(一)基于范围的for循环
- 01d-1: 算法分析
- Python_03-数据类型
- Socket调用方式(同步,异步,阻塞,非阻塞)