tags:

views:

42

answers:

1

Hello!

I have one error in my program , I dont know why this error appears in 63 Line.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HeapSort
{
    class Program
    {
          void restore(int[] T,int k)
           {
               int n = T.Length;

               int i,j;

               i=T[k-1];

               while(k<=n/2)
               {
                   j=2*k;

                   if( (j<n) && (T[j-i] < T[j]) ) j++;
                   if(i>=T[j-1]) break;
                   else
                   {
                       T[k-1] = T[j-1];
                       k=j;
                   }
                   T[k-1]=i;
               }

           }
               //Sort tab T .

               void heapsort(int[] T)
               {
                   int k,swap;
                   int n = T.Length;

                   for(k=n/2;k>0;k--) restore(T,k);

                   do
                   {
                       swap=T[0];
                       T[0]=T[n-1];
                       T[n-1]=swap;
                       n--;
                       restore(T,1);
                   } while(n>1);

               }

        static void Main(string[] args)
        {

        int i;
        int[] T = { 12, 3, -12, 9, 34, 23, 1, 81, 45, 17, 9, 23, 11, 4 };

        for(i = 0;i <T.Length;i++)
            Console.Write(" {0}",T[i]);
            Console.WriteLine();

            heapsort(T); //THE ERROR


            for (i = 0; i < T.Length; i++)
                Console.Write(" {0} ", T[i]);
        }


        }
    }
+2  A: 

It would be nice if you would specify what error, that helps (sometimes). One thing I see here is that you are invoking nonstatic code (heapsort) from static method Main. You should create an object of Program if you really want to, or better - make your 2 methods static. That's for starter.

Ravadre
`An object reference is required for the non-static field, method, or property 'HeapSort.Program.heapsort(int[]) `
Konrad
+1 Agree, make your heapsort and restore both static.
Les
That is the solution.
Konrad