Help people hows it going?

I need some help with a bubble sort algorithm, i basically have to use a bubble sort to look through a number of car registration numbers and put them into ascending order...

Im just not quite sure how to get it done... I asked a friend and he gave me this:

public int sortByRegNo()
    {
        for (int k = list.size(); k > 0; k--)
        {
            for (int l = 0; l < k-1; l++)
            {
                if(list.get(l).compareTo(list.get(1 + 1) < 0)
                {
                    swop(l, l+1);
                }
            }
        }
    }

He said that id need a swop method and a compareTo method but im not quite sure how to do them can any1 help please?

I had a go at the compare To method:

public int compareTo(Car car) {
       return regNo.compareTo(car.getRegNo());
 }

But im not to good at Java so not sure if I did it correct or not, obviously didn't as it don't work lol.

I had a look around the site to something similar to what i need and found this:

http://www.daniweb.com/forums/thread9367.html#

Check around link 150.

Well i really hope some1 can help :)

Thanks

Recommended Answers

All 4 Replies

Without rest of the code we have no chance of determinate if you did it right or wrong.

PS: Saying you are not good with Java will not win you any sympathy...

Help people hows it going?

I need some help with a bubble sort algorithm, i basically have to use a bubble sort to look through a number of car registration numbers and put them into ascending order...

Im just not quite sure how to get it done... I asked a friend and he gave me this:

public int sortByRegNo()
    {
        for (int k = list.size(); k > 0; k--)
        {
            for (int l = 0; l < k-1; l++)
            {
                if(list.get(l).compareTo(list.get(1 + 1) < 0)
                {
                    swop(l, l+1);
                }
            }
        }
    }

He said that id need a swop method and a compareTo method but im not quite sure how to do them can any1 help please?

I had a go at the compare To method:

public int compareTo(Car car) {
       return regNo.compareTo(car.getRegNo());
 }

But im not to good at Java so not sure if I did it correct or not, obviously didn't as it don't work lol.

I had a look around the site to something similar to what i need and found this:

http://www.daniweb.com/forums/thread9367.html#

Check around link 150.

Well i really hope some1 can help :)

Thanks

If regNo is a primitive type with >, < == operators (i.e. int), don't use compareTo. Use <, > or ==. Without knowing regNo's type, it's impossible to say what else needs to be done. Post the Car class. And yes, you'll need to write a swap function.

Just to make note of again, the 1st method in my post is my mates (Eric) and not mine. Just in case teachers think he copied it from the net, this is just to verify he hasn't and it's his own personal method.

Thank you.

I assume "swop" means swap? Then you just have to swap the position of those two values.

For compareTo, I would return whether say, regNo is greater than, less than, or equal to currentRegNo.

HTH.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.