[{"createTime":1735734952000,"id":1,"img":"hwy_ms_500_252.jpeg","link":"https://activity.huaweicloud.com/cps.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=V1g3MDY4NTY=&utm_medium=cps&utm_campaign=201905","name":"华为云秒杀","status":9,"txt":"华为云38元秒杀","type":1,"updateTime":1735747411000,"userId":3},{"createTime":1736173885000,"id":2,"img":"txy_480_300.png","link":"https://cloud.tencent.com/act/cps/redirect?redirect=1077&cps_key=edb15096bfff75effaaa8c8bb66138bd&from=console","name":"腾讯云秒杀","status":9,"txt":"腾讯云限量秒杀","type":1,"updateTime":1736173885000,"userId":3},{"createTime":1736177492000,"id":3,"img":"aly_251_140.png","link":"https://www.aliyun.com/minisite/goods?userCode=pwp8kmv3","memo":"","name":"阿里云","status":9,"txt":"阿里云2折起","type":1,"updateTime":1736177492000,"userId":3},{"createTime":1735660800000,"id":4,"img":"vultr_560_300.png","link":"https://www.vultr.com/?ref=9603742-8H","name":"Vultr","status":9,"txt":"Vultr送$100","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":5,"img":"jdy_663_320.jpg","link":"https://3.cn/2ay1-e5t","name":"京东云","status":9,"txt":"京东云特惠专区","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":6,"img":"new_ads.png","link":"https://www.iodraw.com/ads","name":"发布广告","status":9,"txt":"发布广告","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":7,"img":"yun_910_50.png","link":"https://activity.huaweicloud.com/discount_area_v5/index.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=aXhpYW95YW5nOA===&utm_medium=cps&utm_campaign=201905","name":"底部","status":9,"txt":"高性能云服务器2折起","type":2,"updateTime":1735660800000,"userId":3}]
快速排序法:
* 顾名思议,快速排序法是实践中的一种快速的排序算法,在c++或对java基本类型的排序中特别有用。它的平均运行时间是0(N log
N)。该算法之所以特别快,主要是由于非常精练和高度优化的内部循环。
*
快速排序是对冒泡法的一种改进。通过一趟排序将要排序的的数据分割成独立的两部分,其中一部分的所有数据都比另一部分所有的数据要小,然后再按此方法对这两部分数据分别进行快速排序,整个排序过程可以递归进行,以此达到整个数据变成有序序列。
示意图:
这里 定义最左边元素 为left 最右边元素为right
p 元素的值 就是 2对应的索引 0 加上 5对应的索引 7 之和 除以2 得到 索引为3 对应的元素7
用左边大于 7的数跟右边小于7的数进行 交换位置 一直进行 并且 中间的p也要一直变化位置
直到 排完
代码实现:
import java.util.Arrays; public class kuaisu { public static void
main(String[] args) { int arrays[]=new int[]{2,9,4,7,3,3,6,5};
sort(arrays,0,arrays.length-1); System.out.println(Arrays.toString(arrays)); }
public static void sort(int arrays[],int left,int right){ int l=left ;//给定下标
int r=right;//给定下标 int temp; //定义一个变量 作为中间值 交换 左右两边的元素位置 int
pivot=arrays[(left+right)/2];//中间值 while(l<r){ //在左边查找小于中间值得元素
while(arrays[l]<arrays[pivot]){ l++; } //同理在右边查找大于中间值得元素
while(arrays[r]>arrays[pivot]){ r--; }//直到左边元素大于右边元素就结束 if(l>=r){ break; }
temp=arrays[l]; arrays[l]=arrays[r]; arrays[r]=temp; //交换完arrays[l]=pivot
if(arrays[l]==pivot){ r--; } if(arrays[r]==pivot){ l++; } if(r==l){ //要让左边元素
往左边移 右边元素往右边移 错开 l++; r--; } //对左边进行递归 if(left<r){ sort(arrays,left,r);
}//对右边进行递归 if(right>l){ sort(arrays,l,right); } } } }
<>
控制台输出结果 如下:
写的不足的地方还希望大佬们 帮我支出 我会更加努力地!
一名正在发光的小白!
什么都不如友友们的一键三连哈哈哈哈 !