Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- final int count = 100;
- boolean showVals = false;
- boolean hideFrames = false;
- Line[] lines = new Line[count];
- float stWg;
- final double decrFact = 1.247330950103979;
- boolean notShuffled = true;
- boolean notSorted = false;
- boolean swapped = true;
- boolean notFinished = false;
- int iShuf=0;
- int iSort=0;
- int gap=count;
- int k=0;
- int framesHidden=1;
- void setup() {
- //size(1500, 300);
- fullScreen();
- colorMode(HSB);
- fill(0);
- background(random(200), random(200), random(200));
- stWg=(width)/count+1;
- strokeWeight(stWg);
- int index=0;
- for (int i=0; i<count; i++) {
- lines[index] = new Line(i, i);
- lines[index++].display();
- }
- }
- void draw() {
- if (hideFrames) {
- framesHidden=round(frameRate)/5+1;
- }
- for (int ind=0; ind<framesHidden; ind++) {
- if (notShuffled) {
- int randpos = round(random(count-1));
- lines[iShuf++].swap(lines[randpos]);
- if (iShuf>=count) {
- notShuffled=false;
- notSorted=true;
- }
- printText();
- }
- if (notSorted) {
- if (k+gap<count) {
- if (lines[k].pos>lines[k+gap].pos) {
- lines[k].swap(lines[k+gap]);
- }
- k++;
- } else {
- k=0;
- gap/=decrFact;
- iSort++;
- }
- if (gap==0) {
- notSorted=false;
- notFinished=true;
- }
- printText();
- }
- if (notFinished) {
- for (Line it : lines) {
- it.display();
- }
- notFinished = false;
- printText();
- println();
- println("FIN");
- }
- }
- displayText();
- }
- void displayText() {
- textSize(50);
- if (notShuffled) {
- fill(255);
- rect(width/2-10, 5, 280, 55);
- fill(0);
- text("SHUFFLING", width/2, 50);
- } else if (notSorted) {
- fill(255);
- rect(0, height/2-45, 240, 55);
- fill(0);
- text("SORTING", 10, height/2);
- } else {
- text("SORTED", width/3, height/2);
- }
- }
- void printText() {
- println(iShuf+"; "+iSort+"; "+gap);
- }
- void mouseClicked() {
- notShuffled=true;
- iShuf=0;
- iSort=0;
- gap=count;
- k=0;
- }
- class Line {
- int pos;
- int val;
- color col;
- Line (int _pos, int _val) {
- pos=_pos;
- val=_val;
- }
- void display() {
- float H=map(val, 0, count, 0, 255);
- stroke(H, 255, 255);
- H = map(pos, 0, count, stWg/2, width+stWg/2);
- line(H, 0, H, height);
- if (showVals) {
- textSize(stWg*3/5);
- text(val+1, H-stWg/2, (height+stWg/3)/2);
- }
- }
- void swap(Line drug) {
- int temp = drug.pos;
- drug.pos = pos;
- pos = temp;
- display();
- drug.display();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement