We are about to switch to a new forum software. Until then we have removed the registration on this forum.
This supose to draw a graf of how the buble sort arrange the bars but sometimes only shows the finsh The file "list.txt" is a list of 200 unorder numbers
BufferedReader reader;
int numbers[] = new int[200];
int nof_bars;
boolean start = true;
long lastTime = 0;
void setup()
{
size(900,700);
create_list();
for (int i=0; i < numbers.length;i++)
println(numbers[i]);
nof_bars = numbers.length;
}
void draw()
{
if (start)
{
for(int i=0;i<numbers.length;i++)
for(int j=i;j<numbers.length;j++)
{
int memo =0;
if (numbers[i]<numbers[j])
{
memo = numbers[j];
numbers[j] = numbers[i];
numbers[i] = memo;
background(0);
graf();
}
}
start = !start;
}
}
void graf()
{
for(int i =0; i < numbers.length; i++)
rect(((width)/nof_bars)*i,height,(width)/nof_bars,-1*numbers[i]);
}
void create_list()
{
String[] list = loadStrings("list.txt");
for (int i=0; i < list.length;i++)
numbers[i] = int(list[i]);
}
Answers
You'll get a better response if you format your code. Here's how:
http://forum.processing.org/two/discussion/8045/how-to-format-code-and-text
The display only updates when draw () finishes.
This is by design.
https://forum.processing.org/two/discussion/8085/i-display-images-in-sequence-but-i-see-only-the-last-one-why
for all kind of recursions it would be cool to be able to update the screen throughout somehow.
question: would that be possible using eclipse or JAVA or another workaround?
Just a thought, not sure if it works but would it be a good idea to do the bubble sort in a separate method you launch in setup() and use noloop() and redraw() in each step?
No,
redraw()
also just letsdraw()
update the screen at the end - I guessThe naive approach is :
call sorting from
setup()
copy the full list in an
ArrayList
at each stepin
draw()
replay theArrayList
to show each step throughoutbut this is ugly....
I don't get why the "need" to implement our own sorting algorithm while Java already got sort() for both regular arrays & Collection containers: :O)
It needs a Comparator parameter in case the elements to sort() aren't numbers, strings or don't implement its own Comparable:
http://docs.Oracle.com/javase/8/docs/api/java/lang/Comparable.html
But for something as simple as an
int[]
array, Processing offers sort() too: *-:)https://Processing.org/reference/sort_.html
Also Processing's own containers like IntList, FloatDict, etc., features sorting methods too! :-bd
A bit ugly...but seems to work.
Draw histogram step by step pressing a key.
Yes, my noLoop() idea won't work as setup() will wait until the sort method is finished before moving on to draw.
Here's my solution:
question: would that be possible using eclipse or JAVA or another workaround?
I mean these solutions won't work with recursions anyway
learning exercise.
@Chrisir: with recursions is impossible ( almost for me :) ).
Your solution with ArrayList should work.