当前位置:网站首页>Quick sort
Quick sort
2022-07-06 04:36:00 【A distant youth】
The basic idea of quick sorting : Separate the records to be arranged into two independent parts by one sorting , The keywords of one part of the records are smaller than those of the other part , Then the two parts of records can be sorted separately , To get the whole sequence in order .
6.1 Algorithm description
Fast sorting uses divide and conquer to put a string (list) It's divided into two strings (sub-lists). The specific algorithm is described as follows :
- Pick a member of the sequence , be called “ The benchmark ”(pivot);
- Reorder the sequence , All elements smaller than the base value are placed in front of the base value , All elements that are larger than the base value are placed after the base value ( The same number can go to either side ). After the partition exits , The benchmark is in the middle of the sequence . This is called a partition (partition) operation ;
- recursively (recursive) Sort the substrings of elements that are less than the base value and elements that are greater than the base value .
6.2 Dynamic diagram demonstration

6.3 Code implementation
function quickSort(arr, left, right) {
var len = arr.length,
partitionIndex,
left = typeof left != 'number' ? 0 : left,
right = typeof right != 'number' ? len - 1 : right;
if (left < right) {
partitionIndex = partition(arr, left, right);
quickSort(arr, left, partitionIndex-1);
quickSort(arr, partitionIndex+1, right);
}
return arr;
}
function partition(arr, left ,right) { // Partition operation
var pivot = left, // Set reference value (pivot)
index = pivot + 1;
for (var i = index; i <= right; i++) {
if (arr[i] < arr[pivot]) {
swap(arr, i, index);
index++;
}
}
swap(arr, pivot, index - 1);
return index-1;
}
function swap(arr, i, j) {
var temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}边栏推荐
- During pycharm debugging, the view is read only and pause the process to use the command line appear on the console input
- View workflow
- P2102 地砖铺设(dfs&贪心)
- POI add border
- [face recognition series] | realize automatic makeup
- canal同步mysql数据变化到kafka(centos部署)
- [Zhao Yuqiang] deploy kubernetes cluster with binary package
- The implementation of the maize negotiable digital warehouse receipt standard will speed up the asset digitization process of the industry
- [Chongqing Guangdong education] Suzhou University English film and Television Appreciation reference materials
- 【HBZ分享】云数据库如何定位慢查询
猜你喜欢

Dry goods collection | Vulkan game engine video tutorial

8. Static file

The implementation of the maize negotiable digital warehouse receipt standard will speed up the asset digitization process of the industry

CADD course learning (7) -- Simulation of target and small molecule interaction (flexible docking autodock)

How to estimate the population with samples? (mean, variance, standard deviation)

Digital children < daily question> (Digital DP)

During pycharm debugging, the view is read only and pause the process to use the command line appear on the console input

Redis —— Redis In Action —— Redis 实战—— 实战篇一 —— 基于 Redis 的短信登录功能 —— Redis + Token 的共享 session 应用— 有代码

Fedora/REHL 安装 semanage

Ue5 small knowledge points to enable the setting of lumen
随机推荐
The value of two date types is subtracted and converted to seconds
Dynamic programming (tree DP)
MLAPI系列 - 04 - 网络变量和网络序列化【网络同步】
Mysql database storage engine
【HBZ分享】ArrayList的增删慢查询快的原因
Yyds dry goods inventory OSI & tcp/ip
POI add border
Cross domain and jsonp details
Introduction to hashtable
Meet diverse needs: jetmade creates three one-stop development packages to help efficient development
Scala function advanced
Coreldraw2022 new version new function introduction cdr2022
2328. 网格图中递增路径的数目(记忆化搜索)
How to realize automatic playback of H5 video
Redis - redis in action - redis actual combat - actual combat Chapter 1 - SMS login function based on redis - redis + token shared session application - with code
Tengine kernel parameters
E. Best Pair
SharedPreferences 源码分析
Yyds dry inventory automatic lighting system based on CC2530 (ZigBee)
Comprehensive ability evaluation system