首页 > 其他分享 >CountSort

CountSort

时间:2024-05-06 21:11:07浏览次数:12  
标签:cnt int 关键字 CountSort 表中 排序

有一种简单的排序算法叫作计数排序。这种算法对一个待排序表(用数组A[ ]表示)进行排序排序结果存储在另一个新的表中(用数组B[ ]表示),表中关键字为int 型。必须注意的是,表中所有待排序的关键字互不相同,计数排序算法针对表中的每个关键字,扫描待排序表一趟,统计表中有多少个关键字比该关键字小。假设对某一个关键字,统计出数值为c,那么这个关键字在新的有序表中的位置即为c。设计实现计数排序算法。

/******************************************************************************************************
  * @file name:		  :CountSort
  * @brief  		  :计数排序
  * @author 		  :[email protected]
  * @date 			  :2024/05/06
  * @version          :V1.0
  * @property 		  :暂无
  * @note   		  :None
  * CopyRight (c)  2023-2024   [email protected]   All Right Reseverd
******************************************************************************************************/

/********************************
 *  funName:     BinaryTree_CountLeafNode
 *  funtion:     计数排序        
 *  Argument:
 *               @cnt  :  计数器
 *               @n    :  作为数组A的元素下标
 *               @m    :  记录有多少个元素比当前元素小 
 *  返回结果:     无
 *  注意事项:     None
 *  函数作者:    [email protected]
 *  创建日期:     2024/05/06
 *  修改历史:    None
 *  函数版本:    V1.0
 * ********************************/




void CountSort(int A[],int B[],int size)
{
    int cnt = 0;
    for(int n = 0; n < size; n++)
    {
        cnt = 0;     //计数器置0

        //n作为数组A的元素下标

        for(int m = 0 ; m < size; m++)
        {
            if(A[m] < A[n])
            {
                cnt++;
            }
        }

        B[cnt] = A[n];
    }

}

标签:cnt,int,关键字,CountSort,表中,排序
From: https://www.cnblogs.com/hhail08/p/18175957

相关文章