当前位置:网站首页>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;
}边栏推荐
- Sentinel sliding window traffic statistics
- npm命令--安装依赖包--用法/详解
- 1008 circular right shift of array elements (20 points)
- How to solve the problem of slow downloading from foreign NPM official servers—— Teach you two ways to switch to Taobao NPM image server
- [try to hack] John hash cracking tool
- Comprehensive ability evaluation system
- About some basic DP -- those things about coins (the basic introduction of DP)
- Etcd database source code analysis -- etcdserver bootstrap initialization storage
- Complete list of common functions of turtle module
- 【HBZ分享】云数据库如何定位慢查询
猜你喜欢

解决“C2001:常量中有换行符“编译问题
![[Yu Yue education] reference materials of complex variable function and integral transformation of Northwestern Polytechnic University](/img/22/ead74bc121a64910ef6ef374cd029b.png)
[Yu Yue education] reference materials of complex variable function and integral transformation of Northwestern Polytechnic University

Sqlserver query results are not displayed in tabular form. How to modify them

Implementation of knowledge consolidation source code 1: epoll implementation of TCP server

Vulnerability discovery - vulnerability probe type utilization and repair of web applications

Visio draws Tai Chi

Basic explanation of turtle module - draw curve

How to solve the problem of slow downloading from foreign NPM official servers—— Teach you two ways to switch to Taobao NPM image server

Mysql database storage engine

8. Static file
随机推荐
How to solve the problem of slow downloading from foreign NPM official servers—— Teach you two ways to switch to Taobao NPM image server
11. Intranet penetration and automatic refresh
Data processing methods - smote series and adasyn
Comprehensive ability evaluation system
JVM garbage collector concept
Overturn your cognition? The nature of get and post requests
[tomato assistant installation]
Unity screen coordinates ugui coordinates world coordinates conversion between three coordinate systems
Meet diverse needs: jetmade creates three one-stop development packages to help efficient development
About some basic DP -- those things about coins (the basic introduction of DP)
729. My schedule I (set or dynamic open point segment tree)
Mysql database storage engine
Hashlimit rate control
Easyrecovery reliable and toll free data recovery computer software
Etcd database source code analysis -- etcdserver bootstrap initialization storage
How do programmers teach their bosses to do things in one sentence? "I'm off duty first. You have to work harder."
[Chongqing Guangdong education] Suzhou University English film and Television Appreciation reference materials
In depth MySQL transactions, stored procedures and triggers
CertBot 更新证书失败解决
word封面下划线