Rafi sorting taka using counting sort

          Are you trying to learn Counting sort? Then you are in right place. Today I will show you how does counting sort exactly work. So keep looking at the article.

          To discuss Counting sort, I wanna share a story. This story about my friend Rafi. I think you will understand quickly if you read this carefully. So let’s begin. Rafi was hosting a class party that’s why he took money from everyone. After getting the money he was trying to sort them in ascending order. So that he just used a trick to sort them easily. He counts the total number of each note separately and separated them from other notes. Assume that he had some 50tk, 100tk, 500tk and after counting he got 10 notes of 50tk, 5 notes of 100tk and 2 notes of 500tk. Since he was trying to sort them in ascending order that’s why at first he laid down 10 notes of 50tk in a row then he laid down 5 notes of 100tk and finally he placed 2 notes of 500tk. Now they are sorted. Did you see it carefully? Have you guessed anything? This procedure is similar to Counting sort algorithm. Hopefully, now Counting sort algorithm will be easy to understand for you. Let’s discuss algorithm how it does work.

          Counting sort algorithm time complexity is O(m × n). At first counting sort algorithm count the total numbers of each element. Then it prints all the elements in that order what you have designed for. If you read the above story carefully then you can understand. But It has some limitation that is

  1. It can sort only positive numbers.
  2. It can’t sort negative numbers.
  3. It can sort only less than maximum array size elements.
  4. For fewer elements and the large numbers, it isn’t an appropriate algorithm. 

          Below I have given the counting sort algorithm for sort in ascending order. For descending order, you have to reverse printing first loop. 

Algorithm

Screenshot (169).png

Code

Before seeing this code ensure that you have tried hard most!!!

Please be honest with yourself. Happy coding.

#include<iostream>

using namespace std;

int main()
{
    long long int Data[99999]={0};
    long long int Element_number;
    long long int Max_element=0;
    long long int Element;

    cout<<"Enter the number of element : "<<endl;
    cin>>Element_number;
    cout<<"Enter Elements : ";

    for(int Counter=0; Counter<Element_number; Counter++)
    {
        cin>>Element;
        Data[Element]++;
        Max_element=max(Max_element,Element); //This will detect the largest element of the data
    }

    /**Here we will print the Data in a sequence otherwise
    we have to do nothing **/

    cout<<"Sorted Elements : ";

    for(int Index=0; Index<=Max_element; Index++)
    {
        for(int Counter=0; Counter<Data[Index]; Counter++)
            cout<<Index<<" ";
    }
    cout<<endl;

    return 0;
}

Be with us, stay happy. Please do your precious comment and give an opportunity to improve us. Happy coding.

Leave a comment

Website Powered by WordPress.com.

Up ↑

Design a site like this with WordPress.com
Get started