当前位置:网站首页>Quick sorting, simple and easy to understand principle description, with C code implementation,
Quick sorting, simple and easy to understand principle description, with C code implementation,
2022-06-21 10:28:00 【ah_ yl】
Quick sorting is a practical algorithm in sorting ;
The principle of fast platoon is :( From the Small To Big Sort array arr[N] For example )
1、 First find an element in the array as the anchor point ( Suppose it is the first element in the array arr[0])
2、 Traversal array Will be less than the number of anchor points ( hypothesis k A little bit ) Before array , Exchange these numbers to arr[1]~arr[k];
3、 Swap anchor points arr[0] and arr[k], here arr[k] The number before the value at position is smaller than it , The latter numbers are bigger than it ;
4、 And then the array [ arr[0],arr[k-1] ),( arr[k+1],arr[N] ] These two arrays are then recursively operated as described above
5、 Recurse until the left element subscript is greater than or equal to the right element subscript , Sort stop
C The language implementation is as follows ;
//c The core code of the language is as follows ;
void qsort(int v[],int left,int right)
{
int i,pos;
if (left >= right)
return;
// Take the first element as the anchor point
pos = left;
// Traversal array , Will be less than the number of anchor points swap To the front of the array
for (i=left+1;i<=right;++i) {
if (v[i]<v[left])
swap(v,i,++pos);
}
// Compare the number of anchor points with swap To its correct position pos;
swap(v,left,pos);
// Recurs the process
qsort(v,left,pos-1);
qsort(v,pos+1,right);
}
// among swap To exchange an array with the subscript i and j Function of ;
void swap(int v[],int i,int j)
{
int temp;
temp = v[i];
v[i] = v[j];
v[j] = temp;
}
//
int main()
{
int arr[10] = {7,1,3,9,2,6,1,7,3,5};
qsort(arr,0,9);
return 0;
}边栏推荐
- 108. detailed use of Redux (case)
- Introduction to ground plane in unity
- The memory allocation of the program, the storage of local const and global const in the system memory, and the perception of pointers~
- js正则-梳理
- Lodash real on demand approach
- Start from scratch 10- background management system development
- DSP online upgrade (4) -- functions implemented by bootloader
- 110. JS event loop and setimmediate, process.nexttick
- About Alipay - my savings plan - interest rate calculation instructions
- Tensorflow, danger! Google itself is the one who abandoned it
猜你喜欢

111. solve the problem of prohibiting scripts from running on vs code. For more information, see error reporting

Bit segment operation of STM32, LED lighting based on stm32f103xxx multiple methods

The spingboot microservice is packaged into a docker image and connected to the database

简易的安卓天气app(三)——城市管理、数据库操作

DSP online upgrade (1) -- understand the startup process of DSP chip

leetcode:715. Range 模块【无脑segmentTree】

Matplotlib 绘制圆环图的两种方法!

Xidian AI ranked higher than Qingbei in terms of AI major, and Nantah ranked first in China in terms of Software Science in 2022

The memory allocation of the program, the storage of local const and global const in the system memory, and the perception of pointers~

Es composite query workload evaluation
随机推荐
[actual combat] STM32 FreeRTOS migration series tutorial 2: FreeRTOS mutually exclusive semaphores
移动应用开发学习通测试题答案
Brief introduction of quality control conditions before genotype filling
还在直接用localStorage么?全网最细:本地存储二次封装(含加密、解密、过期处理)
leetcode:715. Range 模块【无脑segmentTree】
Mythical games announced its cooperation with kakao games, a leading Korean game publisher, to promote business expansion in the Asia Pacific Region
The spingboot microservice is packaged into a docker image and connected to the database
TC software outline design document (mobile group control)
如何将MindSpore模型转ONNX格式并使用OnnxRuntime推理---开发测试篇
Vscode high-speed download address -- solve the problem of slow vscode Download
Uni app advanced creation component / native rendering [Day9]
Judge the data type of JS
流式编程:流支持,创建,中间操作以及终端操作
使用shapeit进行单倍型分析
【云原生 | Kubernetes篇】Kubernetes 配置(十五)
并发编程高级部分:并行流,Tasks和Executors以及CompletableFuture类
Optional classes, convenience functions, creating options, optional object operations, and optional streams
Get the data in the configuration file properties
JWT与Session的比较
Bit segment operation of STM32, LED lighting based on stm32f103xxx multiple methods