快速排序算法(Quicksort)
快速排序算法是对集合中元素进行排序最通用的算法,俗称快排,其算法的时间复杂度为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)的更多相关文章
- Javascript算法系列之快速排序(Quicksort)
原文出自: http://www.nczonline.net/blog/2012/11/27/computer-science-in-javascript-quicksort/ https://gis ...
- 排序算法之快速排序(Quicksort)解析
一.快速排序算法的优点,为什么称之为快排? Quicksort是对归并排序算法的优化,继承了归并排序的优点,同样应用了分治思想. 所谓的分治思想就是对一个问题“分而治之”,用分治思想来解决问题需要两个 ...
- 排序算法五:随机化快速排序(Randomized quicksort)
上一篇提到,快速排序的平均时间复杂度是O(nlgn),比其他相同时间复杂度的堆排序.归并排序都要快,但这是有前提的,就是假定要排序的序列是随机分布的,而不是有序的.实际上,对于已经排好的序列,如果用快 ...
- Algorithms - Quicksort - 快速排序算法
相关概念 快速排序法 Quicksort 也是一个分治思想的算法. 对一个子数组 A[p: r] 进行快速排序的三步分治过程: 1, 分解. 将数组 A[p : r] 被划分为两个子数组(可能为空) ...
- C#数据结构与算法系列(二十二):快速排序算法(QuickSort)
1.介绍 快速排序(QuickSort)是对冒泡排序的一种改进,基本思想是:通过一趟排序将要排序的数据分割成独立的两部分, 其中一部分的所有数据都比另一部分的所有数据都要小,然后再按此方法对这两部分数 ...
- 快速排序算法 java 实现
快速排序算法 java 实现 快速排序算法Java实现 白话经典算法系列之六 快速排序 快速搞定 各种排序算法的分析及java实现 算法概念 快速排序是C.R.A.Hoare于1962年提出的一种划分 ...
- C# 集合扩展快速排序算法
/// <summary> /// 对集合进行排序,如 /// List<Person> users=new List<Person>(){.......} /// ...
- 快速排序算法-C语言实现
注:本篇内容为翻译,之所以选择这篇进行翻译原因是该文章含有动画,能够更加直观地展示快速排序.同时,可以仔细看一下代码,代码中把结构化的思想给予了更加充分地表现.按照功能进行模块划分的思想得到了彻底地贯 ...
- C#快速排序算法基础入门篇
相信算法对于许多开发人员来说都是一大难点,之所以难,就像设计模式一样,许多人在阅读之后,没有很好地理解,也不愿意动手上机操作,只停留在理论的学习上面,随着时间推移就慢慢淡忘. 有些东西,你可以发明创造 ...
随机推荐
- dubbo调用服务出现如下异常
log4j:WARN No appenders could be found for logger (org.springframework.context.support.ClassPathXmlA ...
- Android呼叫管理服务之会话发起协议(SIP)API
原文:http://android.eoe.cn/topic/android_sdk Android提供了一个支持会话发起协议(SIP)的API,这可以让你添加基于SIP的网络电话功能到你的应用程序. ...
- Android网络功能之会话发起协议SIP
原文:http://android.eoe.cn/topic/android_sdk * 会话发起协议* Android提供了一个支持会话发起协议(SIP)的API,这可以让你添加基于SIP的网络电话 ...
- 基于node-webkit的web项目打包方案
下载node-webkit https://github.com/rogerwang/node-webkit 找到Downloads这一小节,然后下载对应平台的node-webkit预编译包.(为了介 ...
- nginx.conf中关于nginx-rtmp-module配置指令详解
译序:截至 Jul 8th,2013 官方公布的最新 Nginx RTMP 模块 nginx-rtmp-module 指令详解.指令Corertmp语法:rtmp { ... }上下文:根描述:保存所 ...
- [Windows Azure] What is Windows Azure Active Directory?
What is Windows Azure Active Directory? Windows Azure Active Directory is a service that provides id ...
- android 调用系统相机拍照 获取原图
好吧,为了这个问题又折腾了一整天.之前在网上找来的方法,如果在onActivityResult中直接用data.getData()的方式来生成bitmap,其实获取的是拍照生成的缩略图!看看尺寸就 ...
- 【Android】json格式详解
JSON有两种结构 1. “名称/值”对的集合(A collection of name/value pairs). 不同的语言中,它被理解为对象(object),记录(record),结构(s ...
- 【Acm】算法之美—Fire Net
题目概述:Fire Net Suppose that we have a square city with straight streets. A map of a city is a square ...
- 在IIS服务器上屏蔽IP的访问
今天就跟大家分享一下在IIS服务器上如何屏蔽特定IP的访问,希望对大家有所帮助. 第一种方法:通过iis中的ip地址和域名限制. 此方法简单有效,建议使用 点击网站--右键属性--目录安全性--IP地 ...