C# 堆排序,1 个错误

发布于 2024-09-27 21:46:50 字数 1606 浏览 0 评论 0原文

我的程序中有一个错误,我不知道为什么这个错误出现在 63 行。

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]);
        }


        }
    }

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]);
        }


        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

童话 2024-10-04 21:46:50

如果您能指定什么错误(有时)有帮助,那就太好了。我在这里看到的一件事是,您正在从静态方法 Main 调用非静态代码(堆排序)。如果您确实想要的话,您应该创建一个 Program 对象,或者更好 - 让您的 2 个方法静态。这是初学者的。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文