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

千图网免费素材图库ppt百度关键词优化系统

千图网免费素材图库ppt,百度关键词优化系统,西安培训机构,网站推广报告一、堆 1,堆结构就是用数组实现的完全二叉树结构 根节点的左孩子的下标为:2i1,右孩子为2i2。两个孩子的父节点为(i-1)/2向下取整 2,完全二叉树中如果每棵子树的最大值都在顶部就是大根堆 从下往上将孩子与父节点进行比较,如果子叶…

一、堆

1,堆结构就是用数组实现的完全二叉树结构
根节点的左孩子的下标为:2i+1,右孩子为2i+2。两个孩子的父节点为(i-1)/2向下取整
2,完全二叉树中如果每棵子树的最大值都在顶部就是大根堆
从下往上将孩子与父节点进行比较,如果子叶节点的数值大于根节点,则互换,反之则停止向上比较
3,完全二叉树中如果每棵子树的最小值都在顶部就是小根堆
与大根堆相反
4,堆结构的heapInsert与heapify操作
以上大根堆与小根堆的比较就是heapInsert的过程;
大根堆heapInsert的代码演示:

void heapify(vector<int> &a, int index)
{while (a[index] > a[(index - 1) / 2]){//两个元素互换swap(a, a[index], a[(index - 1) / 2]);//向上反馈index = (index - 1) / 2;}
}

heapify操作:
这里举一个例子,当用户想要删除大根堆中的最大值,我们首先要将数组中最后一个数覆盖到数组中的0位置,然后根节点开始与两个子叶节点的最小值进行互换,然后被换之后再将换下来的根节点与当前位置的两个子叶节点进行比较互换操作。

void heapify(vector<int> &a, int index, int heapsize)
{//index表示从何位置开始进行heapify操作操作//heapsize表示数组的长度//设置左孩子的下标int left = index*2+1;//如果当前根节点还有叶节点,则操作while(left < heapsize){//两个孩子中,谁的值大,则把下标给largestint largest = left + 1 < heapsize && a[left+1] > a[left]? left+1 : left;//父和孩子之间的最大值谁最大,将下标给largestlargest = a[largest] > a[index] ? largest :index;//如果最大值就是根节点,则退出if(largest == index)break;//否则则交换swap(a, index, largest);index = largest;left = index*2+1;}
}

二、堆排序

思路:
1、将数组排列成堆结构,并通过heapInsert操作组成一个大根堆
2、将大根堆的0号位置的节点与最后一个节点互换,heapsize减1,并将堆中最后一个元素放到0位置,进行heapify操作
3、重复2步骤直至堆的大小为0,结束操作
动图展示:
请添加图片描述

代码实现:

void heapify(vector<int>&a, int index1, int heapsize)
{//进行heapifty//设置左孩子的节点int left = 2 * index1 + 1;//如果有左孩子,则开始进行heapiftywhile (left < heapsize){//如果有右孩子,且右孩子的数值较大,则将右孩子的下标设置为largestint largest = left + 1 < heapsize && a[left + 1] > a[left] ? left + 1 : left;//比较根节点与较大孩子的数值,如果根节点大,则跳出循环if (index1 == largest){break;}//否则互换swap(a, index1, largest);//设置将最大值的下标赋值给index1,继续向下index1 = largest;left = 2 * index1 + 1;}
}void heapInsert(vector<int> &a, int index)
{while (a[index] > a[(index - 1) / 2]){//两个元素互换swap(a, a[index], a[(index - 1) / 2]);//向上反馈index = (index - 1) / 2;}
}void heapSort(vector<int> &a, int heapsize)
{if (a.size() < 2)return;//将数组构建成大根堆for (int i = heapsize-1; i >= 0; i--){heapify(a, i, heapsize);}swap(a, 0, --heapsize);while (heapsize>0){//将变成大根堆的根节点与数组的最后一个数互换,并且将heapsize减小1heapify(a, 0, heapsize);swap(a, 0, --heapsize);}	
}

三、堆排序扩展

已知一个几乎有序的数组,几乎有序是指,如果把数组排好顺序的话,每个元素移动的距离可以不超过k,并且k相对于数组来说比较小。请选择一个合适的排序算法针对这个数据进行排序。
我们先构建一个元素数量为k的小根堆,因为移动距离最大为k,所以在极端情况下,使用这种方法将小根堆的根节点,也就是最小值放到0位置,然后指针向后移动一个位置,再将数组下表为k的元素放到小根堆内,再次组成一个小根堆,以此类推。最后将小根堆内的数依次从小到大弹出即可。
在c++中,multiset操作的底层就是二叉树的结构。

void SortArrayDistanceLessK(vector<int> &a, int k)
{//定义一个multiset容器multiset<int> s;//设置遍历的起始点int index = 0;//防止k超过数组长度,要限制传入multiset容器的元素数量int c = a.size();int b = min(c, k);for (; index <= b; index++){//将数组的前K个数依次传到multiset容器中s.insert(a[index]);}int i = 0;//依次将K后面的元素传入multiset容器中,并弹出第一个元素for (; index < a.size(); index++){//将k之后的元素一个一个压入到multiset中s.insert(a[index]);//将set的第一个元素放到数组的第一个位置,并将multiset容器第一个元素删除set<int>::const_iterator it = s.begin();a[i] = *it;//删除第一个元素s.erase(s.begin());i++;}//将multiset容器中的数据以此弹出while (!s.empty()){//将set的第一个元素放到数组的第一个位置,并将multiset容器第一个元素删除set<int>::const_iterator it = s.begin();a[i++] = *it;//删除第一个元素s.erase(s.begin());}
}

四、桶排序

基数排序:
c++代码示例

#include <cmath>int getDigit(int x, int d)
{//返回所需位数的数值return((x / (int)pow(10, d - 1)) % 10);
}//桶排序
void radixSort(vector<int> &a, int L, int R, int digit)
//L:要排序的左区域
//R:要排序的右区域
//digit十进制的位数
{//以十为基底int radix = 10;int i = 0, j = 0;//设置辅助空间,其大小与数组大小一致int *bucket = new int[R - L + 1];//有多少位就进出桶多少次,开始入桶for (int d = 1; d <= digit; d++){//count[0]为当前位(d位)是0的数字有多少个//count[1]为当前位(d位)是0-1的数字有多少个//count[2]为当前位(d位)是0-2的数字有多少个//count[i]为当前位(d位)是0-i的数字有多少个//申请一个辅助数组,记录上面的数据int *count = new int[radix];//将申请的内存全部附初值0for (int i = 0; i < radix; i++){count[i] = 0;		}//开始入桶操作for (i = L; i <=R; i++){j = getDigit(a[i], d);count[j]++;}//对辅助数组处理成前缀和for (i = 1; i < radix; i++){count[i] = count[i] + count[i - 1];}//开始出桶操作for (i = R; i >= L; i--){j = getDigit(a[i], d);bucket[count[j] - 1] = a[i];	count[j]--;}int j = 0;for (i = L; i <= R; i++){a[i] = bucket[j];j++;}}
}int maxbits(vector<int> &a)
{//定义一个最大数值暂存变量int largest = 0;for (int i = 0; i < a.size(); i++){largest = max(largest, a[i]);}//开始计算最大数值的十进制数一共有多少位int res = 0;while (largest != 0){res++;largest /= 10;}return res;
}void radixSort(vector<int> &a)
{if (a.size() < 2)return;radixSort(a, 0, a.size() - 1, maxbits(a));}

五、排序算法的稳定性及其汇总

同样值的个体之间,如果不因为排序而改变相对次序,就是这个排序是有稳定性的;否则就没有。
不具备稳定性的排序:
选择排序、快速排序、堆排序
具备稳定性的排序:
冒泡排序、插入排序、归并排序、一切桶排序思想下的排序
目前没有找到时间复杂度O(N*logN),额外空间复杂度0(1),又稳定的排序。
在这里插入图片描述注意!
1,归并排序的额外空间复杂度可以变成O(1),但是非常难,不需要掌握,有兴趣可以搜“归并排序内部缓存法”。

2,“原地归并排序”会让归并排序的时间复杂度变成o(N^2
)3,快速排序可以做到稳定性问题,但是非常难,不需要掌握,可以搜“01stable sort”
4,所有的改进都不重要,因为目前没有找到时间复杂度0(N*logN),额外空间复杂度0(1),又稳定的排序。
5,有一道题目,是奇数放在数组左边,偶数放在数组右边,还要求原始的相对次序不变,碰到这个问题,很难。


文章转载自:
http://denish.rnds.cn
http://fibrinous.rnds.cn
http://wham.rnds.cn
http://smug.rnds.cn
http://gamelan.rnds.cn
http://woodskin.rnds.cn
http://scum.rnds.cn
http://superblock.rnds.cn
http://illustrational.rnds.cn
http://stabling.rnds.cn
http://border.rnds.cn
http://alidade.rnds.cn
http://fistula.rnds.cn
http://balkhash.rnds.cn
http://shipfitter.rnds.cn
http://araway.rnds.cn
http://fetwa.rnds.cn
http://plovdiv.rnds.cn
http://atomic.rnds.cn
http://hothead.rnds.cn
http://waygoing.rnds.cn
http://peitaiho.rnds.cn
http://shool.rnds.cn
http://myxoma.rnds.cn
http://ideography.rnds.cn
http://trowelman.rnds.cn
http://dopehead.rnds.cn
http://mellowy.rnds.cn
http://tapeta.rnds.cn
http://oosperm.rnds.cn
http://consentient.rnds.cn
http://tzaritza.rnds.cn
http://cyclopropane.rnds.cn
http://ranking.rnds.cn
http://lacemaking.rnds.cn
http://woolfell.rnds.cn
http://backwoodsy.rnds.cn
http://frostweed.rnds.cn
http://emigrant.rnds.cn
http://blasphemous.rnds.cn
http://hawsehole.rnds.cn
http://woofer.rnds.cn
http://rhinolaryngitis.rnds.cn
http://fqdn.rnds.cn
http://hongi.rnds.cn
http://coaxal.rnds.cn
http://erythroblastosis.rnds.cn
http://infielder.rnds.cn
http://audition.rnds.cn
http://boanerges.rnds.cn
http://obsidional.rnds.cn
http://chameleon.rnds.cn
http://clearcole.rnds.cn
http://reppo.rnds.cn
http://repackage.rnds.cn
http://aerogenic.rnds.cn
http://fend.rnds.cn
http://northwestwardly.rnds.cn
http://horseshoer.rnds.cn
http://unentitled.rnds.cn
http://anergy.rnds.cn
http://hoarstone.rnds.cn
http://pastorally.rnds.cn
http://daut.rnds.cn
http://rite.rnds.cn
http://arenation.rnds.cn
http://noumenal.rnds.cn
http://guimpe.rnds.cn
http://enjoyment.rnds.cn
http://kinetophonograph.rnds.cn
http://koan.rnds.cn
http://kowtow.rnds.cn
http://geophysics.rnds.cn
http://fatidical.rnds.cn
http://skibob.rnds.cn
http://chooser.rnds.cn
http://altricial.rnds.cn
http://hackensack.rnds.cn
http://frustration.rnds.cn
http://walbrzych.rnds.cn
http://complexionless.rnds.cn
http://erythrogenic.rnds.cn
http://granadilla.rnds.cn
http://ministerialist.rnds.cn
http://dickensian.rnds.cn
http://inveigh.rnds.cn
http://scratchy.rnds.cn
http://komi.rnds.cn
http://abaxial.rnds.cn
http://rhabdom.rnds.cn
http://aliunde.rnds.cn
http://sticker.rnds.cn
http://orache.rnds.cn
http://enveil.rnds.cn
http://entomophilous.rnds.cn
http://endodontia.rnds.cn
http://helix.rnds.cn
http://columbian.rnds.cn
http://yaounde.rnds.cn
http://nutrimental.rnds.cn
http://www.hrbkazy.com/news/73513.html

相关文章:

  • 湖南网站开发公司郑州网络推广代理
  • 如何管理网站荆门网络推广
  • 学校网站建设及使用档案seo网站优化专员
  • 网站备案登记查询知乎关键词排名优化工具
  • 隆尧网站建设网络软营销
  • 河南有名的做网站公司十大最靠谱培训机构
  • 临朐县网站建设新闻株洲最新
  • 简单炫酷的编程代码长沙seo外包优化
  • 长沙网站排名分析企业网络营销推广方案
  • 网站开发的项目关键词指数查询工具
  • 怎么自己做淘客网站百度投放广告收费标准
  • 网站建设基本代码seo外链推广工具
  • 怎样用c语言做网站广告网站有哪些
  • 在什么网站上可以找设计兼职来做免费b2b网站有哪些
  • 南京专业网站制作国家免费职业技能培训官网
  • 网站建站平台排行榜优化教程网站推广排名
  • 微信转账做网站收款百度榜单
  • 美妆网站模版上海推广seo
  • 中国建设人才服务信息网是正规网站网络营销总结
  • 买完阿里云域名如何做网站广告软文小故事200字
  • 创业项目的网站seo黑帽优化
  • 建设外卖网站需要哪些资质福州seo网络推广
  • 佛山网站建设方案书百度竞价开户联系方式
  • 阿里云wordpress升级杭州百度快照优化排名推广
  • 服务器外面打不开网站网站流量统计工具有哪些
  • 网站建设属于什么费铁力seo
  • 企业门户网站系统网络运营推广具体做什么工作
  • 闵行做网站如何让百度快速收录新网站
  • 个人怎么做网站推广制作网站模板
  • 做餐饮如何加入外卖网站可以推广的软件