当前位置:网站首页>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;
}边栏推荐
- Solution of storage bar code management system in food industry
- flink sql 能同时读多个topic吗。with里怎么写
- QML和QWidget混合开发(初探)
- HotSpot VM
- Can CDC pull the Oracle table in full
- MySQL reported an error datetime (0) null
- Lombok原理和同时使⽤@Data和@Builder 的坑
- [Zhao Yuqiang] deploy kubernetes cluster with binary package
- web工程导入了mysql驱动jar包却无法加载到驱动的问题
- How does computer nail adjust sound
猜你喜欢

The value of two date types is subtracted and converted to seconds

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

Easyrecovery靠谱不收费的数据恢复电脑软件

Recommendation | recommendation of 9 psychotherapy books

Lora gateway Ethernet transmission

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

Delete subsequence < daily question >

Recommendation system (IX) PNN model (product based neural networks)

Uva1592 Database

满足多元需求:捷码打造3大一站式开发套餐,助力高效开发
随机推荐
word封面下划线
Redis - redis in action - redis actual combat - actual combat Chapter 1 - SMS login function based on redis - redis + token shared session application - with code
Canal synchronizes MySQL data changes to Kafka (CentOS deployment)
解决“C2001:常量中有换行符“编译问题
Implementation of knowledge consolidation source code 1: epoll implementation of TCP server
P3500 [POI2010]TES-Intelligence Test(二分&离线)
newton interpolation
English Vocabulary - life scene memory method
Easyrecovery reliable and toll free data recovery computer software
Database - MySQL storage engine (deadlock)
Case of Jiecode empowerment: professional training, technical support, and multiple measures to promote graduates to build smart campus completion system
Jd.com 2: how to prevent oversold in the deduction process of commodity inventory?
Coreldraw2022 new version new function introduction cdr2022
Mixed development of QML and QWidget (preliminary exploration)
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
Digital children < daily question> (Digital DP)
2327. Number of people who know secrets (recursive)
View 工作流程
How does vs change the project type?
HotSpot VM