-1

I have to create a JavaFX program that creates a list of randomly generated 5-digit integers, then use BubbleSort to sort them. I have to bind the sorting method to a progress bar to show it's status. How would I go about binding the progress bar to the BubbleSort method? (the size of the list can be varied to get the best results)

BWIS
  • 11
  • 1
  • 2
    First, you need to determine the notion of progress. Progress is a percentage/ratio; how do you know how far along your bubble sort algorithm is? (To be honest, I doubt there is a way to know it.) – VGR Oct 23 '19 at 19:45
  • 1
    As VGR already said. Even if you update your ratio after every sort it will still be too fast. BubbleSort with 50 elements takes no time at all. Either you raise the amount of items you want to sort or you artificially slow down the sort operations up to the point that a progress bar can be observed and won't instantly jump from 0 to 100. – sarrrrek Oct 23 '19 at 21:12
  • https://stackoverflow.com/questions/53573747/dynamic-bar-chart-in-java-fx/53579013#53579013 – SedJ601 Oct 25 '19 at 03:44

1 Answers1

2

ProgressBars often don't measure real progress (because they sometimes can't know exactly how long something will really take to complete and exactly how far along in the process the progress currently is). If you can, then use real progress, otherwise use an estimate. In this case an estimate might be work better unless you can figure out some way to measure real progress of your bubble sort (I haven't tried to do that, and I don't know if you can, as pointed out by VGR in comments).

What you could do, is benchmark your sorting based upon different sized input sets. If the sort will be expected to take under a second for the size of input given, don't even bother showing a Progress Bar. If the sort is expected to take longer, when sorting actual data, calculate a time estimate based on your benchmark results for different input set sizes. Run a Timeline independent of the actual sort algorithm, which updates progress based upon your estimate. When actually done sorting, stop the timeline and set progress to 100%. It won't be perfect, but probably will be good enough.

I'll leave the implementation of this suggested strategy to an interested reader.

Another, simpler, solution is to use an indeterminate progress bar, which just spins until everything is complete. It isn't really showing progress at all, it just shows that something is (probably) happening. Sometimes that is an OK solution for some tasks, but for your sort progress measurement task, displaying a progress estimate or calculation of real progress might be preferred.

jewelsea
  • 150,031
  • 14
  • 366
  • 406