# Issue with a sorting program

**URL:** https://discourse.processing.org/t/issue-with-a-sorting-program/27163
**Category:** Beginners
**Created:** [January 18, 2021, 4:45pm UTC](https://discourse.processing.org/t/issue-with-a-sorting-program/27163 "2021-01-18T16:45:22Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)
#### Post date: [January 18, 2021, 4:45pm UTC](https://discourse.processing.org/t/issue-with-a-sorting-program/27163/1 "2021-01-18T16:45:22Z")

</div>

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

> **code**
>
> ```auto
> 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);
> }
> 
> ```

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/1/180e0afab3cc2febe494f43e24abfb05d275dcf0.png)

what did I do wrong?

---

<div class="post-metadata">

### Author: ![SNICKRS](https://avatars.discourse-cdn.com/v4/letter/s/43a26b/32.png) [@SNICKRS](https://discourse.processing.org/u/SNICKRS)
#### Post date: [January 18, 2021, 5:34pm UTC](https://discourse.processing.org/t/issue-with-a-sorting-program/27163/2 "2021-01-18T17:34:45Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [January 18, 2021, 5:36pm UTC](https://discourse.processing.org/t/issue-with-a-sorting-program/27163/3 "2021-01-18T17:36:11Z")

</div>

you can enlarge his code with the arrow

the graphic shows that the sorting is not complete

that’s his issue

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [January 18, 2021, 5:36pm UTC](https://discourse.processing.org/t/issue-with-a-sorting-program/27163/4 "2021-01-18T17:36:59Z")

</div>

> [@CodeMasterX](#):
>
> `for(int i = 0; i < n; i++)`

What happens when you press key like 6 times?

for example this works

```auto

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

---

<div class="post-metadata">

### Author: ![SNICKRS](https://avatars.discourse-cdn.com/v4/letter/s/43a26b/32.png) [@SNICKRS](https://discourse.processing.org/u/SNICKRS)
#### Post date: [January 18, 2021, 5:46pm UTC](https://discourse.processing.org/t/issue-with-a-sorting-program/27163/5 "2021-01-18T17:46:08Z")

</div>

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

```auto
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); 
}

```

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [January 18, 2021, 5:52pm UTC](https://discourse.processing.org/t/issue-with-a-sorting-program/27163/6 "2021-01-18T17:52:04Z")

</div>

similar:

```auto
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);

```

---

<div class="post-metadata">

### Author: ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)
#### Post date: [January 18, 2021, 6:19pm UTC](https://discourse.processing.org/t/issue-with-a-sorting-program/27163/7 "2021-01-18T18:19:56Z")

</div>

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

---

<div class="post-metadata">

### Author: ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)
#### Post date: [January 18, 2021, 6:28pm UTC](https://discourse.processing.org/t/issue-with-a-sorting-program/27163/8 "2021-01-18T18:28:50Z")

</div>

I found a problem : P

```auto
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:

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/8/8dc1e710d8393ca78193542effa25e4a75d58a83.png)
