当前位置: 首页 > news >正文

排序算法-快速排序法(QuickSort)

 排序算法-快速排序法(QuickSort)

1、说明

快速排序法是由C.A.R.Hoare提出来的。快速排序法又称分割交换排序法,是目前公认的最佳排序法,也是使用分而治之(Divide and Conquer)的方式,会先在数据中找到一个虚拟的中间值,并按此中间值将所有打算排序的数据分为两部分。其中小于中间值的数据放在左边,而大于中间值的数据放在右边,再以同样的方式分别处理左右两边的数据,直到排序完为止。操作与分割步骤如下:

假设有n项记录R_{1},R_{2},R_{3},...,R_{n},其键值为K_{1},K_{2},K_{3},...,K_{n}

  1. 先假设K的值为第一个键值。
  2. 从左向右找出键值K_{i},使得K_{i}> K
  3. 从左向右找出键值K_{j},使得K_{j}< K
  4. 如果i< j,那么K_{i}K_{j}互换,并回到步骤2。
  5. 如果i\geqslant j,那么将KK_{j}互相,并以j为基准点分割成左、右两部分,然后针对左、右两边执行步骤1~5,直到左边键值等于右边键值为止。

2、算法分析

  1. 在最好情况和平均情况下,时间复杂度为O(nlog_{2^{}}n)。在最坏情况下就是每次挑中的中间值不是最大就是最小的,其时间复杂度为O(n^{2})
  2. 快速排序法不是稳定排序法。
  3. 在最坏情况下,空间复杂度为O(n),而在最好情况下,空间复杂度为O(log_{2^{}}n)
  4. 快速排序法是平均运行时间最快的排序法。

3、C++代码 

#include<iostream>
using namespace std;void Print(int tempData[], int tempSize) {for (int i = 0; i < tempSize; i++) {cout << tempData[i] << "  ";}cout << endl;
}void Quick(int tempData[], int tempLeft, int tempRight) {int temp;int leftIndex;int rightIndex;int t;if (tempLeft < tempRight) {leftIndex = tempLeft + 1;rightIndex = tempRight;while (true) {for (int i = tempLeft + 1; i < tempRight; i++) {if (tempData[i] >= tempData[tempLeft]) {leftIndex = i;break;}leftIndex++;}for (int j = tempRight; j > tempLeft + 1; j--) {if (tempData[j] <= tempData[tempLeft]) {rightIndex = j;break;}rightIndex--;}if (leftIndex < rightIndex) {temp = tempData[leftIndex];tempData[leftIndex] = tempData[rightIndex];tempData[rightIndex] = temp;}else {break;}}if (leftIndex >= rightIndex) {temp = tempData[tempLeft];tempData[tempLeft] = tempData[rightIndex];tempData[rightIndex] = temp;Quick(tempData, tempLeft, rightIndex - 1);Quick(tempData, rightIndex + 1, tempRight);}}
}int main() {const int size = 10;int data[100] = { 32,5,24,55,40,81,17,48,25,71 };//32  5  24  55  40  81  17  48  25  71//32  5  24  25  40  81  17  48  55  71//32  5  24  25  17  81  40  48  55  71//17  5  24  25  32  81  40  48  55  71//5  17  24  25  32  81  40  48  55  71//5  17  25  24  32  81  40  48  55  71//5  17  25  24  32  71  40  48  55  81//5  17  25  24  32  55  40  48  71  81//5  17  25  24  32  48  40  55  71  81//5  17  25  24  32  40  48  55  71  81Print(data, size);Quick(data, 0, size - 1);Print(data, size);return 0;
}

输出结果 

http://www.lryc.cn/news/192267.html

相关文章:

  • Python 简介
  • grafana api创建dashboard 记录
  • 局域网上IP多播与IP单播关于MAC地址的区别
  • 三数之和[中等]
  • 基于天牛须优化的BP神经网络(分类应用) - 附代码
  • 渗透波菜网站
  • Spring Boot:Dao层-实例介绍
  • 接口测试入门:深入理解接口测试!
  • Redis微服务架构
  • 【C++】 局部对象,引用返回
  • 线性代数中涉及到的matlab命令-第二章:矩阵及其运算
  • 计算机毕业设计选什么题目好?springboot 美食推荐系统
  • 爆肝整理,Jmeter接口性能测试-跨线程调用变量实操(超详细)
  • Maven导入程序包jakarta.servlet,但显示不存在
  • es6(二)——常用es6说明
  • 经典垃圾回收器
  • 台达DOP-B07S410触摸屏出现HMI no response无法上传的解决办法
  • [资源推荐] 复旦大学张奇老师科研分享
  • C++数位动态规划算法:统计整数数目
  • ip 网段设置 --chatGPT
  • 使用JMeter进行接口测试教程
  • 文本生成解码策略
  • 华为数通方向HCIP-DataCom H12-831题库(单选题:221-240)
  • AttributeError: module ‘hanlp.utils.rules‘ has no attribute ‘tokenize_english‘
  • 苍穹外卖(四) AOP切面公共字段自动填充及文件上传
  • vue-cli + vue3 项目 ios 苹果手机白屏问题
  • Spring Boot中的JdbcTemplate是什么,如何使用
  • Python测网络连通性、能否访问某个网络或者端口号<网络检测、ping主机、测试端口>
  • 【沧元图】玉阳宫主是正是邪,和面具人有勾结吗?现在已有答案了
  • C++笔记之popen()和std_system()和std_async()执行系统命令比较