当前位置:网站首页>日撸代码300行学习笔记 Day 46
日撸代码300行学习笔记 Day 46
2022-04-21 22:37:00 【Leeyz_1】
1.快速排序
快速排序的思想是基于分治法的。每一次排序过后,都会确定一个数的位置,然后这个数的左边元素,全部小于这个数。而右边的所有元素中,全部大于这个元素,在确定这个元素的位置过后,分别对两个子序列进行相同排序,这也就形成了递归的过程。跳出条件为左大于等于右,此时也就代表着子序列排序完成。
2.代码
/**
*
***********************************
* @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
//递归跳出的条件
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.总结
快速排序是递归的,需要借助一个递归工作栈来保存每层递归调用的必要信息,最好情况下为O(log2n);最坏情况下,因为要进行n-1次递归调用,所以栈的深度为O(n);平均情况下,栈的深度为O(log2n)。快速排序的运行时间与划分是否对称有关,快排的最坏情况就发生在两个区域分别包含n-1个元素和0个元素时,这种最大程度的不对称性若发生在每层递归上,也就是说对于初始排序表基本有序或者基本逆序时,就得到最坏情况下的时间复杂度
。快排应该是所有内部排序算法中平均性能最优的排序算法了。
版权声明
本文为[Leeyz_1]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_45002520/article/details/124313720
边栏推荐
- Yarn online dynamic resource tuning
- Kotlin crawler app, Android development, interview preparation
- 「运维有小邓」企业如何解决AD域弱密码问题?
- Review questions and answers of building materials and structures in 2022 first-class registered architect examination
- 【matlab】matlab绘图操作技巧
- L1-059 敲笨钟 (20 分)
- 期货开户在手机上办理安全吗?需要线下办理吗?
- L1-056 guess the number (20 points)
- We sincerely invite you to sign up for the first openharmony developer growth plan sharing day
- INT 102_ TTL 09
猜你喜欢

CC00000.ZABBIX———————————————

君禾股份:2021年度营收增长稳健,受益产品出口业绩再创新高

OpenCV中的图像处理——离散傅里叶变换实例(11)

主流app开发工具,你头秃都没想到还能这样吧

L3-1 then don't worry (30 points) - pit point, test point analysis

Software designer - Chapter 6: system security analysis and design

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

Exercise questions and answers of basic theories and relevant laws and regulations in 2022 supervision engineer examination

Reading notes "autumn garden", "driftwood", "my fragrance"

模块三:外包学生管理系统-架构设计文档
随机推荐
openCV——几何变换
YARN线上动态资源调优
L1-055 who is the winner (10 points)
将模型训练外包真的安全吗?新研究:外包商可能植入后门,控制银行放款
一些网址的收藏
解锁OpenHarmony技术日!年度盛会,即将揭幕!
2022年监理工程师考试基本理论与相关法规练习题及答案
Industry analysis | development of Internet medicine
CC10000.MySQL———————————————
Is it safe to open futures account on mobile phone? Do you need to handle it offline?
CC10000. ZABBIX———————————————
UVM First Steps with UVM - Register Layer
File create file problem
树莓派3B+ 安装MJPG-streamer
OpenCV中的Core组件——输入输出XML, YAML(12)
Leetcode146 LRU cache - Simulation - bidirectional linked list - hash table - data structure - operating system
黑盒测试-数据的读取与输出方式
Collection of some websites
期货开户在手机上办理安全吗?需要线下办理吗?
L1-063 吃鱼还是吃肉 (10 分)