2

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
5
  • Debugging, debugging, debugging.. Commented Nov 28, 2015 at 17:15
  • why so many variables? that just makes things confusing and harder to debug. also counting sort does not have nested loop anyway. Commented Nov 28, 2015 at 17:16
  • Sorry for too many variables But I am stuck on this from 1 day Commented Nov 28, 2015 at 17:17
  • the very simple and stylish way is described and shown here. en.wikipedia.org/wiki/Counting_sort#The_algorithm Commented Nov 28, 2015 at 17:19
  • 1
    I saw this but i want to implement it with my own thinking,, Commented Nov 28, 2015 at 17:25

2 Answers 2

4

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

Sign up to request clarification or add additional context in comments.

2 Comments

Nice find but I think OP's code is b[j] = b[j] (without the -1)
Yes I add '-1` at my own
1

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++)
    {
    }
}​

1 Comment

op didnt ask for bubble sort way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.