Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int arraySize = 500;
- int count = 0;
- boolean sorted;
- int swapped = 0;
- int access = 0;
- int max;
- int[] collection;
- void setup()
- {
- //size(1080, 600);
- stroke(0,255,0);
- strokeWeight(2);
- frameRate(60);
- background(0);
- //size(500,300);
- fullScreen();
- collection = new int[arraySize];
- for(int i = 0; i < collection.length; i++)
- {
- collection[i] = int(random(collection.length));
- }
- max = collection.length;
- }
- void draw()
- {
- background(0);
- text(swapped, 10,10);
- text(access, 10, 20);
- text(max,10,30);
- if(isSorted())
- {
- for(int i = 0; i < collection.length; i++)
- {
- collection[i] = int(random(collection.length));
- }
- swapped = 0;
- access = 0;
- max = collection.length;
- }
- for(int i = 0; i < collection.length; i++)
- {
- float x = map(i,0,collection.length,25,width - 25);
- stroke(255,map(collection[i],getLowest(),getHighest(),0,150),0);
- line(x, height, x, height - map(collection[i],0,collection.length,0,height));
- }
- bubbleSort(collection);
- }
- int getHighest()
- {
- int highest = 0;
- for(int i = 0; i < collection.length; i++)
- {
- if(collection[i] > highest)
- highest = collection[i];
- }
- return highest;
- }
- int getLowest()
- {
- int lowest = getHighest();
- for(int i = 0; i < collection.length; i++)
- {
- if(collection[i] < lowest)
- lowest = collection[i];
- }
- return lowest;
- }
- boolean isSorted()
- {
- for(int i = 0; i < collection.length - 1; i++)
- {
- if(collection[i] > collection[i + 1])
- return false;
- }
- return true;
- }
- void bubbleSort(int[] a)
- {
- for(int i = 0; i < max -1; i++)
- {
- if(a[i] > a[i + 1])
- {
- int temp = 0;
- temp = a[i];
- a[i] = a[i + 1];
- a[i + 1] = temp;
- swapped++;
- }
- access++;
- }
- max --;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement