当前位置:网站首页>Quick sort
Quick sort
2022-06-30 02:17:00 【Life needs depth】
This chapter introduces quick sort in sorting algorithm .
Catalog
1. Quick sort Introduction
2. Quick sort text description
3. Time complexity and stability of quick sort
4. Fast sort implementation
4.1 Quick sort C Realization
4.2 Quick sort C++ Realization
4.3 Quick sort Java Realization
Reprint please indicate the source : Quick sort - If the sky doesn't die - Blog Garden
More : Data structure and algorithm series Catalog
Quick sort Introduction
Quick sort (Quick Sort) Use divide and conquer strategy .
The basic idea is this : Choose a base number , Divide the data to be sorted into two independent parts by one sorting ; All the data in one part is smaller than all the data in the other part . then , Then according to this method, the two parts of data are sorted quickly , The whole sort process can be done recursively , So that the whole data becomes an ordered sequence .
Quick sort process :
(1) Pick a base value from the sequence .
(2) Place everything smaller than the benchmark in front of the benchmark , All those larger than the benchmark are placed behind the benchmark ( The same number can go to either side ); After the partition exits , The benchmark is in the middle of the sequence .
(3) Recursively " The subsequence before the reference value " and " The subsequence after the reference value " Sort .
Quick sort text description
Quick sort code
/*
* Quick sort
*
* Parameter description :
* a -- Array to sort
* l -- The left boundary of the array ( for example , Sort from the start , be l=0)
* r -- The right boundary of the array ( for example , Sort to the end of the array , be 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--; // Find the first less than... From right to left x Number of numbers
if(i < j)
a[i++] = a[j];
while(i < j && a[i] < x)
i++; // Find the first one greater than... From left to right x Number of numbers
if(i < j)
a[j--] = a[i];
}
a[i] = x;
quick_sort(a, l, i-1); /* Recursively call */
quick_sort(a, i+1, r); /* Recursively call */
}
}The following is a sequence of numbers a={30,40,60,10,20,50} For example , Demonstrate its quick sort process ( Here's the picture ).

The picture above just shows the second 1 A quick sort of process . In the 1 During the trip , Set up x=a[i], namely x=30.
(01) from " Right --> Left " Find less than x Number of numbers : Find the number that satisfies the condition a[j]=20, here j=4; And then a[j] assignment a[i], here i=0; And then traverse from left to right .
(02) from " Left --> Right " Find greater than x Number of numbers : Find the number that satisfies the condition a[i]=40, here i=1; And then a[i] assignment a[j], here j=4; And then traverse from right to left .
(03) from " Right --> Left " Find less than x Number of numbers : Find the number that satisfies the condition a[j]=10, here j=3; And then a[j] assignment a[i], here i=1; And then traverse from left to right .
(04) from " Left --> Right " Find greater than x Number of numbers : Find the number that satisfies the condition a[i]=60, here i=2; And then a[i] assignment a[j], here j=3; And then traverse from right to left .
(05) from " Right --> Left " Find less than x Number of numbers : There is no number that satisfies the condition . When i>=j when , Stop searching ; And then x Assign a value to a[i]. The tour ends !
Follow the same method , Recursive traversal of subsequences . Finally, we get an ordered array !
Time complexity and stability of quick sort
Quicksort stability
Quicksort is an unstable algorithm , It doesn't meet the definition of stable algorithm .
Algorithm stability -- Let's assume that there exists in a sequence a[i]=a[j], If before sorting ,a[i] stay a[j] front ; And after sorting ,a[i] Still in a[j] front . Then the sorting algorithm is stable !
Time complexity of quick sorting
The time complexity of quicksort in the worst case is O(N2), The average time complexity is O(N*lgN).
This sentence is very understandable : Let's assume that the sequence being sorted has N Number . The time complexity of traversing once is O(N), How many times does it take to traverse ? At least lg(N+1) Time , most N Time .
(01) Why at least lg(N+1) Time ? Quick sort is traversed by divide and conquer method , We think of it as a binary tree , The number of times it needs to traverse is the depth of the binary tree , And according to the definition of complete binary tree , Its depth is at least lg(N+1). therefore , The minimum traversal times of quicksort is lg(N+1) Time .
(02) Why at most N Time ? This should be very simple , Or think of quicksort as a binary tree , It's the deepest N. therefore , The most traversal times of quick read sort is N Time .
Fast sort implementation
Quick sort C Realization
Implementation code (quick_sort.c)
/**
* Quick sort :C Language
*
* @author skywang
* @date 2014/03/11
*/
#include <stdio.h>
// The length of the array
#define LENGTH(array) ( (sizeof(array)) / (sizeof(array[0])) )
/*
* Quick sort
*
* Parameter description :
* a -- Array to sort
* l -- The left boundary of the array ( for example , Sort from the start , be l=0)
* r -- The right boundary of the array ( for example , Sort to the end of the array , be 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--; // Find the first less than... From right to left x Number of numbers
if(i < j)
a[i++] = a[j];
while(i < j && a[i] < x)
i++; // Find the first one greater than... From left to right x Number of numbers
if(i < j)
a[j--] = a[i];
}
a[i] = x;
quick_sort(a, l, i-1); /* Recursively call */
quick_sort(a, i+1, r); /* Recursively call */
}
}
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");
} Quick sort C++ Realization
Implementation code (QuickSort.cpp)
/**
* Quick sort :C++
*
* @author skywang
* @date 2014/03/11
*/
#include <iostream>
using namespace std;
/*
* Quick sort
*
* Parameter description :
* a -- Array to sort
* l -- The left boundary of the array ( for example , Sort from the start , be l=0)
* r -- The right boundary of the array ( for example , Sort to the end of the array , be 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--; // Find the first less than... From right to left x Number of numbers
if(i < j)
a[i++] = a[j];
while(i < j && a[i] < x)
i++; // Find the first one greater than... From left to right x Number of numbers
if(i < j)
a[j--] = a[i];
}
a[i] = x;
quickSort(a, l, i-1); /* Recursively call */
quickSort(a, i+1, r); /* Recursively call */
}
}
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;
} Quick sort Java Realization
Implementation code (QuickSort.java)
/**
* Quick sort :Java
*
* @author skywang
* @date 2014/03/11
*/
public class QuickSort {
/*
* Quick sort
*
* Parameter description :
* a -- Array to sort
* l -- The left boundary of the array ( for example , Sort from the start , be l=0)
* r -- The right boundary of the array ( for example , Sort to the end of the array , be 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--; // Find the first less than... From right to left x Number of numbers
if(i < j)
a[i++] = a[j];
while(i < j && a[i] < x)
i++; // Find the first one greater than... From left to right x Number of numbers
if(i < j)
a[j--] = a[i];
}
a[i] = x;
quickSort(a, l, i-1); /* Recursively call */
quickSort(a, i+1, r); /* Recursively call */
}
}
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");
}
}above 3 The implementation principle and output results of the two languages are the same . Here is their output :
before sort:30 40 60 10 20 50 after sort:10 20 30 40 50 60
边栏推荐
- Want to change careers, but don't know what you want to do?
- DDoS attacks - are we really at war?
- Thinking carefully and fearfully: a software can be transmitted online to monitor whether employees want to "run away"
- dhu编程练习
- Scala基础【入门及安装】
- If mybaits cannot query the data, it can query how to change it in the database
- Knowledge payment cannot escape the essence of "anxiety"
- 实现VS每次只运行一个源文件
- DDoS threat situation gets worse
- 207. curriculum - graph theory, depth traversal
猜你喜欢

What problems can cloud storage architecture solve for Devops?
![[naturallanguageprocessing] [multimodality] ofa: unified architecture, tasks and modes through a simple sequence to sequence learning framework](/img/c9/7be54c428212d7226cbbbb4800fcdb.png)
[naturallanguageprocessing] [multimodality] ofa: unified architecture, tasks and modes through a simple sequence to sequence learning framework

一种跳板机的实现思路

Illustration Google V8 19: asynchronous programming (II): how does V8 implement async/await?

33Mysql

Jenkins continuous integration environment construction VII (Jenkins parametric construction)

Share the source code of the website of graduation student record
![[Galaxy Kirin V10] [desktop] Firefox browser settings home page does not take effect](/img/96/e3afb2b9683cce7645afd483cc0844.png)
[Galaxy Kirin V10] [desktop] Firefox browser settings home page does not take effect

(4) Blender source code analysis flash window display process

希尔排序
随机推荐
Recommendations for agileplm database parameter optimization
7 — filter
網上炒股安全麼?炒股需要開戶嗎?
9 - regular check set
Simple implementation of unity object pool
Matlab 2012a drawing line segment with arrow
DDoS surge in mobile and data centers
[MySQL 04] use MySQL workbench 8.0 CE to back up and restore MySQL databases in Linux
Matlab 2012a 绘制带箭头的线段
DHU programming exercise
002_ container
【MySQL 05】SUSE 12 SP5 安装MySQL后第一次修改mysql密码
Record an oom exception in production
007_ checkbox
Weekly recommended short video: why is the theory correct but can not get the expected results?
001_ layout
堆排序
归并排序
Learning C language from scratch day 026
Radware warns about the next round of DDoS Attacks