开发者

Increment number of passes

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


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
}


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.


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


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();

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜