快速排序

  • 快速排序原理

  快速排序(Quick Sort)的基本思想是,通过一趟排序将待排记录分割成独立的两部分,其中一部分记录的关键字均比另一部分记录的关键字小,则可对这两部分记录继续进行排序,以达到整个序列有序的目的。

  • 主程序
package Sort;

public class QuickSort {

	private void Qsort(int[] list, int low, int high){
int privot;
if(low < high) {
print(list);
/*算出枢轴值privot*/
privot = Partition(list, low, high);
/*对低子表递归排序*/
Qsort(list, low, privot-1);
/*对高子表递归排序*/
Qsort(list, privot+1, high);
}
} private int Partition(int[] list, int low, int high){
/*用表的第一个记录做枢轴记录*/
int privotkey = list[low];
while (low < high){
while (low < high && list[high] > privotkey)
high--;
/*将比枢轴记录小的记录交换到低端*/
swap(list, low, high);
while (low < high && list[low] < privotkey)
low++;
/*将比枢轴记录大的记录交换到高端*/
swap(list, low, high);
}
/*返回枢轴所在的位置*/
return low;
} private void swap(int [] list,int j, int i){
int temp;
temp = list[i];
list[i] = list[j];
list[j] = temp;
} private void print(int[] data) {
System.out.println("当前list数组的值:");
for (int i = 0; i < data.length; i++) {
System.out.print(data[i] + "\t");
}
System.out.println();
} public static void main(String[] args) {
int[] list = {50,40,80,30,60,70,10,90,20};
QuickSort qs = new QuickSort();
qs.Qsort(list, 0, list.length-1);
qs.print(list);
}
}

  

  • Qsort()分析
	private void Qsort(int[] list, int low, int high){
int privot;
if(low < high) {
print(list);
/*算出枢轴值privot*/
privot = Partition(list, low, high);
/*对低子表递归排序*/
Qsort(list, low, privot-1);
/*对高子表递归排序*/
Qsort(list, privot+1, high);
}
}

  

  • Partition()分析
	private int Partition(int[] list, int low, int high){
/*用表的第一个记录做枢轴记录*/
int privotkey = list[low];
while (low < high){
while (low < high && list[high] > privotkey)
high--;
/*将比枢轴记录小的记录交换到低端*/
swap(list, low, high);
while (low < high && list[low] < privotkey)
low++;
/*将比枢轴记录大的记录交换到高端*/
swap(list, low, high);
}
/*返回枢轴所在的位置*/
return low;
}

  

  • 输出结果
当前list数组的值:
50 40 80 30 60 70 10 90 20
当前list数组的值:
20 40 10 30 50 70 60 90 80
当前list数组的值:
10 20 40 30 50 70 60 90 80
当前list数组的值:
10 20 30 40 50 70 60 90 80
当前list数组的值:
10 20 30 40 50 60 70 90 80
当前list数组的值:
10 20 30 40 50 60 70 80 90

  

  • 时间复杂度分析

  最优情况下,快速排序算法的时间复杂度为O(nlogn),空间复杂度也为O(nlogn)

  • 单链表快速排序实现
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode sortList(ListNode head) {
if(head == null || head.next == null) return head; ListNode left = new ListNode(0), leftHead = left;
ListNode right = new ListNode(0), rightHead = right;
ListNode mid = new ListNode(0), midHead = mid;
int val = head.val; while(head != null) {
if(head.val < val) {
left.next = head;
left = head;
} else if(head.val > val) {
right.next = head;
right = head;
} else {
mid.next = head;
mid = head;
}
head = head.next;
} left.next = null;
right.next = null;
mid.next = null;
return merge(sortList(leftHead.next), midHead.next, sortList(rightHead.next));
} public ListNode merge(ListNode left, ListNode mid, ListNode right) {
ListNode leftTail = getTail(left);
ListNode midTail = getTail(mid);
midTail.next = right;
if(leftTail != null) {
leftTail.next = mid;
return left;
} else {
return mid;
}
} public ListNode getTail(ListNode head) {
if(head == null) return head;
while(head.next != null) {
head = head.next;
}
return head;
}
}

  

最新文章

  1. zookeeper启动后没有相关进程
  2. CSS 盒子
  3. js之事件冒泡和事件捕获
  4. js 和 jq 控制 checkbox
  5. sizeof 和 strlen 的区别
  6. MapReduce的输入输出格式
  7. [转载]开机出现A disk read error occurred错误
  8. Eclipse 各种包说明
  9. OC语法10——@protocol协议,
  10. Linux下的定时器
  11. Raphael入门实例:动画与箭头
  12. 支持老版本IE的3种解决方案
  13. FTRL(Follow The Regularized Leader)学习总结
  14. open
  15. 使用Gson解析复杂、变态的Json数据(包含中文key)
  16. hdu 5017 模拟退火/三分求椭圆上离圆心最近的点的距离
  17. Android之新建项目
  18. rp2833 网卡以及串口与接插件位置关系
  19. SDWebImage从缓存中获取图片
  20. C# 表达式树 创建、生成、使用、lambda转成表达式树~表达式树的知识详解

热门文章

  1. Cocos2d-js 3.0 alp2 使用指南
  2. FolderBrowserDialog
  3. [D3] 8. Margins
  4. JAAS LOGIN IN WEBLOGIC SERVER--reference
  5. maven 学习1 -安装maven 并执行编译命令
  6. JQuery字符串替换replace方法
  7. PHP 内存的分布问题
  8. asp.net各种获取客户端ip方法
  9. android中sharedPreferences的笔记
  10. .NET 操作PDF文档以及PDF文件打印摸索总结