Nafees
Nafees

Reputation: 1034

Counting Sort Implementation in C#

I am implementing counting sort But some thing is wrong with my code I am new in Programming Please help me to find an error. I am implenting it step by step .

    namespace ConsoleApplication1
    {
        class Program
        {
            public static int[] a = { 0,0,0,5,4,8,9,9,7,3, 3, 2, 1 };
            public static void Sorting()
            {
                int j = 0, i = 0, smallestvalue = 0, largestvalue = 0, n = a.Length, lengthof_B = 0, temp = 0, anothersmallestvalue;
                smallestvalue = largestvalue = a[0];
                for (i = 0; i < n; i++)
                {
                    if (smallestvalue > a[i])
                    {
                        smallestvalue = a[i];
                    }
                    else if (largestvalue < a[i])
                    {
                        largestvalue = a[i];
                    }
                }
                int x = anothersmallestvalue = smallestvalue;

                lengthof_B = largestvalue - smallestvalue + 1;
                int[] b = new int[lengthof_B];
                for (i = 0; i < lengthof_B && smallestvalue <= largestvalue; i++)
                {
                    for (j = 0; j < n; j++)
                    {
                        if (smallestvalue == a[j])
                        {
                            b[i] = b[i] + 1;
                        }
                    }
                    b[i] = temp + b[i];
                    temp = b[i];
                    smallestvalue++;
                }
                int[] c = new int[a.Length];
                                                // I think error here 
                for (i = n - 1; i >= 0; i--)
                {
                    anothersmallestvalue = x;
                    for (j = 0; j <= lengthof_B ; j++)        
                    {

                        if (a[i] == anothersmallestvalue)
                        {
                            temp = b[j];
                            c[temp - 1] = anothersmallestvalue;
                            b[j] = b[j];
                        }
                        anothersmallestvalue++;
                    }
                }            
                for (i = 0; i < c.Length; i++)
                {
                    Console.WriteLine("c[i] : " + c[i]);
                }
            }
        }
        class Demo
        {
            static void Main(string[] args)
            {
                Program.Sorting();
                Console.ReadLine();
            }
        }
    }

Desired Output is

000123457899

But output of my program is

000120457809

Upvotes: 2

Views: 1710

Answers (2)

jdweng
jdweng

Reputation: 34421

Normal sorting your two loops should look like this

for (i = 0; i < lengthof_B - 1; i++)
{
    for (j = i + 1; j < lengthof_B; j++)
    {
    }
}​

Upvotes: 1

Syed Qasim Ahmed
Syed Qasim Ahmed

Reputation: 1362

This Is Your Code Here I found a mistake.

And your Code is too complex Please Go through your code Once more.

for (i = n - 1; i >= 0; i--)
                {
                    anothersmallestvalue = x;
                    for (j = 0; j <= lengthof_B ; j++)        
                    {

                        if (a[i] == anothersmallestvalue)
                        {
                            temp = b[j];
                            c[temp - 1] = anothersmallestvalue;
                            b[j] = b[j] -1 ;// Possible Mistake I think here
                        }
                        anothersmallestvalue++;
                    }
                }            

the very simple and stylish way is described and shown here.

en.wikipedia.org/wiki/Counting_sort#The_algorithm

Upvotes: 4

Related Questions