java算法之三快速排序(交换排序)
2016-01-12 20:09
363 查看
快速排序的基本思想:
通过一趟排序将待排序记录分割成独立的两部分,其中一部分记录的关键字均比另一部分关键字小,则分别对这两部分继续进行排序,直到整个序列有序。
先看一下这幅图:
![](http://hi.csdn.net/attachment/201202/23/0_1329967950X8ro.gif)
把整个序列看做一个数组,把第零个位置看做中轴,和最后一个比,如果比它小交换,比它大不做任何处理;交换了以后再和小的那端比,比它小不交换,比他大交换。这样循环往复,一趟排序完成,左边就是比中轴小的,右边就是比中轴大的,然后再用分治法,分别对这两个独立的数组进行排序。
[html] view
plaincopy
public int getMiddle(Integer[] list, int low, int high) {
int tmp = list[low]; //数组的第一个作为中轴
while (low < high) {
while (low < high && list[high] > tmp) {
high--;
}
list[low] = list[high]; //比中轴小的记录移到低端
while (low < high && list[low] < tmp) {
low++;
}
list[high] = list[low]; //比中轴大的记录移到高端
}
list[low] = tmp; //中轴记录到尾
return low; //返回中轴的位置
}
递归形式的分治排序算法:
[html] view
plaincopy
public void _quickSort(Integer[] list, int low, int high) {
if (low < high) {
int middle = getMiddle(list, low, high); //将list数组进行一分为二
_quickSort(list, low, middle - 1); //对低字表进行递归排序
_quickSort(list, middle + 1, high); //对高字表进行递归排序
}
}
[html] view
plaincopy
public void quick(Integer[] str) {
if (str.length > 0) { //查看数组是否为空
_quickSort(str, 0, str.length - 1);
}
}
编写测试方法:
[html] view
plaincopy
public class TestMain {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Integer[] list={34,3,53,2,23,7,14,10};
QuicSort qs=new QuicSort();
qs.quick(list);
for(int i=0;i<list.length;i++){
System.out.print(list[i]+" ");
}
System.out.println();
}
}
看一下打印结果吧:
2 3 7 10 14 23 34 53
这样就排序好了,快速排序是对【冒泡排序】的一种改进,主要是引入了分治思想的概念。平均时间复杂度是O(nlogn)。
通过一趟排序将待排序记录分割成独立的两部分,其中一部分记录的关键字均比另一部分关键字小,则分别对这两部分继续进行排序,直到整个序列有序。
先看一下这幅图:
![](http://hi.csdn.net/attachment/201202/23/0_1329967950X8ro.gif)
把整个序列看做一个数组,把第零个位置看做中轴,和最后一个比,如果比它小交换,比它大不做任何处理;交换了以后再和小的那端比,比它小不交换,比他大交换。这样循环往复,一趟排序完成,左边就是比中轴小的,右边就是比中轴大的,然后再用分治法,分别对这两个独立的数组进行排序。
[html] view
plaincopy
public int getMiddle(Integer[] list, int low, int high) {
int tmp = list[low]; //数组的第一个作为中轴
while (low < high) {
while (low < high && list[high] > tmp) {
high--;
}
list[low] = list[high]; //比中轴小的记录移到低端
while (low < high && list[low] < tmp) {
low++;
}
list[high] = list[low]; //比中轴大的记录移到高端
}
list[low] = tmp; //中轴记录到尾
return low; //返回中轴的位置
}
递归形式的分治排序算法:
[html] view
plaincopy
public void _quickSort(Integer[] list, int low, int high) {
if (low < high) {
int middle = getMiddle(list, low, high); //将list数组进行一分为二
_quickSort(list, low, middle - 1); //对低字表进行递归排序
_quickSort(list, middle + 1, high); //对高字表进行递归排序
}
}
[html] view
plaincopy
public void quick(Integer[] str) {
if (str.length > 0) { //查看数组是否为空
_quickSort(str, 0, str.length - 1);
}
}
编写测试方法:
[html] view
plaincopy
public class TestMain {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Integer[] list={34,3,53,2,23,7,14,10};
QuicSort qs=new QuicSort();
qs.quick(list);
for(int i=0;i<list.length;i++){
System.out.print(list[i]+" ");
}
System.out.println();
}
}
看一下打印结果吧:
2 3 7 10 14 23 34 53
这样就排序好了,快速排序是对【冒泡排序】的一种改进,主要是引入了分治思想的概念。平均时间复杂度是O(nlogn)。
相关文章推荐
- 《Spring实战》学习笔记-第三章:最小化SpringXML配置
- Java NIO使用及原理分析(二)
- eclipse不能进入debug
- Spring 学习笔记 整合Hibernate
- Spring MVC API
- Spring 学习笔记整合 Hibernate
- Java NIO使用及原理分析 (一)
- Spring 学习笔记之整合Hibernate
- 使用SPRING+HIBERNATE 控制事务
- JavaEE_SSM_报错_Open quote is expected for attribute "property" associated with an element
- java如何创建一个窗口出来
- java异常处理<1>__笔记(17)
- 文章标题
- Java 文件读取
- Date,SimpleDateFormat,Calendar 类的应用
- 基于Spring可扩展Schema提供自定义配置支持
- commons-beanutils的使用【在对象之间进行属性值复制】
- Spring控制多张表的提交事务操作
- Eclipse+ADT 环境配置教程
- java复制文件夹及所有子目录和文件