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

Algorithm - Quick Sort(Java)

分享一个大牛的人工智能教程。零基础!通俗易懂!风趣幽默!希望你也加入到人工智能的队伍中来!请轻击人工智能教程大家好!欢迎来到我的网站! 人工智能被认为是一种拯救世界、终结世界的技术。毋庸置疑,人工智能时代就要来临了,科… 继续阅读 前言https://www.captainai.net/troubleshooter

package live.every.day.Algorithm.Sort; import java.util.Arrays; /** * @author LiveEveryDay * * Basic Thought: * * Quick Sort: * Split the sequence into two independent parts by One Trip Quick Sort, * of which one part data is smaller than the other. * And then perform Quick Sort separately on the two parts, * the whole process can be recursive, * hereby the whole sequence becomes sorted sequence. * * One Trip Quick Sort: * Assume the array is a[0]...a[n-1], * adjust the array pivot to the middle value of three: a[low], a[(low+high)/2], a[high]; * and swap the pivot to the first element. * then put all the elements smaller than the pivot before the pivot, * put all the elements bigger than the pivot after the pivot, * this process is called One Trip Quick Sort. * * Algorithm: * * Quick Sort algorithm is designed as following: * 1. Set two variable low and high, if low >= high, terminate recursive process. * 2. Perform One Trip Quick Sort and get pivot. * 3. Quick Sort the left part of pivot recursively. * 4. Quick Sort the right part of pivot recursively. * * One Trip Quick Sort algorithm is designed as following: * 1. Set two variable i and j, i = 0, j = n - 1. * 2. Set the first element as pivot, i.e. pivot = a[i]. * 3. Search forward from j (j--), * find the first element that a[j] < pivot, * assign a[j] to a[i]. * 4. Search backward from i (i++), * find the first element that a[i] > pivot, * assign a[i] to a[j]. * 5. Repeat step3 and step4 until i == j. * Then, a[i] = pivot, return i. * * Complexity: * The average time complexity of Quick Sort is O(nlog2n). * * Stability: * Note that Quick Sort is not stable, * i.e. the relative positions of equal elements may be changed after Quick Sort. */ public class QuickSort { /** * Quick Sort * * @param a The array to be sorted * @param low The start position of sorting * @param high The end position of sorting */ public static void quickSort(int[] a, int low, int high) { if (a == null || a.length == 0) { return; } if (low >= high) { return; } int pivot = oneTripQuickSort(a, low, high); // Print each trip of sorting System.out.printf("Array: %s, pivot position: %2d, pivot value: %2d%n", Arrays.toString(a), pivot, a[pivot]); // Sort the left part of pivot quickSort(a, low, pivot - 1); // Sort the right part of pivot quickSort(a, pivot + 1, high); } /** * One Trip Quick Sort * * @param a The array to be sorted * @param low The start position of sorting * @param high The end position of sorting * @return The pivot position */ private static int oneTripQuickSort(int[] a, int low, int high) { int i = low; int j = high; // Adjust pivot in array adjustPivot(a, i, j); System.out.printf("Adjust pivot of array: %s%n", Arrays.toString(a)); // Set the first element as pivot int pivot = a[i]; while (i < j) { // From right to left, find the first element that less than pivot. while (i < j && a[j] >= pivot) { j--; } // Replace a[i] = a[j]; // From left to right, find the first element that greater than pivot. while (i < j && a[i] <= pivot) { i++; } // Replace a[j] = a[i]; } // Hereto, i must be equal to j, i (or j) is the pivot position. a[i] = pivot; // Return the pivot position return i; } /** * Adjust the array pivot to the middle value of three: a[low], a[(low+high)/2], a[high]; * and swap the pivot to the first element. * * @param a The array * @param low The low index * @param high The high index */ private static void adjustPivot(int[] a, int low, int high) { int mid = (low + high) >> 1; if ((a[mid] > a[low] && a[mid] < a[high]) || (a[mid] > a[high] && a[mid] < a[low])) { swap(a, low, mid); } if ((a[high] > a[mid] && a[high] < a[low]) || (a[high] > a[low] && a[high] < a[mid])) { swap(a, low, high); } } /** * Swap two elements in array * * @param a The array * @param i The element index i * @param j The element index j */ private static void swap(int[] a, int i, int j) { int t = a[i]; a[i] = a[j]; a[j] = t; } /** * Test program * * @param args The arguments */ public static void main(String[] args) { int[] a = {-11, 68, 22, -3, 99, 0, 16, 6, -1, 99, 101}; System.out.println("Before quick sort:"); System.out.println(Arrays.toString(a)); System.out.println("In quick sort:"); QuickSort.quickSort(a, 0, a.length - 1); System.out.println("After quick sort:"); System.out.println(Arrays.toString(a)); } } /* ------ Output ------ Before quick sort: [-11, 68, 22, -3, 99, 0, 16, 6, -1, 99, 101] In quick sort: Adjust pivot of array: [0, 68, 22, -3, 99, -11, 16, 6, -1, 99, 101] Array: [-1, -11, -3, 0, 99, 22, 16, 6, 68, 99, 101], pivot position: 3, pivot value: 0 Adjust pivot of array: [-3, -11, -1, 0, 99, 22, 16, 6, 68, 99, 101] Array: [-11, -3, -1, 0, 99, 22, 16, 6, 68, 99, 101], pivot position: 1, pivot value: -3 Adjust pivot of array: [-11, -3, -1, 0, 99, 22, 16, 6, 68, 99, 101] Array: [-11, -3, -1, 0, 68, 22, 16, 6, 99, 99, 101], pivot position: 8, pivot value: 99 Adjust pivot of array: [-11, -3, -1, 0, 22, 68, 16, 6, 99, 99, 101] Array: [-11, -3, -1, 0, 6, 16, 22, 68, 99, 99, 101], pivot position: 6, pivot value: 22 Adjust pivot of array: [-11, -3, -1, 0, 6, 16, 22, 68, 99, 99, 101] Array: [-11, -3, -1, 0, 6, 16, 22, 68, 99, 99, 101], pivot position: 4, pivot value: 6 Adjust pivot of array: [-11, -3, -1, 0, 6, 16, 22, 68, 99, 99, 101] Array: [-11, -3, -1, 0, 6, 16, 22, 68, 99, 99, 101], pivot position: 9, pivot value: 99 After quick sort: [-11, -3, -1, 0, 6, 16, 22, 68, 99, 99, 101] */
http://www.cnnetsun.cn/news/1314168.html

相关文章:

  • Spring Cloud Circuit Breaker 2.0.0 M1(Milestone 1)是 Spring Cloud 官方在 2022 年初发布的
  • 计算机毕业设计Hadoop农产品价格预测 农产品销量分析 农产品价格分析 农产品可视化 农产品数据分析 农产品爬虫 农产品大数据 大数据毕设
  • Java程序员转行AI大模型:避坑指南+落地路径
  • 2026年C盘空间不足怎么清理临时文件?一键清理工具安全吗?
  • 【实时Linux工业PLC解决方案系列】第三十三篇 - 实时Linux PLC安全PLC功能实现
  • 好写作AI:本科生实验记录变论文初稿的4个神操作,理工科狂喜!
  • Rockstar WebAssembly部署终极指南:5步将摇滚程序带到浏览器
  • 终极无线安全测试指南:Social-Engineer Toolkit的airbase-ng和airmon-ng集成方案
  • Apache Weex移动端文件操作终极指南:快速实现读写本地文件
  • 为什么选择React Native Firebase Starter?10个核心优势深度剖析
  • 终极指南:Docusaurus状态管理的React Context和全局状态最佳实践
  • Rust无标准库开发:num库的no_std模式应用详解
  • 玩转Takahē自定义表情:创建、上传与跨服务器分享全攻略
  • 如何用DVA集成WebAssembly提升前端计算性能:终极优化指南
  • iOS应用色彩可访问性终极指南:使用Chameleon框架的5个关键技巧
  • 终极Magpie窗口缩放软件集成指南:10个实用技巧让应用协同工作更流畅
  • 从0到1构建离线Web应用:基于gh_mirrors/ap/application-shell的开发指南
  • awspec高级特性:自定义匹配器开发与复杂资源测试策略
  • 如何用awspec实现AWS基础设施即代码(IaC)的自动化测试
  • OpenSpeedy配置转换终极指南:从旧格式到新格式的完整迁移教程
  • OpenObserve告警通知终极指南:5种常用场景模板集合与配置方法
  • Shoelace Vue开发终极指南:构建企业级应用的10个最佳实践
  • 10个Latent Diffusion实用技巧:提升AI绘画质量的关键参数设置
  • Pillow图像处理终极指南:30+格式支持与高效转换技巧
  • SiameseUIE中文-base生产部署:Nginx反向代理+SSL证书+访问限流配置
  • 百川2-13B-4bits开源模型效果实测:长文本生成(2048 tokens)下的上下文连贯性验证
  • 卡证检测矫正模型高算力适配:T4/V100/A10实测显存与延迟数据
  • RMBG-2.0开源模型社区共建:已支持中文文档、Bilibili教学视频、微信答疑群
  • periph库常见问题解答:解决外设编程中的疑难杂症
  • MVVM Light IoC容器:SimpleIoc依赖注入完全攻略