当前位置:网站首页>Day code 300 lines learning notes day 46
Day code 300 lines learning notes day 46
2022-04-21 22:39:00 【Leeyz_ one】
1. Quick sort
The idea of quick sort is based on divide and conquer . After each sorting , Will determine the position of a number , Then the left element of this number , All less than this number . And of all the elements on the right , All greater than this element , After determining the position of this element , Sort the two subsequences in the same way , This forms a recursive process . The jump out condition is that the left is greater than or equal to the right , At this time, it means that the sub sequence sorting is completed .
2. Code
/**
*
***********************************
* @Title: quickSortRecursive
* @Description:
* @param: @param paraStart
* @param: @param paraEnd
* @return: void
***********************************
*/
public void quickSortRecursive(int paraStart, int paraEnd) {
//null
if(paraStart >= paraEnd)
return;
int tempPivot = data[paraEnd].key;
DataNode tempNodeForSwap;
int tempLeft = paraStart;
int tempRight = paraEnd - 1;
while(true) {
while((data[tempLeft].key < tempPivot)&&(tempLeft <tempRight)) {
tempLeft++;
}//Of while
while((data[tempRight].key >= tempPivot) && (tempLeft < tempRight)) {
tempRight--;
}//Of while
// The conditions for recursion to jump out
if(tempLeft < tempRight) {
// Swap.
System.out.println("Swapping " + tempLeft + " and " + tempRight);
tempNodeForSwap = data[tempLeft];
data[tempLeft] = data[tempRight];
data[tempRight] = tempNodeForSwap;
}
else {
break;
}//of if
}//of while
// Swap
if (data[tempLeft].key > tempPivot) {
tempNodeForSwap = data[paraEnd];
data[paraEnd] = data[tempLeft];
data[tempLeft] = tempNodeForSwap;
} else {
tempLeft++;
} // Of if
System.out.print("From " + paraStart + " to " + paraEnd + ": ");
System.out.println(this);
quickSortRecursive(paraStart, tempLeft - 1);
quickSortRecursive(tempLeft + 1, paraEnd);
}// Of quickSortRecursive
public void quickSort() {
quickSortRecursive(0, length - 1);
}// Of quickSort
/**
*********************
* Test the method.
*********************
*/
public static void quickSortTest() {
int[] tempUnsortedKeys = { 1, 3, 12, 10, 5, 7, 9 };
String[] tempContents = { "if", "then", "else", "switch", "case", "for", "while" };
DataArray tempDataArray = new DataArray(tempUnsortedKeys, tempContents);
System.out.println(tempDataArray);
tempDataArray.quickSort();
System.out.println("Result\r\n" + tempDataArray);
}// ofquickSortTest
/**
*
***********************************
* @Title: main
* @Description:
* @param: @param args
* @return: void
***********************************
*/
public static void main(String args[]) {
System.out.println("\r\n-------quickSortTest-------");
quickSortTest();
}// Of main
3. summary
Quick sorting is recursive , You need to use a recursive work stack to save the necessary information of recursive calls at each layer , In the best case O(log2n); In the worst case , Because there's going to be n-1 Recursive calls , So the depth of the stack is O(n); On average , The depth of the stack is O(log2n). The running time of quick sort is related to whether the partition is symmetrical , The worst case of fast platoon occurs when two areas contain n-1 Elements and 0 Element time , If this maximum asymmetry occurs at each level of recursion , In other words, when the initial sort table is basically in order or basically in reverse order , The worst-case time complexity is obtained
. Fast sorting should be the best sorting algorithm with the best average performance among all internal sorting algorithms .
版权声明
本文为[Leeyz_ one]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204212237067087.html
边栏推荐
- Overview of MySQL database paradigm design theory
- 【中南林业科技大学】【陈】第九周作业三角形异常处理
- Daily question - leetcode824 - goat Latin - simulation
- L1-058 6 turned over (15 points)
- 算法--合并K个升序链表(Kotlin)
- Analysis on the underlying principle of MySQL transaction and isolation level
- Opencv -- histogram processing
- L1-063 吃鱼还是吃肉 (10 分)
- Pyqt5 + opencv operate local camera
- Eventbridge integrated cloud service practice
猜你喜欢

Software designer - Chapter 6: system security analysis and design

How can "Xiaodeng" enterprises solve the problem of weak password in AD domain?

After learning the linked list, don't you find some classic examples to consolidate your knowledge? The five OJ classic examples of the linked list, can you? Let's have a look (knowledge analysis + gr

CC00012.MySQL———————————————

HTTP cache notes

1956年高考数学

一些网址的收藏

L1-058 6 turned over (15 points)

对称二叉树之经典递归思想 + 递归借助栈转迭代

软件设计师——第六章:系统安全分析与设计
随机推荐
模块三:外包学生管理系统-架构设计文档
mysql事务和隔离级别底层原理浅析
L1-063 吃鱼还是吃肉 (10 分)
OpenCV中的图像处理——离散傅里叶变换实例(11)
Lesson 5: correlation coefficient
Sorting methods (8 kinds) detailed explanation 7 - counting sorting
Analysis and interpretation of specialized special new agency and specialized special new agency policy, with a subsidy of RMB 200000-1 million
L1-061 新胖子公式 (10 分)
L2-3 完全二叉树的层序遍历 (25 分)——递归还原二叉树
[Central South University of forestry science and technology] [Chen] ninth week operation triangle exception handling
CC00000.ZABBIX———————————————
Industry analysis | development of Internet medicine
【matlab】matlab绘图操作技巧
We sincerely invite you to sign up for the first openharmony developer growth plan sharing day
CC10000.MySQL———————————————
Deep understanding of MySQL locks
君禾股份:2021年度营收增长稳健,受益产品出口业绩再创新高
Mysql的并发参数调整详解
It is revealed that Xiaomi has new machines this month, and many of its products are ready to go
Fundamentals of Power Electronics