当前位置:网站首页>快速排序
快速排序
2022-06-30 02:13:00 【生活需要深度】
本章介绍排序算法中的快速排序。
目录
1. 快速排序介绍
2. 快速排序图文说明
3. 快速排序的时间复杂度和稳定性
4. 快速排序实现
4.1 快速排序C实现
4.2 快速排序C++实现
4.3 快速排序Java实现
转载请注明出处:快速排序 - 如果天空不死 - 博客园
更多内容:数据结构与算法系列 目录
快速排序介绍
快速排序(Quick Sort)使用分治法策略。
它的基本思想是:选择一个基准数,通过一趟排序将要排序的数据分割成独立的两部分;其中一部分的所有数据都比另外一部分的所有数据都要小。然后,再按此方法对这两部分数据分别进行快速排序,整个排序过程可以递归进行,以此达到整个数据变成有序序列。
快速排序流程:
(1) 从数列中挑出一个基准值。
(2) 将所有比基准值小的摆放在基准前面,所有比基准值大的摆在基准的后面(相同的数可以到任一边);在这个分区退出之后,该基准就处于数列的中间位置。
(3) 递归地把"基准值前面的子数列"和"基准值后面的子数列"进行排序。
快速排序图文说明
快速排序代码
/*
* 快速排序
*
* 参数说明:
* a -- 待排序的数组
* l -- 数组的左边界(例如,从起始位置开始排序,则l=0)
* r -- 数组的右边界(例如,排序截至到数组末尾,则r=a.length-1)
*/
void quick_sort(int a[], int l, int r)
{
if (l < r)
{
int i,j,x;
i = l;
j = r;
x = a[i];
while (i < j)
{
while(i < j && a[j] > x)
j--; // 从右向左找第一个小于x的数
if(i < j)
a[i++] = a[j];
while(i < j && a[i] < x)
i++; // 从左向右找第一个大于x的数
if(i < j)
a[j--] = a[i];
}
a[i] = x;
quick_sort(a, l, i-1); /* 递归调用 */
quick_sort(a, i+1, r); /* 递归调用 */
}
}下面以数列a={30,40,60,10,20,50}为例,演示它的快速排序过程(如下图)。

上图只是给出了第1趟快速排序的流程。在第1趟中,设置x=a[i],即x=30。
(01) 从"右 --> 左"查找小于x的数:找到满足条件的数a[j]=20,此时j=4;然后将a[j]赋值a[i],此时i=0;接着从左往右遍历。
(02) 从"左 --> 右"查找大于x的数:找到满足条件的数a[i]=40,此时i=1;然后将a[i]赋值a[j],此时j=4;接着从右往左遍历。
(03) 从"右 --> 左"查找小于x的数:找到满足条件的数a[j]=10,此时j=3;然后将a[j]赋值a[i],此时i=1;接着从左往右遍历。
(04) 从"左 --> 右"查找大于x的数:找到满足条件的数a[i]=60,此时i=2;然后将a[i]赋值a[j],此时j=3;接着从右往左遍历。
(05) 从"右 --> 左"查找小于x的数:没有找到满足条件的数。当i>=j时,停止查找;然后将x赋值给a[i]。此趟遍历结束!
按照同样的方法,对子数列进行递归遍历。最后得到有序数组!
快速排序的时间复杂度和稳定性
快速排序稳定性
快速排序是不稳定的算法,它不满足稳定算法的定义。
算法稳定性 -- 假设在数列中存在a[i]=a[j],若在排序之前,a[i]在a[j]前面;并且排序之后,a[i]仍然在a[j]前面。则这个排序算法是稳定的!
快速排序时间复杂度
快速排序的时间复杂度在最坏情况下是O(N2),平均的时间复杂度是O(N*lgN)。
这句话很好理解:假设被排序的数列中有N个数。遍历一次的时间复杂度是O(N),需要遍历多少次呢?至少lg(N+1)次,最多N次。
(01) 为什么最少是lg(N+1)次?快速排序是采用的分治法进行遍历的,我们将它看作一棵二叉树,它需要遍历的次数就是二叉树的深度,而根据完全二叉树的定义,它的深度至少是lg(N+1)。因此,快速排序的遍历次数最少是lg(N+1)次。
(02) 为什么最多是N次?这个应该非常简单,还是将快速排序看作一棵二叉树,它的深度最大是N。因此,快读排序的遍历次数最多是N次。
快速排序实现
/**
* 快速排序:C 语言
*
* @author skywang
* @date 2014/03/11
*/
#include <stdio.h>
// 数组长度
#define LENGTH(array) ( (sizeof(array)) / (sizeof(array[0])) )
/*
* 快速排序
*
* 参数说明:
* a -- 待排序的数组
* l -- 数组的左边界(例如,从起始位置开始排序,则l=0)
* r -- 数组的右边界(例如,排序截至到数组末尾,则r=a.length-1)
*/
void quick_sort(int a[], int l, int r)
{
if (l < r)
{
int i,j,x;
i = l;
j = r;
x = a[i];
while (i < j)
{
while(i < j && a[j] > x)
j--; // 从右向左找第一个小于x的数
if(i < j)
a[i++] = a[j];
while(i < j && a[i] < x)
i++; // 从左向右找第一个大于x的数
if(i < j)
a[j--] = a[i];
}
a[i] = x;
quick_sort(a, l, i-1); /* 递归调用 */
quick_sort(a, i+1, r); /* 递归调用 */
}
}
void main()
{
int i;
int a[] = {30,40,60,10,20,50};
int ilen = LENGTH(a);
printf("before sort:");
for (i=0; i<ilen; i++)
printf("%d ", a[i]);
printf("\n");
quick_sort(a, 0, ilen-1);
printf("after sort:");
for (i=0; i<ilen; i++)
printf("%d ", a[i]);
printf("\n");
}/**
* 快速排序:C++
*
* @author skywang
* @date 2014/03/11
*/
#include <iostream>
using namespace std;
/*
* 快速排序
*
* 参数说明:
* a -- 待排序的数组
* l -- 数组的左边界(例如,从起始位置开始排序,则l=0)
* r -- 数组的右边界(例如,排序截至到数组末尾,则r=a.length-1)
*/
void quickSort(int* a, int l, int r)
{
if (l < r)
{
int i,j,x;
i = l;
j = r;
x = a[i];
while (i < j)
{
while(i < j && a[j] > x)
j--; // 从右向左找第一个小于x的数
if(i < j)
a[i++] = a[j];
while(i < j && a[i] < x)
i++; // 从左向右找第一个大于x的数
if(i < j)
a[j--] = a[i];
}
a[i] = x;
quickSort(a, l, i-1); /* 递归调用 */
quickSort(a, i+1, r); /* 递归调用 */
}
}
int main()
{
int i;
int a[] = {30,40,60,10,20,50};
int ilen = (sizeof(a)) / (sizeof(a[0]));
cout << "before sort:";
for (i=0; i<ilen; i++)
cout << a[i] << " ";
cout << endl;
quickSort(a, 0, ilen-1);
cout << "after sort:";
for (i=0; i<ilen; i++)
cout << a[i] << " ";
cout << endl;
return 0;
}快速排序Java实现
实现代码(QuickSort.java)
/**
* 快速排序:Java
*
* @author skywang
* @date 2014/03/11
*/
public class QuickSort {
/*
* 快速排序
*
* 参数说明:
* a -- 待排序的数组
* l -- 数组的左边界(例如,从起始位置开始排序,则l=0)
* r -- 数组的右边界(例如,排序截至到数组末尾,则r=a.length-1)
*/
public static void quickSort(int[] a, int l, int r) {
if (l < r) {
int i,j,x;
i = l;
j = r;
x = a[i];
while (i < j) {
while(i < j && a[j] > x)
j--; // 从右向左找第一个小于x的数
if(i < j)
a[i++] = a[j];
while(i < j && a[i] < x)
i++; // 从左向右找第一个大于x的数
if(i < j)
a[j--] = a[i];
}
a[i] = x;
quickSort(a, l, i-1); /* 递归调用 */
quickSort(a, i+1, r); /* 递归调用 */
}
}
public static void main(String[] args) {
int i;
int a[] = {30,40,60,10,20,50};
System.out.printf("before sort:");
for (i=0; i<a.length; i++)
System.out.printf("%d ", a[i]);
System.out.printf("\n");
quickSort(a, 0, a.length-1);
System.out.printf("after sort:");
for (i=0; i<a.length; i++)
System.out.printf("%d ", a[i]);
System.out.printf("\n");
}
}上面3种语言的实现原理和输出结果都是一样的。下面是它们的输出结果:
before sort:30 40 60 10 20 50 after sort:10 20 30 40 50 60
边栏推荐
- 012_ switch
- Tencent released the first Office Photo 23 years ago. It's so chronological
- 谁再用Redis过期监听实现关闭订单,立马滚蛋!
- Let‘sPlayCurling
- Blitzkrieg companies with DDoS attacks exceeding 100gbps in 2014
- DDoS extortion attacks: what you need to know
- 搞透AQS原理(流程圖及同步隊列圖解)
- DDoS attacks and destructive ripple effects against online gamers
- Local page floating animation is realized with the help of scroll wheel
- Tools and life services
猜你喜欢

DMX的配置

当大学毕业感到迷茫怎么办?
![Scala basics [introduction and installation]](/img/c5/9e62070719e1e0db29b0e44b0f0bc1.png)
Scala basics [introduction and installation]

DTW learning (dynamic time warping) -- Thought and code implementation

Using grpcui to test asp Net core grpc service

【自然语言处理】【多模态】OFA:通过简单的sequence-to-sequence学习框架统一架构、任务和模态

012_ switch

004_ icon
![[MySQL 04] sauvegarde et restauration de la base de données MySQL sous Linux en utilisant MySQL Workbench 8.0 ce](/img/e7/fc2925a10ac5fb370dd221c3f4a46a.png)
[MySQL 04] sauvegarde et restauration de la base de données MySQL sous Linux en utilisant MySQL Workbench 8.0 ce
![[protection mode] segment descriptor](/img/23/19b12c496da437fbf03829b7b4e3b8.jpg)
[protection mode] segment descriptor
随机推荐
搞透AQS原理(流程图及同步队列图解)
013_ slider
DDoS "fire drill" service urges companies to prepare
DDoS attacks - are we really at war?
003_ color
Is it safe to open an account in Sinosteel futures?
day33
Oppo mobile phone search
Summary of DOM
Matlab 2012a drawing line segment with arrow
[Galaxy Kirin V10] [desktop] Firefox browser settings home page does not take effect
Jenkins continuous integration environment construction VII (Jenkins parametric construction)
7 — filter
A keepalived high availability accident made me learn it again!
Local page floating animation is realized with the help of scroll wheel
Using grpcui to test asp Net core grpc service
Electron FAQ 54 - make your own fireworks based on electron
dhu编程练习
If mybaits cannot query the data, it can query how to change it in the database
9 - regular check set