views:

94

answers:

3

I am asked to write my own version of Bucket Sort for Sorting an array of objects. My plan is to write a simple Entry class and another class with a main method and try to manipulate an array of lists. This is what I have so far, I just need some help putting it all together because I'm not very experienced with coding but I know what I am supposed to do. What would it look like sorting just an array of integers, that way I might have better understanding.

My Entry Class (Class Node):

public class Node {

    protected int element;
    protected Node next;

    public Node()
    {
        element = 0;
        next = null;
    }
    public Node getNext(Node n)
    {
        return next;
    }
    public void setNext(Node n)
    {
        n = next;
    }
    public void setElement(int e)
    {
        e = element;
    }
    public int getElement()
    {
        return element;
    }
    public void insert(int e)
    {
        e = element;

    }
}

My Bucket Sort Class:

public class BucketSort extends Node {

    public void remove(int[] x)
    {
        x = null;
    }
    public static void bucketSort(int[] a)
    {
        int[] array = a;
        Node[] buckets = new Node[array.length];

        for (int i=0; i<array.length; i++)
        {
            buckets[i] = null; 
        } 
        for (int i=0; i<array.length; i++)
        {
            array.remove(array[i]);
            buckets[i].insert(array[i]);
        }


    }

}

I do get an error at array.remove(array[i]); as well.

A: 

A few things. When you declare the Node[] buckets its already empty there is no need to null it out. Also BucketSort should not Extend Node, It should contain a collection of Node objects (Array for instance). Your remove method should not accept an int[] but merely an int that will point to the index that is to be removed.

Woot4Moo
A: 

Here are my quick observations of the design:

  1. Have two different classes: Node which just represents the single node (element and next) and another class LinkedList which contains the head of the list. Now you can support methods like add, remove,search on the LinkedList class.

  2. BucketSort extending Node isn't a good class design. You probably want to keep them independent and make BucketSort generic enough to sort on any data type. Keep the Sorting mechanism in a separate class and pass the LinkedList to it.

  3. Why are element and next protected? You can change them to Private.

  4. Since you are designing a LinkedList API, it would be better and cleaner to stick to the standard methods LinkedList supports.

Ravi Gummadi
A: 

ok, two errors:

1 - array.remove(array[i]); ??? what is this? array does not have a method remove and you dont want do remove the item from array, do you? I think you can do just remove this line.

2 - If you create a bucket for each element in a array how this is a Bucket Sort? You should create less buckets than elements (grouping they in some way) right?

Plínio Pantaleão