Issue with a sorting program

Hi!
I started created a sorting algorithm, that inserts the highest value on first place (and swaps them)

code
ArrayList<Float> values = new ArrayList<Float>();
int n = 600, min = 0, max = 600, it = 0;
void setup() {
  size(600,600);
  for(int i = 0; i < n; i++) values.add(random(min,max));
}
void draw() {
  background(0);
  stroke(255);
  for(int i = 0; i < n; i++) {
    line(i,height,i,height-values.get(i));
  }
}
void keyPressed() { for(int i = 0; i < n; i++) sortArray(); }
void sortArray() {
  int mx = it;
  for(int i = it; i < n; i++) {
    if(mx < n && values.get(i) > values.get(mx)) {
      swap(mx,i);
      mx = i;
    }
  }
  it++;
}

void swap(int a, int b) {
  float temp = values.get(a);
  values.set(a,values.get(b));
  values.set(b,temp);
}

what did I do wrong?

Can I have more details? I don’t have an idea of what to help you with.

you can enlarge his code with the arrow

the graphic shows that the sorting is not complete

that’s his issue

What happens when you press key like 6 times?

for example this works



ArrayList<Float> values = new ArrayList<Float>();
int n = 600, min = 0, max = 600, it = 0;

void setup() {
  size(600, 600);
  for (int i = 0; i < n; i++) 
    values.add(random(min, max));
}

void draw() {
  background(0);
  stroke(255);
  for (int i = 0; i < n; i++) {
    line(i, height, i, height-values.get(i));
  }
}

void keyPressed() {
  for (int i2 = 0; i2 < 7; i2++) {
    it=0; 
    for (int i = 0; i < n; i++)
      sortArray();
  }
}

void sortArray() {
  int mx = it;
  for (int i = it; i < n; i++) {
    if (mx < n && values.get(i) > values.get(mx)) {
      swap(mx, i);
      mx = i;
    }
  }
  it++;
}

void swap(int a, int b) {
  float tempA = values.get(a);
  float tempB = values.get(b);

  values.set(a, tempB);
  values.set(b, tempA);
}

maybe you have to repeat not 7 but until there is no swapping occured

Hey @CodeMasterX, maybe you can use the sort() method in processing.
This is an implementation:

void setup() {
int[] array  = new int[10];
for(int i = 0; i < 100; i+= 10) {
  array[i] = i+((int)random(1000);
}
 printArray(array);
 array = sort(array);
 printArray(array); 
}

similar:

import java.util.Collections;  // Import the Collections class

ArrayList<Float> list = new ArrayList();

// --------------------------------------------------------------------------------

list.add (17.1); 
list.add (7.7);
list.add (12.9);

// -----------------------------------------------
// Use 

println(list);  

Collections.sort(list); 

println(list);

The whole purpose of the program is to create a sorting algorithm : P

I found a problem : P

ArrayList<Float> values = new ArrayList<Float>();
int n = 600, min = 0, max = 600, it = 0;
void setup() {
  size(600,600);
  for(int i = 0; i < n; i++) values.add((float)i);//random(min,max));
}
void draw() {
  background(0);
  stroke(255);
  for(int i = 0; i < n; i++) {
    line(i,height,i,height-values.get(i));
  }
}
void keyPressed() { while( sortArray()); }
boolean sortArray() {
  int mx = it;
  boolean aaa = true;
  for(int i = 0; i < n; i++) {
    if(mx < n && i > mx && values.get(i) > values.get(mx)) {
      swap(i,mx);
      mx = i;
      aaa = false;
    }
  }
  it++;
  return !aaa;
}

void swap(int a, int b) {
  float temp = values.get(a);
  values.set(a,values.get(b));
  values.set(b,temp);
}

it returns this:

1 Like