Advertisement
A-Sin

Untitled

Jan 16th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.62 KB | None | 0 0
  1.  
  2. final int count = 100;
  3. boolean showVals = false;
  4. boolean hideFrames = false;
  5.  
  6. Line[] lines = new Line[count];
  7. float stWg;
  8. final double decrFact = 1.247330950103979;
  9.  
  10. boolean notShuffled = true;
  11. boolean notSorted = false;
  12. boolean swapped = true;
  13. boolean notFinished = false;
  14.  
  15. int iShuf=0;
  16. int iSort=0;
  17. int gap=count;
  18. int k=0;
  19. int framesHidden=1;
  20.  
  21. void setup() {
  22.   //size(1500, 300);
  23.   fullScreen();
  24.   colorMode(HSB);
  25.   fill(0);
  26.   background(random(200), random(200), random(200));
  27.   stWg=(width)/count+1;
  28.   strokeWeight(stWg);
  29.  
  30.   int index=0;
  31.   for (int i=0; i<count; i++) {
  32.     lines[index] = new Line(i, i);
  33.     lines[index++].display();
  34.   }
  35. }
  36.  
  37. void draw() {
  38.   if (hideFrames) {
  39.     framesHidden=round(frameRate)/5+1;
  40.   }
  41.   for (int ind=0; ind<framesHidden; ind++) {  
  42.     if (notShuffled) {    
  43.       int randpos = round(random(count-1));
  44.       lines[iShuf++].swap(lines[randpos]);
  45.       if (iShuf>=count) {
  46.         notShuffled=false;
  47.         notSorted=true;
  48.       }  
  49.       printText();
  50.     }
  51.  
  52.     if (notSorted) {
  53.       if (k+gap<count) {
  54.         if (lines[k].pos>lines[k+gap].pos) {
  55.           lines[k].swap(lines[k+gap]);
  56.         }      
  57.         k++;
  58.       } else {
  59.         k=0;
  60.         gap/=decrFact;
  61.         iSort++;
  62.       }
  63.  
  64.       if (gap==0) {
  65.         notSorted=false;
  66.         notFinished=true;
  67.       }  
  68.       printText();
  69.     }
  70.  
  71.     if (notFinished) {
  72.       for (Line it : lines) {
  73.         it.display();
  74.       }
  75.       notFinished = false;
  76.       printText();
  77.       println();
  78.       println("FIN");
  79.     }
  80.   }
  81.   displayText();
  82. }
  83.  
  84. void displayText() {
  85.   textSize(50);
  86.   if (notShuffled) {
  87.     fill(255);
  88.     rect(width/2-10, 5, 280, 55);
  89.     fill(0);
  90.     text("SHUFFLING", width/2, 50);
  91.   } else if (notSorted) {
  92.     fill(255);
  93.     rect(0, height/2-45, 240, 55);
  94.     fill(0);  
  95.     text("SORTING", 10, height/2);
  96.   } else {
  97.     text("SORTED", width/3, height/2);
  98.   }
  99. }
  100.  
  101. void printText() {
  102.   println(iShuf+"; "+iSort+"; "+gap);
  103. }
  104.  
  105. void mouseClicked() {
  106.   notShuffled=true;
  107.   iShuf=0;
  108.   iSort=0;
  109.   gap=count;
  110.   k=0;
  111. }
  112.  
  113.  
  114. class Line {
  115.   int pos;
  116.   int val;
  117.   color col;
  118.  
  119.   Line (int _pos, int _val) {
  120.     pos=_pos;
  121.     val=_val;
  122.   }
  123.  
  124.   void display() {        
  125.     float H=map(val, 0, count, 0, 255);
  126.     stroke(H, 255, 255);    
  127.     H = map(pos, 0, count, stWg/2, width+stWg/2);
  128.     line(H, 0, H, height);  
  129.  
  130.     if (showVals) {
  131.       textSize(stWg*3/5);
  132.       text(val+1, H-stWg/2, (height+stWg/3)/2);
  133.     }
  134.   }
  135.   void swap(Line drug) {
  136.     int temp = drug.pos;
  137.     drug.pos = pos;
  138.     pos = temp;
  139.     display();
  140.     drug.display();
  141.   }
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement