快速排序

每次排序的时候设置一个基准点,将小于等于基准点的数全部放到基准点的左边,将大于等于基准点的数全部放到基准点的右边。
快速排序是不稳定的,时间复杂度(平均):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]);
}
}
}

最新文章

  1. 承接unity外包:2016年VR产业八大发展趋势
  2. hello 漂亮的小靓仔
  3. anaconda win10安装报错:UnicodeDecodeError解决方法
  4. oracle 监测数据库是否存在指定字段
  5. BZOJ1106: [POI2007]立方体大作战tet
  6. Linux oracle 11g r2 安装前检查及安装
  7. java 解析 json 遍历未知key
  8. C++第11周(春)项目1 - 存储班长信息的学生类
  9. Python网络编程学习_Day10
  10. mybatis只能模糊查询英文不能查询中文
  11. Git详解之三:Git分支
  12. 老男孩python学习之作业二---三级菜单
  13. NGUI制作可滚动的文本框(摘,如有侵权,联系删除)
  14. 【Android Studio安装部署系列】十六、Android studio在layout目录下新建子目录
  15. JMeter性能测试中控制业务比例
  16. httpd配置文件httpd.conf规则说明和一些基本指令
  17. git 每次push都需要输入用户和密码
  18. oracle中取得当前日期,前一天,当前月,前一个月
  19. 12.SolrCloud原理
  20. 如何使用URLOS进行docker应用开发

热门文章

  1. 「小程序JAVA实战」小程序的分享和下载功能(69)
  2. 建设银行网上银行MD5withRSA php版
  3. Linux 清除N天前的 日期文件夹(yyyy-MM-dd)
  4. nested exception is java.lang.IllegalStateException: No persistence units parsed from {classpath*:META-INF/persistence.xml}
  5. Node.js中的express框架获取http参数
  6. 实例: Java代码操作oracle数据库(JDBC+sevrlet+jsp+html)
  7. 第6章&#160;数组、指针与字符串(一)基于范围的for循环
  8. 01d-1: 算法分析
  9. Python_03-数据类型
  10. Socket调用方式(同步,异步,阻塞,非阻塞)