当前位置:网站首页>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;
}
边栏推荐
- Figure application details
- P2102 地砖铺设(dfs&贪心)
- Yyds dry goods inventory OSI & tcp/ip
- Several important classes in unity
- P2022 interesting numbers (binary & digit DP)
- SharedPreferences source code analysis
- SharedPreferences 源码分析
- Basic explanation of turtle module - draw curve
- 2327. 知道秘密的人数(递推)
- [Chongqing Guangdong education] Suzhou University English film and Television Appreciation reference materials
猜你喜欢
Delete subsequence < daily question >
Unity screen coordinates ugui coordinates world coordinates conversion between three coordinate systems
The implementation of the maize negotiable digital warehouse receipt standard will speed up the asset digitization process of the industry
Sorting out the latest Android interview points in 2022 to help you easily win the offer - attached is the summary of Android intermediate and advanced interview questions in 2022
Selection of slow motion function
CertBot 更新证书失败解决
Implementation of knowledge consolidation source code 1: epoll implementation of TCP server
Is the mode of education together - on campus + off campus reliable
CADD课程学习(7)-- 模拟靶点和小分子相互作用 (柔性对接 AutoDock)
Comprehensive ability evaluation system
随机推荐
Knowledge consolidation source code implementation 3: buffer ringbuffer
Sqlserver query results are not displayed in tabular form. How to modify them
CADD课程学习(7)-- 模拟靶点和小分子相互作用 (柔性对接 AutoDock)
R note prophet
Database - MySQL storage engine (deadlock)
One question per day (Mathematics)
C. The Third Problem(找规律)
[05-1, 05-02, 05-03] network protocol
Lora gateway Ethernet transmission
I'd like to ask about the current MySQL CDC design. In the full volume phase, if a chunk's binlog backfill phase,
P3500 [POI2010]TES-Intelligence Test(二分&离线)
11. Intranet penetration and automatic refresh
Mysql database storage engine
Implementation of knowledge consolidation source code 1: epoll implementation of TCP server
1008 circular right shift of array elements (20 points)
题解:《单词覆盖还原》、《最长连号》、《小玉买文具》、《小玉家的电费》
Recommendation | recommendation of 9 psychotherapy books
Certbot failed to update certificate solution
Script lifecycle
[Chongqing Guangdong education] Suzhou University English film and Television Appreciation reference materials