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

网站开发分哪几个步骤学习html的网站

网站开发分哪几个步骤,学习html的网站,简述新建站点的步骤,怎么做网站卖保险堆排序即利用堆的思想来进行排序,总共分为两个步骤: 1. 建堆 升序:建大堆 降序:建小堆 原因分析: 若升序建小堆时间复杂度是O(N^2) 升序建大堆,时间复杂度O(N*logN) 所以升序建大堆…

堆排序即利用堆的思想来进行排序,总共分为两个步骤:


1. 建堆
升序:建大堆
降序:建小堆

原因分析:

若升序建小堆时间复杂度是O(N^2)

升序建大堆,时间复杂度O(N*logN)

所以升序建大堆,降序建小堆


2. 利用堆删除思想来进行排序


建堆和堆删除中都用到了向下调整,因此掌握了向下调整,就可以完成堆排序。

代码实现

#include<stdio.h>//交换函数
void Swap(int* px, int* py)
{int tmp = *px;*px = *py;*py = tmp;
}
//向下调整算法
void AdjustDown(int* a, int n, int parent)
{int child = parent * 2 + 1;while (child < n){if (child + 1 < n && a[child + 1] > a[child]){child++;}if (a[child] > a[parent]){Swap(&a[child], &a[parent]);parent = child;child = child * 2 + 1;}else{break;}}
}
void HeapSort(int* a, int n)
{//建堆for (int i = (n - 1 - 1) / 2; i >= 0; i--){AdjustDown(a, n, i);}int end = n - 1;while (end > 0){Swap(&a[0], &a[end]);AdjustDown(a, end, 0);end--;}
}
int main()
{int a[] = { 0,4,3,6,7,8,1 };HeapSort(a, sizeof(a) / sizeof(int));return 0;
}

运行结果

若是建小堆则只需要把向下调整中的>改成<,修改后如下

if (child + 1 < n && a[child + 1] < a[child])
{child++;
}
if (a[child] < a[parent])
{Swap(&a[child], &a[parent]);parent = child;child = child * 2 + 1;
}

运行结果

当然我们也可以先写一个堆的数据结构再进行堆排序,但是这显然不如上面的快速且节省空间

自主实现数据结构堆再进行堆排序

代码实现

#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<assert.h>
#include<string.h>typedef int HPDataType;
typedef struct Heap
{HPDataType* a;int size;int capacity;
}HP;
//初始化
void HPArrayInit(HP* hp, HPDataType* a, int n);
//销毁
void HPDestroy(HP* hp);
// 堆的插入
void HPPush(HP* hp, HPDataType x);
// 堆的删除
void HPPop(HP* hp);
// 取堆顶的数据
HPDataType HPTop(HP* hp);
// 堆的数据个数
int HPSize(HP* hp);
// 堆的判空
int HPEmpty(HP* hp);
//向上调整算法
void Adjustup(HPDataType* a, int child);
//向下调整算法
void AdjustDown(HPDataType* a, int n, int parent);void HPArrayInit(HP* hp, HPDataType* a, int n)
{assert(hp);hp->a = (HPDataType*)malloc(sizeof(HPDataType) * n);if (hp->a == NULL){perror("malloc fail");return;}memcpy(hp->a, a, n * sizeof(HPDataType));hp->size = hp->capacity = n;//向上调整,建堆时间复杂度O(N*logN)for (int i = 1; i < hp->size; i++){Adjustup(hp->a, i);}//向下调整,建堆时间复杂度O(N)for (int i = (hp->size - 1 - 1) / 2; i >= 0; i--){AdjustDown(hp->a, hp->size, i);}
}
//销毁
void HPDestroy(HP* hp)
{assert(hp);hp->size = hp->capacity = 0;free(hp->a);hp->a = NULL;
}
//交换函数
void Swap(HPDataType* px, HPDataType* py)
{HPDataType tmp;tmp = *px;*px = *py;*py = tmp;
}
//向上调整算法
void Adjustup(HPDataType* a, int child)
{int parent = (child - 1) / 2;while (child > 0){if (a[child] < a[parent]){Swap(&a[child], &a[parent]);child = parent;parent = (parent - 1) / 2;//找原本父亲的父亲下标}else{break;}}
}
// 堆的插入
void HPPush(HP* hp, HPDataType x)
{assert(hp);//扩容if (hp->size == hp->capacity){int newcapacity = hp->capacity == 0 ? 4 : 2 * hp->capacity;HPDataType* tmp = (HPDataType*)realloc(hp->a, sizeof(HPDataType) * newcapacity);if (tmp == NULL){perror("realloc fail");return;}hp->a = tmp;hp->capacity = newcapacity;}hp->a[hp->size++] = x;Adjustup(hp->a, hp->size - 1);
}
//向下调整算法
void AdjustDown(HPDataType* a, int n, int parent)
{int child = parent * 2 + 1;while (child < n){if (child + 1 < n && a[child + 1] < a[child]){child++;}if (a[child] < a[parent]){Swap(&a[child], &a[parent]);parent = child;child = child * 2 + 1;}else{break;}}
}
// 堆的删除
void HPPop(HP* hp)
{assert(hp);assert(hp->size > 0);Swap(&hp->a[0], &hp->a[hp->size - 1]);hp->size--;AdjustDown(hp->a, hp->size, 0);
}
// 取堆顶的数据
HPDataType HPTop(HP* hp)
{assert(hp);return hp->a[0];
}
// 堆的数据个数
int HPSize(HP* hp)
{assert(hp);return hp->size;
}
// 堆的判空
int HPEmpty(HP* hp)
{assert(hp);return hp->size == 0;
}
void HeapSort(int* a, int n)
{HP hp;HPArrayInit(&hp, a, n);int i = 0;while (!HPEmpty(&hp)){a[i++] = HPTop(&hp);//将堆顶数据放入数组中HPPop(&hp);//再已放入数组中的堆顶数据删除}HPDestroy(&hp);
}
int main()
{int a[] = { 60,70,65,50,32,100 };HeapSort(a, sizeof(a) / sizeof(int));for (int i = 0; i < sizeof(a) / sizeof(int); i++){printf("%d ", a[i]);}return 0;
}

运行结果

欢迎各位大佬一起学习交流~

http://www.laogonggong.com/news/88085.html

相关文章:

  • 晋城龙采网站建设谷歌sem服务商
  • php源码项目 门户网站开发wordpress截图
  • 建水县住房和城乡建设局网站石材网站源码
  • 网站建设与制作外包服务如何做微信个人网站
  • 根据描述生成图片的网站网站页面素材
  • 考生登录贵州省住房和城乡建设厅网站好的平面设计灵感网站
  • 网站建设用什么字体做网站在阿里云上面买哪个服务
  • 企业网站建设源码+微信+手机深圳做网站(官网)
  • 如果做网站推广网站推广软文欣赏
  • 组织部信息化建设官方网站网站备案要到哪里
  • 公众号的网站怎么做的阿里巴巴外贸平台怎么样
  • 无棣网站建设怎么做公众号网站
  • 中国建设银行有哪些招聘网站网站栏目规划注意事项
  • 意大利设计网站怎么制作网站模版
  • 上海门户网站怎么登录搭建网站架构是什么意思
  • wordpress海外建站科技类网站
  • 网站公司云建站怎么样淮南网站seo
  • 网站 改版 建议wordpress 隐私策略
  • 高端网站开发成本越秀定制型网站建设
  • 网络彩票网站开发上海公司黄页
  • 游戏租号网站怎么建设网站icp备案证明
  • 山东响应式网站正版网站设计制作
  • 做外贸生意最好的网站学计算机网站建设
  • 郑州建站价格食品类建设网站的目的
  • 网站建设项目描述范文接软件开发项目的平台
  • 德州哪里有学做网站的免费 wordpress主题
  • 厦门网站建设高级课程wordpress登录才能
  • 灵芝产品网站建设方案枣庄市建设局网站
  • 成都网站网络建设手机网站关于我们
  • 公司网站推广运营wordpress cdn 不刷新