当前位置:网站首页>快速排序
快速排序
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/快速排序
边栏推荐
- 示波器和频谱分析仪的区别
- Leetcode 501: mode dans l'arbre de recherche binaire
- 宁愿“大小周”、每天只写 200 行代码、月薪 8k-17k 人群再涨
- Four ways to add names to threads in the thread pool
- [new version] new pseudo personal homepage v2.0- starze V Club
- 黑马头条丨腾讯薪酬制度改革引争议;英特尔全国扩招女工程师;黑马100%就业真的吗......
- Solve access denied for user 'root' @ 'localhost' (using password: yes)
- LabVIEW图片在从16位强制转换为8位后看起来要亮或暗
- LabVIEW错误“内存已满 - 应用程序停止在节点”
- easyrecovery15操作简单方便的数据恢复工具
猜你喜欢

Fiddler configuration

LabVIEW调用DLL时出现异常0xc0000005代码

LabVIEW获取IMAQ Get Last Event坐标

Kubernetes 基本介绍及核心组件

30 | 怎么重设消费者组位移?

LabVIEW编程规范

LabVIEW锁相环(PLL)

Exécuteur - shutdown, shutdown Now, awaittermination details and actual Fighting

An adaptive size / full screen display method for iframe frames

基于CenterOS7安装Redis及常见问题解决(带图讲解)
随机推荐
Is it safe to open an account online in Shanghai?
一文带你了解J.U.C的FutureTask、Fork/Join框架和BlockingQueue
LabVIEW打开其他EXE程序
The time (in minutes) required for a group of workers to cooperate to complete the assembly process of a part are as follows:
How to handle the database query error with Emoji expression in Typecho- Xingze V Club
Why is the kotlin language not popular now?
怎么生成自动参考文献(简单 有图)
Data and information resource sharing platform (V)
Error 1046 when LabVIEW uses MathScript node or matlab script
OpenVP*整合ldap認證
Is it safe for CICC Fortune Securities to open an account? Is it reliable?
给线程池里面线程添加名称的4种方式
Self made app connected to onenet --- realize data monitoring and distribution control (mqtt)
Openvp * consolider l'authentification LDAP
IGBT与三代半导体SiC双脉冲测试方案
【二叉树】二叉树剪枝
LabVIEW获取IMAQ Get Last Event坐标
easyrecovery15操作简单方便的数据恢复工具
Kubernetes 基本介绍及核心组件
LabVIEW用高速数据流盘