HiveBrain v1.2.0
Get Started
← Back to all entries
snippetjavaMinor

Bucket Sort with Linked List

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
withbucketsortlistlinked

Problem

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> buckets = new LinkedList<>(); //create buckets
    for (int i = 0; i ()); //fill bucketlist with buckets
    }

    for (int i = 0; i  studentLinkedList = buckets.get(i);
        Student[] s = studentLinkedList.toArray(new Student[studentLinkedList.size()]);
        insertionSortByClass(s);
        studentLinkedList = new LinkedList<>(Arrays.asList(s));
        buckets.set(i, studentLinkedList);
    }

    LinkedList result = new LinkedList<>();
    for (LinkedList bucket : buckets) {
        result.addAll(bucket);
    }

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


As requested:

public static void insertionSortByClass(Student[] student) {
    Student temp;
    for (int i = 1; i  0; j--) {
            if (student[j].compareTo(student[j - 1]) < 0) {
                temp = student[j];
                student[j] = student[j - 1];
                student[j - 1] = temp;
            }
        }
    }
}

Solution

This code seems to be a specific BucketSort made for a specific program. I would instead create a more generic BucketSort:

public static > void bucketSort(E[] array) {
    // ...
}

public static  void bucketSort(E[] array, Comparator 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 > void bucketSort(E[] array, E[] separators) {

}

public static  void bucketSort(E[] array, E[] separators, Comparator 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 > List bucketSort(E[] array,
            E[] separators) {
        Comparator comparator = new Comparator() {

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

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

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

    private static  int bucketNumber(E element, E[] separators, int low,
            int high, Comparator 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  {

    private List elements;

    public Bucket() {
        this.elements = new LinkedList();
    }

    public Bucket(Collection elements) {
        this.elements = new LinkedList(elements);
    }

    public List 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] ]

Code Snippets

public static <E extends Comparable<E>> void bucketSort(E[] array) {
    // ...
}

public static <E> void bucketSort(E[] array, Comparator<E> comparator) {
    // ...
}
public static <E extends Comparable<E>> void bucketSort(E[] array, E[] separators) {

}

public static <E> void bucketSort(E[] array, E[] separators, Comparator<E> comparator) {

}
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 <E extends Comparable<E>> List<E> bucketSort(E[] array,
            E[] separators) {
        Comparator<E> comparator = new Comparator<E>() {

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

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

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

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

    private static <E> int bucketNumber(E element, E[] separators, int low,
            int high, Comparator<E> 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 < 0 ? bucketNumber(element, separators, low,
                searchIndex, comparator) : bucketNumber(element, separators,
                searchIndex, high, comparator);
    }

}

class Bucket<E> {

    private List<E> elements;

    public Bucket() {
        this.elements = new LinkedList<E>();
    }

    public Bucket(Collection<E> elements) {
        this.elements = new LinkedList<E>(elements);
    }

    public List<E> getItems() {
        return elements;
    }

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

Context

StackExchange Code Review Q#107222, answer score: 5

Revisions (0)

No revisions yet.