0

As a coding execise, I'm working on a visualisation of different sorting algorithms. I have a button that pauses the algorithm and shows every item of the array as a bar-diagramm. To achive this, I have a nested while-loop inside of the two for-loops for the sorting. It loops throug, until I press the button again, a boolean variable gets set to true and the sorting continues.

However, after the programm exits the while loop, the two indecies of the for-loops (i and j) get reset to 0 and the sorting starts from the beginning again.

swap() and draw() are custom functions that do pretty much exactly what the name suggests.

Here's my code for the sorting:

for (i = 0; i < items.Count(); i++)
    {
        for (j = 0; j < items.Count() - 1 - i; j++)
        {
            //lbl_i.Text = Convert.ToString(i);
            //lbl_j.Text = Convert.ToString(j);

            if (items[j] > items[j + 1])
            {
                swap(j, j + 1); //swaps the items at two given indecies
                draw(); // draws the array to the picturebox
            }

            while (sorting == false) //the sorting is paused
            {
                Application.DoEvents();
            }
        }
    }

Any idea why this could happen?

I have a suspition that it could be a problem with the Application.DoEvents()-call, but I need that so I can press the button.

Also, if you notice anything else in my code that I could do better, please let me know, I'm not very experienced at coding, so any help and constructive criticism is welcome. :-)

Thank You!

Benjamin

6
  • 4
    Use debug and see ! Commented Dec 22, 2016 at 14:55
  • Are you calling this function from a click event on the same button? Commented Dec 22, 2016 at 14:56
  • It sounds to me like you're breaking out of these for loops somehow and starting over. It's hard to say what the problem is without the rest of the code. Commented Dec 22, 2016 at 15:00
  • 2
    j = 0; j < items.Count() - 1 - i; j++ this line here doesn't pass the smell test. why would you subtract i here? Commented Dec 22, 2016 at 15:12
  • krillgar: I'm stupid. Yes I do, so the function gets called again when I want to continue. Thanks! Commented Dec 23, 2016 at 8:15

2 Answers 2

2

You don't create i and j in the for-loops, i and j might get changed somewhere else in your app. Try this:

for (int i = 0; i < items.Count(); i++)
{
    for (int j = 0; j < items.Count() - 1 - i; j++)
    {
        //lbl_i.Text = Convert.ToString(i);
        //lbl_j.Text = Convert.ToString(j);

        if (items[j] > items[j + 1])
        {
            swap(j, j + 1); //swaps the items at two given indecies
            draw(); // draws the array to the picturebox
        }

        while (sorting == false) //the sorting is paused
        {
            Application.DoEvents();
        }
    }
}

The change is in: int i, int j

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

1 Comment

I had it like this at first, and it didn't work either. int i and int j are initialised right before the two loops.
0

Found the answer: The problem was, that I was calling this function from the same button in the first place. In its click-event, I have an if-statement to check if the button said "Pause" or "Start", but when i paused the programm, it said "Start" again. So when I click it, the function gets called again and int i and int j are reinitialised to 0.

Code in the click-event:

    private void btn_start_Click(object sender, EventArgs e)
    {
        //new added code
        if (btn_start.Text == "Start")
        {
            btn_start.Text = "Pause";
            sorting = true;
            sort();
        }
        else if (btn_start.Text == "Pause")
        {
            btn_start.Text = "Continue"; // '= "Start"' in the old code
            tmr_sorting.Stop();
            sorting = false;
        } 
        // new added code
        else if (btn_start.Text == "Continue")
        {
            btn_start.Text = "Pause";
            sorting = true;
        }
    }

Comments

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.