当前位置: 首页 > 编程笔记 >

计数排序

盖锐进
2023-03-14
本文向大家介绍计数排序,包括了计数排序的使用技巧和注意事项,需要的朋友参考一下

计数排序是一种稳定的排序技术,用于根据较小的键对对象进行排序。它计算键值相同的键的数量。当不同键之间的差异不太大时,此排序技术非常有效,否则会增加空间复杂度。

计数排序技术的复杂性

  • 时间复杂度:O(n + r)

  • 空间复杂度:O(n + r)

输入输出

Input:
A list of unsorted data: 2 5 6 2 3 10 3 6 7 8
Output:
Array before Sorting: 2 5 6 2 3 10 3 6 7 8
Array after Sorting: 2 2 3 3 5 6 6 7 8 10

算法

counting sort(array, size)

输入:数据数组,数组中的总数

输出:排序后的数组

Begin
   max = get maximum element from array.
   define count array of size [max+1]

   for i := 0 to max do
      count[i] = 0 //set all elements in the count array to 0
   done

   for i := 1 to size do
      increase count of each number which have found in the array
   done

   for i := 1 to max do
      count[i] = count[i] + count[i+1] //find cumulative frequency
   done

   for i := size to 1 decrease by 1 do
      store the number in the output array
      decrease count[i]
   done

   return the output array
End

示例

#include<iostream>
#include<algorithm>
using namespace std;

void display(int *array, int size) {
   for(int i = 1; i<=size; i++)
      cout << array[i] << " ";
   cout << endl;
}

int getMax(int array[], int size) {
   int max = array[1];
   for(int i = 2; i<=size; i++) {
      if(array[i] > max)
         max = array[i];
   }

   return max; //the max element from the array
}

void countSort(int *array, int size) {
   int output[size+1];
   int max = getMax(array, size);
   int count[max+1]; //create count array (max+1 number of elements)

   for(int i = 0; i<=max; i++)
      count[i] = 0; //initialize count array to all zero
   for(int i = 1; i <=size; i++)
      count[array[i]]++; //increase number count in count array.
   for(int i = 1; i<=max; i++)
      count[i] += count[i-1]; //find cumulative frequency

   for(int i = size; i>=1; i--) {
      output[count[array[i]]] = array[i];
      count[array[i]] -= 1; //decrease count for same numbers
   }

   for(int i = 1; i<=size; i++) {
      array[i] = output[i]; //store output array to main array
   }
}

int main() {
   int n;
   cout << "Enter the number of elements: ";
   cin >> n;
   int arr[n+1]; //create an array with given number of elements
   cout << "输入元素:" << endl;

   for(int i = 1; i<=n; i++) {
      cin >> arr[i];
   }

   cout << "Array before Sorting: ";
   display(arr, n);
   countSort(arr, n);
   cout << "Array after Sorting: ";
   display(arr, n);
}

输出结果

Enter the number of elements: 10
输入元素:
2 5 6 2 3 10 3 6 7 8
Array before Sorting: 2 5 6 2 3 10 3 6 7 8
Array after Sorting: 2 2 3 3 5 6 6 7 8 10
 类似资料:
  • 计数排序的核心在于将输入的数据值转化为键存储在额外开辟的数组空间中。作为一种线性时间复杂度的排序,计数排序要求输入的数据必须是有确定范围的整数。 1. 动图演示 2. JavaScript 代码实现 function countingSort(arr, maxValue) { var bucket = new Array(maxValue+1), sortedIndex =

  • 主要内容:计数排序算法的实现思路,计数排序算法的具体实现通过统计序列中各个元素出现的次数,完成对整个序列的升序或降序排序,这样的排序算法称为 计数排序算法。 接下来,我们为您系统地讲解计数排序算法。 计数排序算法的实现思路 假设待排序序列为 {4, 2, 2, 8, 3, 3, 1},使用计数排序算法完成升序排序的过程为: 1) 找到序列中的最大值(用 max 表示)。对于 {4, 2, 2, 8, 3, 3, 1} 序列来说,最大值是 8。 2) 创

  • 本文向大家介绍C++计数排序详解,包括了C++计数排序详解的使用技巧和注意事项,需要的朋友参考一下 计数排序不同于比较排序,是基于计数的方式,对于计数排序,假设每一个输入都是介于0~k之间的整数。对于每一个输入元素x,确定出小于x的元素的个数。假如有17个元素小于x,则x就属于第18个输出位置。 计数排序涉及到三个数组A[0…..length-1],length为数组A的长度;数组B与数组A长度相

  • sort()函数返回输入数组的排序副本。 它有以下参数: 其中: 序号 参数及描述 1. a 要排序的数组 2. axis 沿着它排序数组的轴,如果没有数组会被展开,沿着最后的轴排序 3. kind 默认为'quicksort'(快速排序) 4. order 如果数组包含字段,则是要排序的字段 import numpy as np a = np.array([[3,7],[9,1]]) pri

  • 我有一张这样的桌子: 我想按组排序,然后按count(名称)排序,然后按rand()排序,结果如下: 我写了这个查询,但是如果我在group和rand之间添加count(Name),它会给我一个坏结果

  • 我使用的是Spring数据JpaRepository,我发现它非常容易使用。我实际上需要所有这些功能——分页、排序、过滤。不幸的是,有一件令人讨厌的事情似乎迫使我退回到普通JPA的使用上来。 我需要按关联集合的大小订购。例如,我有: 我必须按bes排序。size() 是否有办法以某种方式自定义排序,仍然利用分页、过滤和其他Spring Data的强大功能?