当前位置:网站首页>快速排序
快速排序
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/快速排序
边栏推荐
- vtk.js中vtp下载
- LeetCode+ 16 - 20
- 一 组工人合作完成某一部件的装配工序所需的时间(单位:分钟)分别如下:
- Is it safe to open an account in Shanghai Securities?
- Kubernetes 基本介绍及核心组件
- Unity script cannot display Chinese comments of C # source code or the script created by vs does not have comments of C # source code
- 改变世界的开发者丨玩转“俄罗斯方块”的瑶光少年
- LabVIEW和VDM提取色彩和生成灰度图像
- Fiddler filtering sessions
- Data and information resource sharing platform (VIII)
猜你喜欢

LabVIEW错误“内存已满 - 应用程序停止在节点”

Analysis of Genesis public chain

Vs tomato assistant add header comments and usage

High speed data stream disk for LabVIEW

vtk.js中vtp下载

ILRuntime热更框架 安装以及断点调试

PHP implementation of iframe cross site text replacement / iframe website text replacement

Ilruntime hotfix framework installation and breakpoint debugging

Solutions to the error reported by executing Oracle SQL statement [ora-00904: "createtime": invalid identifier] and [ora-00913: too many values]

干货丨MapReduce的工作流程是怎样的?
随机推荐
LabVIEW改变Point ROI Overlay的形状或颜色
LabVIEW获取IMAQ Get Last Event坐标
已知某种木材的横纹抗压力服从N(x,d2),现对十个试件作横纹抗压力试验,得数据如下:(单位:kg/cm2)
R 语言绘制二维正态分布的密度曲面图;
Postgraduate entrance examination English vocabulary unit1
Data and information resource sharing platform (V)
Baidu quick collection SEO optimization keyword ranking optimization skills
LabVIEW错误“内存已满 - 应用程序停止在节点”
LabVIEW打开其他EXE程序
HyperLeger Fabric安装
[paper sharing] pata: fuzzing with path aware Taint Analysis
LabVIEW确定控件在显示器坐标系中的位置
Exception 0xc00000005 code occurred when LabVIEW called DLL
MySQL learning child query
LabVIEW get IMAQ get last event coordinates
C语言创建动态二维数组
LabVIEW中创建毫秒时间标识
Analysis of Genesis public chain
给线程池里面线程添加名称的4种方式
LabVIEW phase locked loop (PLL)