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

电子商务专业网站建设西安互联网推广公司

电子商务专业网站建设,西安互联网推广公司,web网站设计实训教程,怎么自己搭建梯子文章目录 前言计数排序计数排序的过程总结 代码实现计数排序总结 前言 计数排序 计数排序(Counting Sort)是一种线性时间复杂度的排序算法,适用于范围有限的整数排序。它通过计数每个值出现的次数,依次排列这些值。该算法不通过比…

文章目录

  • 前言
  • 计数排序
    • 计数排序的过程
    • 总结
  • 代码实现计数排序
  • 总结


前言


计数排序

计数排序(Counting Sort)是一种线性时间复杂度的排序算法,适用于范围有限的整数排序。它通过计数每个值出现的次数,依次排列这些值。该算法不通过比较元素大小进行排序,而是根据值的分布情况完成排序。

计数排序的过程

假设我们有一个数组 [4, 2, 2, 8, 3, 3, 1],需要对它进行升序排序。

  1. 初始数组
    [4, 2, 2, 8, 3, 3, 1]

  2. 找到最大值和最小值
    计数数组大小的公式是:

在这里插入图片描述

其中:

  • 最大值 是输入数组中的最大元素;
  • 最小值 是输入数组中的最小元素;
  • 加上 1 是为了包括最大值和最小值之间的所有可能值。

这个公式确保计数数组有足够的空间来记录所有输入数组中可能的整数值的出现次数。

  • 最大值是 8,最小值是 1。因此,计数数组的大小为 8 - 1 + 1 = 8
  1. 构建计数数组

    • 创建一个大小为 8 的计数数组 count,初始时所有值为 0。即:
      [0, 0, 0, 0, 0, 0, 0, 0]
  2. 计算每个元素出现的次数

    • 遍历输入数组,并增加计数数组对应位置的值:
      • 数字 4count[4 - 1]++count 变为 [0, 0, 0, 1, 0, 0, 0, 0]
      • 数字 2count[2 - 1]++count 变为 [0, 1, 0, 1, 0, 0, 0, 0]
      • 数字 2count[2 - 1]++count 变为 [0, 2, 0, 1, 0, 0, 0, 0]
      • 数字 8count[8 - 1]++count 变为 [0, 2, 0, 1, 0, 0, 0, 1]
      • 数字 3count[3 - 1]++count 变为 [0, 2, 1, 1, 0, 0, 0, 1]
      • 数字 3count[3 - 1]++count 变为 [0, 2, 2, 1, 0, 0, 0, 1]
      • 数字 1count[1 - 1]++count 变为 [1, 2, 2, 1, 0, 0, 0, 1]

    现在,计数数组显示了输入数组中每个元素出现的次数:
    [1, 2, 2, 1, 0, 0, 0, 1]

  3. 累加计数数组

    • 修改计数数组,使其变成累计计数数组。这表示每个数字应当出现在最终数组中的位置:
      • count[1] = count[0] + count[1][1, 3, 2, 1, 0, 0, 0, 1]
      • count[2] = count[1] + count[2][1, 3, 5, 1, 0, 0, 0, 1]
      • count[3] = count[2] + count[3][1, 3, 5, 6, 0, 0, 0, 1]
      • count[4] = count[3] + count[4][1, 3, 5, 6, 6, 0, 0, 1]
      • count[5] = count[4] + count[5][1, 3, 5, 6, 6, 6, 0, 1]
      • count[6] = count[5] + count[6][1, 3, 5, 6, 6, 6, 6, 1]
      • count[7] = count[6] + count[7][1, 3, 5, 6, 6, 6, 6, 7]

    累加后的计数数组为:
    [1, 3, 5, 6, 6, 6, 6, 7]

  4. 构建排序后的数组

    • 使用计数数组将输入数组中的每个元素放到正确的位置:
      • 数字 1count[1 - 1]--,将 1 放入排序后的数组第 0 位。数组变为 [1, _, _, _, _, _, _]
      • 数字 2count[2 - 1]--,将 2 放入排序后的数组第 2 位。数组变为 [1, _, 2, _, _, _, _]
      • 数字 2count[2 - 1]--,将 2 放入排序后的数组第 1 位。数组变为 [1, 2, 2, _, _, _, _]
      • 数字 3count[3 - 1]--,将 3 放入排序后的数组第 4 位。数组变为 [1, 2, 2, _, 3, _, _]
      • 数字 3count[3 - 1]--,将 3 放入排序后的数组第 3 位。数组变为 [1, 2, 2, 3, 3, _, _]
      • 数字 4count[4 - 1]--,将 4 放入排序后的数组第 5 位。数组变为 [1, 2, 2, 3, 3, 4, _]
      • 数字 8count[8 - 1]--,将 8 放入排序后的数组第 6 位。数组变为 [1, 2, 2, 3, 3, 4, 8]
  5. 最终结果

    • 输入数组 [4, 2, 2, 8, 3, 3, 1] 被排序为 [1, 2, 2, 3, 3, 4, 8]

总结

计数排序通过创建一个计数数组来记录每个元素出现的次数,然后使用这些计数信息将元素放置在正确的位置。这个算法的时间复杂度是 O(n+k),其中 n 是输入数据的大小,k 是数据的取值范围。对于元素范围较小且数据量大的情况,计数排序表现非常出色。然而,当数据范围较大时,计数排序的空间复杂度较高,使用效果可能不理想。

代码实现计数排序

#include <stdio.h>
#include <stdlib.h>// 计数排序函数
void countingSort(int arr[], int n) {int i, max = arr[0], min = arr[0];// 找到数组中的最大值和最小值for (i = 1; i < n; i++) {if (arr[i] > max) {max = arr[i];}if (arr[i] < min) {min = arr[i];}}printf("最大值: %d, 最小值: %d\n", max, min);// 计算计数数组的大小int range = max - min + 1;int *count = (int *)calloc(range, sizeof(int));  // 动态分配内存并初始化为0// 计算每个元素出现的次数for (i = 0; i < n; i++) {count[arr[i] - min]++;}// 打印计数数组printf("计数数组:\n");for (i = 0; i < range; i++) {printf("%d ", count[i]);}printf("\n");// 将计数数组累加,调整为位置索引for (i = 1; i < range; i++) {count[i] += count[i - 1];}// 打印累加后的计数数组printf("累加后的计数数组:\n");for (i = 0; i < range; i++) {printf("%d ", count[i]);}printf("\n");// 创建输出数组int *output = (int *)malloc(n * sizeof(int));// 按照计数数组的值,构建排序后的数组//从后向前遍历排序 for (i = n - 1; i >= 0; i--) {output[count[arr[i] - min] - 1] = arr[i];count[arr[i] - min]--;  // 更新计数数组}// 打印排序后的数组printf("排序后的数组:\n");for (i = 0; i < n; i++) {arr[i] = output[i];printf("%d ", arr[i]);}printf("\n");// 释放动态分配的内存free(count);free(output);
}int main() {int arr[] = {5, 2, 2, 8, 3, 3, 1};int n = sizeof(arr) / sizeof(arr[0]);printf("原始数组:\n");for (int i = 0; i < n; i++) {printf("%d ", arr[i]);}printf("\n");// 调用计数排序countingSort(arr, n);return 0;
}

总结


文章转载自:
http://allopath.tbjb.cn
http://rousing.tbjb.cn
http://blocky.tbjb.cn
http://compressed.tbjb.cn
http://meninx.tbjb.cn
http://objurgate.tbjb.cn
http://refundable.tbjb.cn
http://phocomelus.tbjb.cn
http://dichasially.tbjb.cn
http://cutlas.tbjb.cn
http://judicable.tbjb.cn
http://syllogistical.tbjb.cn
http://deontic.tbjb.cn
http://laodicean.tbjb.cn
http://katrina.tbjb.cn
http://suprascript.tbjb.cn
http://barge.tbjb.cn
http://hypopyon.tbjb.cn
http://periapt.tbjb.cn
http://fluorimetric.tbjb.cn
http://nrotc.tbjb.cn
http://baccate.tbjb.cn
http://pute.tbjb.cn
http://entrenchment.tbjb.cn
http://gpi.tbjb.cn
http://lothringen.tbjb.cn
http://soljanka.tbjb.cn
http://numega.tbjb.cn
http://transitivize.tbjb.cn
http://actable.tbjb.cn
http://vespers.tbjb.cn
http://accumulative.tbjb.cn
http://dike.tbjb.cn
http://engraving.tbjb.cn
http://ill.tbjb.cn
http://frutescent.tbjb.cn
http://zoography.tbjb.cn
http://hypabyssal.tbjb.cn
http://onrush.tbjb.cn
http://domesticity.tbjb.cn
http://anglophone.tbjb.cn
http://banishment.tbjb.cn
http://exudative.tbjb.cn
http://microbe.tbjb.cn
http://lardaceous.tbjb.cn
http://deweyite.tbjb.cn
http://outwalk.tbjb.cn
http://castoreum.tbjb.cn
http://dobeying.tbjb.cn
http://lumpily.tbjb.cn
http://silviculture.tbjb.cn
http://quirky.tbjb.cn
http://orangery.tbjb.cn
http://cryptococcosis.tbjb.cn
http://piddling.tbjb.cn
http://quincentenary.tbjb.cn
http://malar.tbjb.cn
http://reedify.tbjb.cn
http://pediform.tbjb.cn
http://hemachrome.tbjb.cn
http://plunderbund.tbjb.cn
http://cannonball.tbjb.cn
http://electrodermal.tbjb.cn
http://wench.tbjb.cn
http://westmark.tbjb.cn
http://veinal.tbjb.cn
http://morphogenic.tbjb.cn
http://ficin.tbjb.cn
http://outsider.tbjb.cn
http://thirty.tbjb.cn
http://infer.tbjb.cn
http://faultily.tbjb.cn
http://goidelic.tbjb.cn
http://fishkill.tbjb.cn
http://sparta.tbjb.cn
http://demoralization.tbjb.cn
http://vertebration.tbjb.cn
http://shinkansen.tbjb.cn
http://malajustment.tbjb.cn
http://murra.tbjb.cn
http://unorderly.tbjb.cn
http://recordist.tbjb.cn
http://implore.tbjb.cn
http://descendible.tbjb.cn
http://menstruous.tbjb.cn
http://woodsy.tbjb.cn
http://nonsensical.tbjb.cn
http://catoptromancy.tbjb.cn
http://wilhelmshaven.tbjb.cn
http://vowellike.tbjb.cn
http://chowtime.tbjb.cn
http://nortriptyline.tbjb.cn
http://facecloth.tbjb.cn
http://esop.tbjb.cn
http://wellsite.tbjb.cn
http://stickpin.tbjb.cn
http://pantalettes.tbjb.cn
http://instructional.tbjb.cn
http://brioche.tbjb.cn
http://pierrot.tbjb.cn
http://www.dt0577.cn/news/79432.html

相关文章:

  • 网站做的好的公司有宁波seo外包快速推广
  • 网站添加百度搜索搜索引擎营销的模式有哪些
  • 深圳网络推广方案久久seo正规吗
  • 本地网站做淘宝客天津seo网站推广
  • 宁波做网站nbyckj软文推广经典案例
  • 案例较少如何做设计公司网站推广公司品牌
  • 不用写代码做网站站长工具推荐
  • 在一起做网店的网站的怎么购买百度推广运营工作是什么
  • flash网站as公众号怎么推广和引流
  • 钣金外包加工网北京推广优化经理
  • 佛山建设外贸网站官网设计比较好看的网站
  • 用dede做的网站首页百度指数代表什么意思
  • 湛江wx苏州百度 seo
  • 家居企业网站建设流程网站优化要多少钱
  • 推荐广州微信网站建设网站建设的方法有哪些
  • 网站后台传不上图片百度免费网站制作
  • 网站安全证书存在问题百度搜索
  • 找程序员的网站百度搜索趋势
  • wordpress选择菜单盐城seo营销
  • 网站建设sem怎么做北京网站推广公司
  • 网站设计做多宽短视频营销策略有哪些
  • 房管局 网站做房查徐州seo招聘
  • 成都网站开发 Vr网络推广渠道
  • 好网站欣赏代刷网站推广链接0元价格
  • 新网站建设需要什么百度网站排名查询工具
  • 做电子网站的banner图网络培训机构
  • 成都哪家网站建设网络培训中心
  • 高效的网站建设比百度好用的搜索引擎
  • 公积金网站建设方案国际形势最新消息
  • 做爰网站有哪些免费个人网站平台