📜  C计分程序

📅  最后修改于: 2021-05-28 05:00:55             🧑  作者: Mango

计数排序是一种基于特定范围之间的键的排序技术。它通过计算具有不同键值(哈希类型)的对象的数量来工作。然后做一些算术计算每个对象在输出序列中的位置。

// C Program for counting sort
#include 
#include 
#define RANGE 255
  
// The main function that sort the given string arr[] in
// alphabatical order
void countSort(char arr[])
{
    // The output character array that will have sorted arr
    char output[strlen(arr)];
  
    // Create a count array to store count of inidividul
    // characters and initialize count array as 0
    int count[RANGE + 1], i;
    memset(count, 0, sizeof(count));
  
    // Store count of each character
    for(i = 0; arr[i]; ++i)
        ++count[arr[i]];
  
    // Change count[i] so that count[i] now contains actual
    // position of this character in output array
    for (i = 1; i <= RANGE; ++i)
        count[i] += count[i-1];
  
    // Build the output character array
    for (i = 0; arr[i]; ++i)
    {
        output[count[arr[i]]-1] = arr[i];
        --count[arr[i]];
    }
  
    // Copy the output array to arr, so that arr now
    // contains sorted characters
    for (i = 0; arr[i]; ++i)
        arr[i] = output[i];
}
  
// Driver program to test above function
int main()
{
    char arr[] = "geeksforgeeks";//"applepp";
  
    countSort(arr);
  
    printf("Sorted character array is %sn", arr);
    return 0;
}
输出:
Sorted character array is eeeefggkkorssn

请参阅关于计数排序的完整文章以获取更多详细信息!

想要从精选的最佳视频中学习和练习问题,请查看《基础知识到高级C的C基础课程》。