增加通过次数

发布于 2024-12-02 14:57:09 字数 2538 浏览 0 评论 0原文

我想增加传递次数,但到目前为止我的输出显示的格式不正确。例如,我输入了 5 个数字,如 5 4 3 2 1。格式为:

Pass 1: 1 4 3 2 5
通过 2: 1 4 3 2 5
通过 3: 1 2 3 4 5
通过 4: 1 2 3 4 5
通过 5: 1 2 3 4 5

但我当前的输出是:

通过 1: 2: 3: 4: 5: 1 4 3 2 5
通过 1: 2: 3: 4: 5: 1 4 3 2 5
通过 1: 2: 3: 4: 5: 1 2 3 4 5
通过 1: 2: 3: 4: 5: 1 2 3 4 5
Pass 1: 2: 3: 4: 5: 1 2 3 4 5

我被 for 循环语句困住了。任何想法我将如何做这种输出。我正在做一个快速排序。

    int[] nums = new int[100];
    int SizeNum;
    bool isNum = false;

    private void ExeButton_Click(object sender, EventArgs e)
    {
            string SizeString = SizeTextBox.Text;
            isNum = Int32.TryParse(SizeString, out SizeNum);
            string[] numsInString = EntNum.Text.Split(' ');   //split values in textbox
            for (int j = 0; j < numsInString.Length; j++)
            {
                nums[j] = int.Parse(numsInString[j]);
            }
            if (SizeNum == numsInString.Length)
            {

                sortArray();
            }
        }
    }

    public void q_sort(int left, int right)
    {
        int pivot, l_hold, r_hold;

        l_hold = left;
        r_hold = right;
        pivot = nums[left];

        while (left < right)
        {
            while ((nums[right] >= pivot) && (left < right))
            {
                right--;
            }

            if (left != right)
            {
                nums[left] = nums[right];
                left++;
            }

            while ((nums[left] <= pivot) && (left < right))
            {
                left++;
            }
            if (left != right)
            {
                nums[right] = nums[left];
                right--;
            }
        }
        nums[left] = pivot;
        pivot = left;
        left = l_hold;
        right = r_hold; 
        Display();
        if (left < pivot)
        {
            q_sort(left, pivot - 1);
        }
        if (right > pivot)
        {
            q_sort(pivot + 1, right);
        }
    }

    public void sortArray()
    {
        q_sort(0, SizeNum - 1);
    }

    public void Display()
    {
        int i;
        int x;
        String numbers = "";
        ResultText.AppendText("Pass ");
        for (x = 1; x < SizeNum; x++)
        {

            ResultText.AppendText(" " + x + ": ");

        }
        for (i = 0; i < SizeNum; i++)
        {
            numbers += nums[i].ToString() + " , ";
        }
        ResultText.AppendText(numbers + "\n");
    }

I want to increment the number of passes but the output I have so far displays incorrect format. For example, I entered 5 numbers like 5 4 3 2 1. The format would be:

Pass 1: 1 4 3 2 5
Pass 2: 1 4 3 2 5
Pass 3: 1 2 3 4 5
Pass 4: 1 2 3 4 5
Pass 5: 1 2 3 4 5

but my current output is:

Pass 1: 2: 3: 4: 5: 1 4 3 2 5
Pass 1: 2: 3: 4: 5: 1 4 3 2 5
Pass 1: 2: 3: 4: 5: 1 2 3 4 5
Pass 1: 2: 3: 4: 5: 1 2 3 4 5
Pass 1: 2: 3: 4: 5: 1 2 3 4 5

I'm stuck with the for loop statements. Any ideas how will I do this kind of output. I'm doing a quick sort.

    int[] nums = new int[100];
    int SizeNum;
    bool isNum = false;

    private void ExeButton_Click(object sender, EventArgs e)
    {
            string SizeString = SizeTextBox.Text;
            isNum = Int32.TryParse(SizeString, out SizeNum);
            string[] numsInString = EntNum.Text.Split(' ');   //split values in textbox
            for (int j = 0; j < numsInString.Length; j++)
            {
                nums[j] = int.Parse(numsInString[j]);
            }
            if (SizeNum == numsInString.Length)
            {

                sortArray();
            }
        }
    }

    public void q_sort(int left, int right)
    {
        int pivot, l_hold, r_hold;

        l_hold = left;
        r_hold = right;
        pivot = nums[left];

        while (left < right)
        {
            while ((nums[right] >= pivot) && (left < right))
            {
                right--;
            }

            if (left != right)
            {
                nums[left] = nums[right];
                left++;
            }

            while ((nums[left] <= pivot) && (left < right))
            {
                left++;
            }
            if (left != right)
            {
                nums[right] = nums[left];
                right--;
            }
        }
        nums[left] = pivot;
        pivot = left;
        left = l_hold;
        right = r_hold; 
        Display();
        if (left < pivot)
        {
            q_sort(left, pivot - 1);
        }
        if (right > pivot)
        {
            q_sort(pivot + 1, right);
        }
    }

    public void sortArray()
    {
        q_sort(0, SizeNum - 1);
    }

    public void Display()
    {
        int i;
        int x;
        String numbers = "";
        ResultText.AppendText("Pass ");
        for (x = 1; x < SizeNum; x++)
        {

            ResultText.AppendText(" " + x + ": ");

        }
        for (i = 0; i < SizeNum; i++)
        {
            numbers += nums[i].ToString() + " , ";
        }
        ResultText.AppendText(numbers + "\n");
    }

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

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

发布评论

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

评论(4

旧情勿念 2024-12-09 14:57:09

最简单的方法是通过快速排序函数传递调试变量以跟踪当前状态,例如:

public void q_sort(int left, int right, int currentPass)
{
    /* ... */

    Display(currentPass);

    /* ... */

    q_sort(left, pivot - 1, currentPass + 1);
    q_sort(pivot + 1, right, currentPass + 1);

    /* ... */
}

public void Display(int currentPass)
{
    ResultText.AppendText("Pass " + currentPass);

    // output the array contents as you currently do
}

The easiest way would be to pass a debugging variable along through your quicksort function to keep track of the current state, e.g.:

public void q_sort(int left, int right, int currentPass)
{
    /* ... */

    Display(currentPass);

    /* ... */

    q_sort(left, pivot - 1, currentPass + 1);
    q_sort(pivot + 1, right, currentPass + 1);

    /* ... */
}

public void Display(int currentPass)
{
    ResultText.AppendText("Pass " + currentPass);

    // output the array contents as you currently do
}
蓝咒 2024-12-09 14:57:09

您的 SizeNum 始终为 5,因此 Display: 中的循环

    for (x = 1; x < SizeNum; x++)
    {

        ResultText.AppendText(" " + x + ": ");

    }

将始终打印 1-5。您需要以某种方式让 Display 知道它被调用了多少次才能获得正确的标签。

your SizeNum is always 5 so the loop in Display:

    for (x = 1; x < SizeNum; x++)
    {

        ResultText.AppendText(" " + x + ": ");

    }

will always print 1-5. You need to somehow let Display know how many times its been called in order to get the right label.

眼泪都笑了 2024-12-09 14:57:09

让我们看一下第一个 for 循环:

for (x = 1; x < SizeNum; x++)
{
    ResultText.AppendText(" " + x + ": ");
}

请注意,您正在从 1 计数到 SizeNum。此循环将每个数字附加到您的结果中,这就是为什么您在每行上得到 Pass 1: 2: 3: 4: 5: 的原因。如果您接受更大的数字数组,您将看到输出反映了这一点。例如,如果您输入一个包含 10 个数字的数组,您将看到 Pass 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 后跟该数组。

为了获得您正在寻找的内容,您需要将当前迭代传递给您的函数,正如一些答案已经指出的那样,如下所示:

private void Display(int pass)
    // initialise your other variables here
    ResultText.AppendText("Pass " + pass + ": ");

    // output your array as normal here

Let's take a look at that first for loop:

for (x = 1; x < SizeNum; x++)
{
    ResultText.AppendText(" " + x + ": ");
}

Note that you're counting from 1 to SizeNum. This loop appends each number to your result, which is why you get Pass 1: 2: 3: 4: 5: on each line. If you take in a larger array of numbers, you'll see the output reflect that. For example, if you take in an array of 10 numbers, you'll see Pass 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: followed by the array.

To get what you're looking for, you'll need to pass in the current iteration to your function, as some answers have already pointed out, like so:

private void Display(int pass)
    // initialise your other variables here
    ResultText.AppendText("Pass " + pass + ": ");

    // output your array as normal here
无人问我粥可暖 2024-12-09 14:57:09

在类的顶部添加

int Pass=0;

,然后在显示例程中替换

for (x = 1; x < SizeNum; x++)  
        {  

            ResultText.AppendText(" " + x + ": ");  

       } 

  ResultText.AppendFormat("{0}: ",Pass++);

由于您的代码看起来可以运行多次,您可能想在 SortArray(); 中重置 Pass=1;

at the top of your class add

int Pass=0;

and then in your display routine replace

for (x = 1; x < SizeNum; x++)  
        {  

            ResultText.AppendText(" " + x + ": ");  

       } 

with

  ResultText.AppendFormat("{0}: ",Pass++);

As your code looks like it could run more than once you probably want to reset Pass=1 in SortArray();

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