首页 > 其他分享 >Quick sort【1月19日学习笔记】

Quick sort【1月19日学习笔记】

时间:2024-01-19 11:55:27浏览次数:18  
标签:sort end 19 int 分区 start pIndex 索引 Quick

点击查看代码
//Quick sort
#include<iostream>
using namespace std;

int partition(int A[],int start,int end) {
	int pivot = A[end];//默认选取末尾为主元
	int pIndex = start;//分区索引初始化
	for (int i = start; i < end; i++) {//从索引start开始扫描
		if (A[i] < pivot) {
			int temp = A[i];
			A[i] = A[pIndex];
			A[pIndex] = temp;
			pIndex++;
		}//比主元小的元素全交换在分区索引左边,分区索引右移则A[pIndex]比A[end]大(或相等)
	}
	int temp = A[end];
	A[end] = A[pIndex];
	A[pIndex] = temp;//交换后分区索引右边全比主元大或相等
	return pIndex;
}

int randomisedpartition(int A[], int start, int end) {
	srand(time(0));
	int random = (rand() % (end - start + 1)) + start;
	int temp = A[end];
	A[end] = A[random];
	A[random] = temp;//start与end间随机抽取元素作为新end
	return partition(A, start, end);//返回分区索引
}


void quicksort(int A[], int start,int end) {
	if (start >= end) return;//中止条件
	int pIndex = randomisedpartition(A, start, end);//建立分区并返回分区索引
	quicksort(A, start, pIndex - 1);//分区索引左侧排序
	quicksort(A, pIndex+1,end);//分区索引右侧排序
}//not stable
 //average case time complexity:O(nlogn)  by using random pIndex
 //worst case time complexity:O(n^2)
 //average case space complexity:O(logn)  by using random pIndex
//worst case space complexity:O(n)

int main() {
	int A[] = { 2,7,4,1,5,3 };
	quicksort(A, 0,5);
	for (int i = 0; i < 6; i++)  cout << A[i] << " ";
	cout << endl;
}

标签:sort,end,19,int,分区,start,pIndex,索引,Quick
From: https://www.cnblogs.com/whvivy/p/17974335

相关文章

  • 2024-1-19事件绑定,input与hover事件
    目录事件绑定,input与hover事件事件绑定hover事件input事件事件绑定.on()方法注意:off()方法事件绑定,input与hover事件在jQ内很多中事件常用的事件有下面的click(function(){...})//绑定一个点击事件hover(function(){...})//悬停触发事件blur(function(){...})//失焦事件处理......
  • 2024.1.19日报
    本质:启动一个JVMProcess进程(一个进程里有多个线程),执行任务TaskLocal模式可以限制模拟Spark集群环境的线程数量,即Local[N]或Local[*]其中N代表可以使用N个线程,每个线程拥有一个cpucore,如果不指定N,则默认是1个线程(该线程有一个core)。通常Cpu有几个core,就指定几个线程,最大化利用......
  • 2024-1-19事件绑定,input与hover事件
    目录事件绑定,input与hover事件事件绑定hover事件input事件事件绑定,input与hover事件在jQ内很多中事件常用的事件有下面的click(function(){...})//绑定一个点击事件hover(function(){...})//悬停触发事件blur(function(){...})//失焦事件处理focus(function(){...})//焦点......
  • SpiderFlow爬虫平台漏洞利用分析(CVE-2024-0195)
    1.漏洞介绍SpiderFlow爬虫平台项目中spider-flow-web\src\main\java\org\spiderflow\controller\FunctionController.java文件的FunctionService.saveFunction函数调用了saveFunction函数,该调用了自定义函数validScript,该函数中用户能够控制 functionName、parameters 或 sc......
  • 1978:扩号匹配问题C
    #include<stdio.h>intmain(){chars[101];while(scanf("%s",s)!=EOF){printf("%s\n",s);chartem[101];inta[101]={0};inttop=0;inti=0;for(;s[i]!='\0';i++){......
  • 2024年1月【考试战报】|ORACLE OCP 19C考试通过
    2023年12月【考试战报】ORACLEOCP19C考试通过2023年11月【考试战报】|ORACLEOCP19C考试通过2024年10月【考试战报】|ORACLEOCP19C考试通过203年8月【考试战报】|ORACLEOCP19C考试通过数据库工程师-OracleOCP19C认证介绍ORACLE 2024年新年考试战报恭喜顺利拿证新......
  • Merge sort【1月18日学习笔记】
    点击查看代码//Mergesort#include<iostream>usingnamespacestd;voidmerge(intL[],intR[],intA[],intnL,intnR){//将两个已排序数组合并填入 inti=0,j=0,k=0;//i,j为未拾取元素索引,k为归并数组索引 while(i<nL&&j<nR){ if(L[i]<R[j]){......
  • C# .net中PropertyDescriptor的使用和BindingList的ApplySort排序
    找了好多资料都是java中PropertyDescriptor的使用,至于C#中的都抄袭别人的,又讲不清楚怎么用。官方文档也没不会手把手教你怎么用,经过一下午的研究,结果如下1、找到PropertyDescriptor同一dll下的,使用TypeDescriptor反射出属性的PropertyDescriptorCollection,从这里拿出对应属性的P......
  • 19条MySQL优化
    一善用EXPLAIN 做MySQL优化,我们要善用 EXPLAIN 查看SQL执行计划。下面来个简单的示例,标注(1,2,3,4,5)我们要重点关注的数据•type列: 连接类型。一个好的sql语句至少要达到range级别。杜绝出现all级别•key列: 使用到的索引名。如果没有选择索引,值是NULL。可以采取强制......
  • 19、nginx中location语法
    1.概述在实际应用中,权限控制的需求更加复杂。例如,对于网站下的img目录允许所有用户访问,但对于网站下的admin目录则仅允许管理员身份的用户访问。此时,仅靠deny和allow这两个权限指令不能满足用户的需求,还需要使用location块来完成相关需求的匹配。2.location语法location......