# Having trouble trying to implement bucket sort algorithm

**URL:** <https://discourse.processing.org/t/having-trouble-trying-to-implement-bucket-sort-algorithm/33066>\
**Category:** Coding Questions\
**Created:** [October 25, 2021, 7:17pm UTC](https://discourse.processing.org/t/having-trouble-trying-to-implement-bucket-sort-algorithm/33066 "2021-10-25T19:17:14Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![systemCollapse](https://avatars.discourse-cdn.com/v4/letter/s/85f322/32.png) [@systemCollapse](https://discourse.processing.org/u/systemCollapse)\
**Post date:** [October 25, 2021, 7:17pm UTC](https://discourse.processing.org/t/having-trouble-trying-to-implement-bucket-sort-algorithm/33066/1 "2021-10-25T19:17:14Z")

</div>

Im trying to bucket sort an array and represent the sorting visually with processing functions.  
Found this bucket sort code to be rather common used in Java:

```auto
import java.util.*;
import java.util.Collections;
float[] arr = {23, 4, 67, 1, 90, 54, 21, 50};
int pos1 = -1, pos2 = -1;

void setup()
{
   size(600,600);
   background(255);
   thread("Execute");
}
void Execute()
{
  delay(1000);
  BucketSort(arr, arr.length);
}

void draw()
{
   background(255);
   int N = arr.length;
   float base = width/N;
   
   for(int i = 0; i < N; i++)
   {
     if(i >= pos1 && i <= pos2) fill(0, 0, 255);
     else fill(0, 255, 0);
     
     float h = arr[i] * 5;
     rect(i*base, height - h, base, h);
   }
}
void BucketSort(float arr[], int n)
    {
        if (n <= 0) return;
        Vector<Float>[] buckets = new Vector[n];
 
        for (int i = 0; i < n; i++) {
            buckets[i] = new Vector<Float>();
        }
 
        for (int i = 0; i < n; i++) {
            float idx = arr[i] * n;
            buckets[(int)idx].add(arr[i]);
        }
 
        for (int i = 0; i < n; i++) {
            Collections.sort(buckets[i]);
        }
 
        int index = 0;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < buckets[i].size(); j++) {
                arr[index++] = buckets[i].get(j);
            }
        }
    }

```

but its giving away those errors:

```auto
java.lang.ArrayIndexOutOfBoundsException: 184
	at aula8.BucketSort(aula8.java:63)
	at aula8.Execute(aula8.java:34)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at processing.core.PApplet.method(PApplet.java:3867)
	at processing.core.PApplet$2.run(PApplet.java:3905)

```

can someone explain it to me? im kinda noob in java and processing, it looks like the code is trying to access a number in the array that is out of its bounds, \<0 or \>8, but I dont understand where the code is trying it.

---

<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:** [October 25, 2021, 7:28pm UTC](https://discourse.processing.org/t/having-trouble-trying-to-implement-bucket-sort-algorithm/33066/2 "2021-10-25T19:28:32Z")

</div>

> [@systemCollapse](#):
>
> where the code is trying it.

Normally the line is highlighted that produces the error

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [October 25, 2021, 7:34pm UTC](https://discourse.processing.org/t/having-trouble-trying-to-implement-bucket-sort-algorithm/33066/3 "2021-10-25T19:34:51Z")

</div>

Hello,

Processing 4 on W10 PC highlighted where the error is in blue.

I added a `println()` to illustrate the problem:

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

184 is clearly out of bounds of the 8 element array.

`:)`
