当前位置:网站首页>快速排序
快速排序
2022-06-10 22:38:00 【Li_XiaoJin】
关于快速排序的相关内容
快速排序的思路:
- 从数组中选一个数做为基准值,一般选第一个数,或者最后一个数。
- 采用双指针(头尾两端)遍历,从左往右找到比基准值大的第一个数,从右往左找到比基准值小的第一个数,交换两数位置,直到头尾指针相等或头指针大于尾指针,把基准值与头指针的数交换。这样一轮之后,左边的数就比基准值小,右边的数就比基准值大。
- 对左边的数列,重复上面1,2步骤。对右边重复1,2步骤。
- 左右两边数列递归结束后,排序完成。
https://lixj.fun/upload/2021/07/%E5%BF%AB%E9%80%9F%E6%8E%92%E5%BA%8F-eadb58e4707d48d6a047cecc1b637849.gif
算法复杂度:O(nlogn)
算法空间复杂度:O(1)
算法稳定性:不稳定
public class QuickSort {
public static void quickSort(int[] arr, int low, int high) {
int i,j,temp;
if (i > j) {
return;
}
i = low;
j = high;
temp = arr[low];
while (i < j) {
while (arr[j] >= temp && i < j) {
j--;
}
while (arr[i] <= temp && i < j) {
i++;
}
if (i < j) {
// 交换i和j的值
swap(arr, i, j);
}
}
// 交换low 和 i 的值
swap(arr, low, i);
quickSort(arr, low, i-1);
quickSort(arr, i+1, high);
}
private static void swap(int[] arr, int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
public static void main(String[] args){
int[] arr = {10,7,2,4,7,62,3,4,2,1,8,9,19};
// int[] arr = {6 ,1 ,2, 7 ,9 ,3 ,4 ,5 ,10 ,8};
quickSort(arr, 0, arr.length-1);
System.out.println(Arrays.toString(arr));
}
}
Copyright: 采用 知识共享署名4.0 国际许可协议进行许可 Links:https://lixj.fun/archives/快速排序
边栏推荐
- PHP implementation of iframe cross site text replacement / iframe website text replacement
- Mathematics and quality education
- LeetCode 501 :二叉搜索树中的众数
- BGP - route map extension (explanation + configuration)
- Ilruntime hotfix framework installation and breakpoint debugging
- HyperLeger Fabric安装
- LabVIEW错误“内存已满 - 应用程序停止在节点”
- LabVIEW编程规范
- 嵌入式软件开发中的2种调试技术
- 云图说|每个成功的业务系统都离不开APIG的保驾护航
猜你喜欢

High speed data stream disk for LabVIEW

mysql 表机制

LabVIEW and VDM extract color and generate gray image

第六章——分枝限界法

IGBT与三代半导体SiC双脉冲测试方案

LabVIEW确定控件在显示器坐标系中的位置

Example analysis of SQL query optimization principle

LabVIEW获取Clamp函数找到的所有点的信息

Six procurement challenges perplexing Enterprises

LabVIEW obtains the information of all points found by the clamp function
随机推荐
Im instant messaging source code with tutorial /uniapp instant messaging source code, with installation tutorial
iframe框架自适应大小/全屏显示网页框架的方法
考研英语词汇 unit1
Binary tree pruning
LabVIEW在波形图或波形图表上显示时间和日期
BGP - route map extension (explanation + configuration)
LabVIEW phase locked loop (PLL)
300题 线代第一讲行列式
Data and information resource sharing platform (VII)
LabVIEW obtains the information of all points found by the clamp function
VS 番茄助手添加头注释 以及使用方式
MySQL table mechanism
Fiddler creates an autoresponder
【二叉树】二叉树剪枝
What Fiddler does for testing
细数十大信息安全原则
示波器和频谱分析仪的区别
一文带你了解J.U.C的FutureTask、Fork/Join框架和BlockingQueue
Six procurement challenges perplexing Enterprises
Build TAR model using beersales data set in TSA package