快速排序算法是对集合中元素进行排序最通用的算法,俗称快排,其算法的时间复杂度为O(nlgn),空间复杂度为O(1)。

我们举例来对其算法思路进行理解,譬如数组 A = { 4, 8, 1, 2, 9, 7, 3, 0, 5, 6 };

第一步,以最后一个数6为基准,把小于等于6的数挪到数组左边,把大于6的数挪到数组右边。

那么结果为 { 4, 1, 2, 3, 0, 5, 8, 9, 7, 6 },这个时候再做一步,把8和6进行交换,得到{ 4, 1, 2, 3, 0, 5, 6, 9, 7, 8 }把6的最新位置返回。这个时候其实数组被切割成两部分,准确地说,是三部分{ 4, 1, 2, 3, 0, 5 }, { 6 }和{ 9, 7, 8 }.

第二步,对 { 4, 1, 2, 3, 0, 5 }和 { 9, 7, 8 }重复第一步的过程,我们得到

{ 4, 1, 2, 3, 0 }, { 5 }, { 7 }, { 8 }, { 9 }

第三步,再对{ 4, 1, 2, 3, 0 }进行分割,得到 { 0 }, { 1, 2, 3, 4 }

第四步,再对{ 1, 2, 3, 4 }进行分割,得到{ 1, 2, 3 }和{ 4 }

第五步,对{ 1, 2, 3 }进行分割,得到{ 1, 2 }和{ 3 }

第六步,对{ 1, 2 }进行分割,得到{ 1 }和{ 2 }

C实现代码如下

#include <stdio.h>

#define LEN 10

int partition(int *arr, int start, int end)
{
int pivot = arr[end];
int i = start;
int j;
int tmp;
for (j = start; j < end; ++j) {
if (arr[j] <= pivot) {
tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
++i;
}
}
arr[end] = arr[i];
arr[i] = pivot;
return i;
} int quicksort(int *arr, int start, int end)
{
int pivot_location;
if (start < end) {
pivot_location = partition(arr, start, end);
quicksort(arr, start, pivot_location - );
quicksort(arr, pivot_location + , end);
}
} int main()
{
int i;
int arr[LEN] = { , , , , , , , , , };
quicksort(arr, , LEN - );
for (i = ; i < LEN; ++i)
printf("%d\n", arr[i]);
return ;
}

Java实现代码如下

public class QuickSort {

    // Sort a list of numbers in an array within [start, end]
public void quickSort(int[] A, int start, int end) {
if (start < end) {
int pivotLocation = partition(A, start, end);
quickSort(A, start, pivotLocation - 1);
quickSort(A, pivotLocation + 1, end);
}
} // Select A[end] as the pivot, separate the array into two parts.
private int partition(int[] A, int start, int end) {
int pivot = A[end];
int i = start;
for (int j = start; j < end; ++j) {
if (A[j] <= pivot) {
int tmp = A[i];
A[i] = A[j];
A[j] = tmp;
++i;
}
} A[end] = A[i];
A[i] = pivot;
return i;
} public static void main(String[] args) {
QuickSort q = new QuickSort();
int[] A = { 4, 8, 1, 2, 9, 7, 3, 0, 5, 6 };
q.quickSort(A, 0, A.length - 1);
for (int i = 0; i < A.length; ++i) {
System.out.println(A[i]);
}
} }

快速排序算法(Quicksort)的更多相关文章

  1. Javascript算法系列之快速排序(Quicksort)

    原文出自: http://www.nczonline.net/blog/2012/11/27/computer-science-in-javascript-quicksort/ https://gis ...

  2. 排序算法之快速排序(Quicksort)解析

    一.快速排序算法的优点,为什么称之为快排? Quicksort是对归并排序算法的优化,继承了归并排序的优点,同样应用了分治思想. 所谓的分治思想就是对一个问题“分而治之”,用分治思想来解决问题需要两个 ...

  3. 排序算法五:随机化快速排序(Randomized quicksort)

    上一篇提到,快速排序的平均时间复杂度是O(nlgn),比其他相同时间复杂度的堆排序.归并排序都要快,但这是有前提的,就是假定要排序的序列是随机分布的,而不是有序的.实际上,对于已经排好的序列,如果用快 ...

  4. Algorithms - Quicksort - 快速排序算法

    相关概念 快速排序法 Quicksort 也是一个分治思想的算法. 对一个子数组 A[p: r] 进行快速排序的三步分治过程: 1, 分解. 将数组 A[p : r] 被划分为两个子数组(可能为空) ...

  5. C#数据结构与算法系列(二十二):快速排序算法(QuickSort)

    1.介绍 快速排序(QuickSort)是对冒泡排序的一种改进,基本思想是:通过一趟排序将要排序的数据分割成独立的两部分, 其中一部分的所有数据都比另一部分的所有数据都要小,然后再按此方法对这两部分数 ...

  6. 快速排序算法 java 实现

    快速排序算法 java 实现 快速排序算法Java实现 白话经典算法系列之六 快速排序 快速搞定 各种排序算法的分析及java实现 算法概念 快速排序是C.R.A.Hoare于1962年提出的一种划分 ...

  7. C# 集合扩展快速排序算法

    /// <summary> /// 对集合进行排序,如 /// List<Person> users=new List<Person>(){.......} /// ...

  8. 快速排序算法-C语言实现

    注:本篇内容为翻译,之所以选择这篇进行翻译原因是该文章含有动画,能够更加直观地展示快速排序.同时,可以仔细看一下代码,代码中把结构化的思想给予了更加充分地表现.按照功能进行模块划分的思想得到了彻底地贯 ...

  9. C#快速排序算法基础入门篇

    相信算法对于许多开发人员来说都是一大难点,之所以难,就像设计模式一样,许多人在阅读之后,没有很好地理解,也不愿意动手上机操作,只停留在理论的学习上面,随着时间推移就慢慢淡忘. 有些东西,你可以发明创造 ...

随机推荐

  1. centOS7 安装man中文手册

    [root@localhost ~]# yum list | grep man.*zh -.el7 base [root@localhost ~]# yum -y install man-pages- ...

  2. 参数化查询 '(@ActualShipTime datetime' 需要参数 @AuthorizationNumber,但未提供该参数。

    转自 http://www.cnblogs.com/cxd4321/archive/2012/08/09/2629716.html 在平时的C#项目开发中,当调用某个存储过程或函数的时候,我们可能经常 ...

  3. Golang 新手可能会踩的 50 个坑

    前言 Go 是一门简单有趣的编程语言,与其他语言一样,在使用时不免会遇到很多坑,不过它们大多不是 Go 本身的设计缺陷.如果你刚从其他语言转到 Go,那这篇文章里的坑多半会踩到. 如果花时间学习官方 ...

  4. /etc/ssh/sshd_config 关建字:PermitRootLogin no  禁示以root身份登录服务器

    这种情况,不会影响,普通用户su到root

  5. centos7 开启特定的端口

    systemctl是centos7的服务管理工具中主要的工具,它融合之前的service和chkconfig功能于一体; 开机时启动:systemctl enable firewalld.servic ...

  6. zabbix 通过自定义key完成网卡监控

    创建执行脚本: # cat /etc/zabbix/monitor_scripts/network.sh #!/bin/bash #set -x usage() { echo "Useage ...

  7. nodejs文件上传报错总结

    语法: fs.rename(oldPath,newPath,callback) 今天在使用formidable模块做图片上传处理的时候,fs.rename方法的报了一个这样的错:cross-devic ...

  8. Oracle客户端使用sqlldr导数据中文乱码问题解决方法

    String strctl = "OPTIONS (skip=0)" + // 0是从第一行开始 1是 从第二行 CHARACTERSET AL32UTF8 是为了解决导入中文为乱 ...

  9. [Windows Azure] Walkthrough to Configure System Center Management Pack for Windows Azure Fabric Preview for SCOM 2012 SP1 (with a MetricsHub Bonus)

    The wait is finally over. This is a huge update to the Azure Management Pack over the one that was r ...

  10. PHP网站环境搭配: Apache Http+PHP+Mysql

    Apache Http+PHP+Mysql 环境搭配 1. 先下载上述三个软件 都要下载对应系统的软件,mysql还可以再下载navicat for mysql. 2.  安装Apache Http ...