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