Bucket Sort with Linked List This is my implentation of Bucket Sort, but I see myself using way too many for loops, is this necessary?

  public static void bucketSort(Student[] students) {
    final String[] courses = { "IB", "IG", "IN", "IS", "IT" };
    final int amountOfBuckets = courses.length;
    final int n = students.length;

    if (n == 0) return;

    LinkedList&lt;LinkedList&lt;Student&gt;&gt; buckets = new LinkedList&lt;&gt;(); //create buckets
    for (int i = 0; i &lt; amountOfBuckets; i++) {
        buckets.add(new LinkedList&lt;&gt;()); //fill bucketlist with buckets
    }

    for (int i = 0; i &lt; courses.length; i++) {
        for (Student student : students) {
            if (student.getClassNumber().startsWith(courses[i])) {
                buckets.get(i).add(student);
            }
        }
    }

    for (int i = 0; i &lt; amountOfBuckets; i++) {
        LinkedList&lt;Student&gt; studentLinkedList = buckets.get(i);
        Student[] s = studentLinkedList.toArray(new Student[studentLinkedList.size()]);
        insertionSortByClass(s);
        studentLinkedList = new LinkedList&lt;&gt;(Arrays.asList(s));
        buckets.set(i, studentLinkedList);
    }

    LinkedList&lt;Student&gt; result = new LinkedList&lt;&gt;();
    for (LinkedList&lt;Student&gt; bucket : buckets) {
        result.addAll(bucket);
    }

    for (int i = 0; i &lt; result.size(); i++) {
        students[i] = result.get(i);
    }
}


As requested:

    public static void insertionSortByClass(Student[] student) {
    Student temp;
    for (int i = 1; i &lt; student.length; i++) {
        for (int j = i; j &gt; 0; j--) {
            if (student[j].compareTo(student[j - 1]) &lt; 0) {
                temp = student[j];
                student[j] = student[j - 1];
                student[j - 1] = temp;
            }
        }
    }
}
This code seems to be a specific BucketSort made for a specific program. I would instead create a more generic BucketSort:

public static &lt;E extends Comparable&lt;E&gt;&gt; void bucketSort(E[] array) {
    // ...
}

public static &lt;E&gt; void bucketSort(E[] array, Comparator&lt;E&gt; comparator) {
    // ...
}


Note there is two methods; one is for a class that implements the Comparator interface, and one to allow a Comparator to be passed as the Comparator for the class, where there is no comparator, or if you want to compare it in a different way. 

Then the only problem is that you don't know the bounds for the buckets; the simple way would be to use bounds provided by the calling method:

public static &lt;E extends Comparable&lt;E&gt;&gt; void bucketSort(E[] array, E[] separators) {

}

public static &lt;E&gt; void bucketSort(E[] array, E[] separators, Comparator&lt;E&gt; comparator) {

}


The separators will be the upper limit of the bucket right before it; and the uppermost bucket will catch everything that is larger than the largest bound.

Now for the actual code:

import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;

public class BucketSort {

    public static &lt;E extends Comparable&lt;E&gt;&gt; List&lt;E&gt; bucketSort(E[] array,
            E[] separators) {
        Comparator&lt;E&gt; comparator = new Comparator&lt;E&gt;() {

            @Override
            public int compare(E element1, E element2) {
                return element1.compareTo(element2);
            }

        };
        return bucketSort(array, separators, comparator);
    }

    public static &lt;E&gt; List&lt;E&gt; bucketSort(E[] array, E[] separators,
            Comparator&lt;E&gt; comparator) {
        final int numOfBuckets = separators.length + 1;
        // Create Buckets
        // Used ArrayList because we don't need the functions of a LinkedList
        List&lt;Bucket&lt;E&gt;&gt; buckets = new ArrayList&lt;Bucket&lt;E&gt;&gt;(numOfBuckets);
        for (int i = 0; i &lt; numOfBuckets; i++) {
            buckets.add(new Bucket&lt;E&gt;());
        }
        // Sort into Buckets
        for (E element : array) {
            // Use binary search
            buckets.get(bucketNumber(element, separators, comparator)).getItems().add(element);
        }
        // Sort buckets
        for (Bucket&lt;E&gt; bucket : buckets) {
            bucket.getItems().sort(comparator);
        }
        // Combine Buckets and return
        for (int i = 1; i &lt; numOfBuckets; i++) {
            buckets.get(0).getItems().addAll(buckets.get(i).getItems());
        }
        return buckets.get(0).getItems();
    }

    private static &lt;E&gt; int bucketNumber(E element, E[] separators,
            Comparator&lt;E&gt; comparator) {
        return bucketNumber(element, separators, 0, separators.length,
                comparator);
    }

    private static &lt;E&gt; int bucketNumber(E element, E[] separators, int low,
            int high, Comparator&lt;E&gt; comparator) {
        if (low + 1 == high) {
            return low;
        }
        int searchIndex = (high - low) / 2 + low;
        int compareResult = comparator
                .compare(element, separators[searchIndex - 1]);
        if (compareResult == 0) {
            return searchIndex;
        }
        return compareResult &lt; 0 ? bucketNumber(element, separators, low,
                searchIndex, comparator) : bucketNumber(element, separators,
                searchIndex, high, comparator);
    }

}

class Bucket&lt;E&gt; {

    private List&lt;E&gt; elements;

    public Bucket() {
        this.elements = new LinkedList&lt;E&gt;();
    }

    public Bucket(Collection&lt;E&gt; elements) {
        this.elements = new LinkedList&lt;E&gt;(elements);
    }

    public List&lt;E&gt; getItems() {
        return elements;
    }

}


Yes, I know it looks 10x more complex than it need to be, but this can be used for further applications.

Since you asked for an explanation of the separators array...

The separator's array is there so that the method can know where to separate it into buckets. For example:

separator = [2, 5, 8, 11]
array = [1, 7, 2, 6, 4, 8, 12, 10, 13, 0, 5, 3, 9, 11]


Then the buckets would be:

buckets = [ [0, 1] , [2, 3, 4] , [5, 6, 7] , [8, 9, 10] , [11, 12, 13] ]