I want a code for array barchart sorted using selection sort algorithm in acceding order and the bar

I want a code for array bar-chart sorted using selection sort algorithm in acceding order and the bar animation for swaping arrays

Tagged:

Answers

  • edited June 2017

    I use this code but i dont know how to sort it with selection sort algorithm and how to make animation for the bar-chart swapping ?

    int distX = 100;
    int distY = 250;
    int a = 21;
    int b = 12;
    int c = 88;
    int d = 30;
    int e = 40;
    int f = 50;
    int g = 10;
    int h = 75;
    int i = 5;
    int j = 19;
    
    void setup(){
      size(390,350);
    }
    
    void draw (){
      background(0);
      stroke (255,0,0);
      line(distX+10,252,distX+10+10*10,252);
      stroke(0);
      fill(255);
      rect(10+distX, distY, 10,- a);
      rect(20+distX, distY, 10,- b);
      rect(30+distX, distY, 10,- c);
      rect(40+distX, distY, 10,- d);
      rect(50+distX, distY, 10,- e);
      rect(60+distX, distY, 10,- f);
      rect(70+distX, distY, 10,- g);
      rect(80+distX, distY, 10,- h);
      rect(90+distX, distY, 10,- i);
      rect(100+distX, distY, 10,- j);
    
    }
    
  • int a = 21;
    int b = 12;
    int c = 88;
    int d = 30;
    int e = 40;
    int f = 50;
    int g = 10;
    int h = 75;
    int i = 5;
    int j = 19;
    

    this is a terrible start. change this to use an array.

  • edited June 2017

    I used this for a random array but i want this code in rectangle not line and as column form

    int[] array = new int[20]; void setup() { size(400, 450); for (int i=0; i<array.length; i++) { array[i] = (int)random(50); } frameRate(4); } int i=0; void draw() { background(#102439); int index_of_min = i; for (int j=i; j<array.length; j++) { if (array[index_of_min]>array[j]) { index_of_min = j; } } int temp = array[i]; array[i] = array[index_of_min]; array[index_of_min] = temp;

    for (int c=0; c<array.length; c++) { fill(-1); text(array[c], 50, 30+20c); stroke(#68BF4B); strokeWeight(2); line(90, 25+20c, 90+5array[c], 25+20c); } i++;
    if (i>array.length-1) noLoop(); }

  • edit post, highlight code, press ctrl-o to format.

  • For a recent related discussion of sorting algorithms and bar charts, see:

Sign In or Register to comment.