• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Sorting Array of objects using bubble sort algorithm

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello to everybody,

I'm working on my project about managing Authors and OnlineCourses, this time I'm trying to sort alphabetically the list of authors. Here's my code for the sorting method and for the listing one:



When I execute my code I get this output:



By the exception and the return of the program it seems that the Bubble Sort is working but I'm probably doing something wrong with indices. Can someone lend me a hand?

Thank you
 
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Jordi,

I have not checked your code yet, but could there be some nulls in your Author[] array?
 
Jordi Sedo
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Piet,
That’s correct, it’s not mandatory to fill the array
 
Jordi Sedo
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey again,
I think I solved the problem, I changed the method from static to normal, and I used the variable I was using to counting the number of authors for each OnlineCourse, now I get my output as expected:



Output:
 
Piet Souris
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well done!

But if you are working with an array that may or may not be fully filled, why not use a List (an ArrayList or so)? That would be much simpler.
 
Jordi Sedo
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For this project I was forced to use a simple array for authors.
 
Marshal
Posts: 79239
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am not convinced you have found the best solution. I think those methods should all be static because they need no information other than their parameters, and they emit no information other than their returned values.
I think yoiu should have a utility class with a method that swaps two elements in an array.I also think yoiu should have a bubbleSort method in the same utility class:-Use the sort method to do the sorting, and pass on swapping to the swap method.

Read about <T> here, and about Comparable and Comparator here, both in the Java™ Tutorials. There are several ways you can create a Comparator<Author> including alphabetical order of name. I think the tutorials link will explain everything you need to know.
 
Piet Souris
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is an assignment, concerning possibly partly filled arrays. This is not common, so putting it in a utility class seems not obvious. But why isn't the variable 'nbrAuthors' a parameter in this case?
 
Jordi Sedo
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry Piet, the nbrAuthors it’s a variable I have defined in the OnlineCourse, and initialized it at the constructor. So I had to stop using the static modifier for my sorting method so I could use nbrAuthors as the limit of my for
 
Jordi Sedo
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:I am not convinced you have found the best solution. . . . .
Read about <T> here, and about Comparable and Comparator here, both in the Java™ Tutorials. There are several ways you can create a Comparator<Author> including alphabetical order of name. I think the tutorials link will explain everything you need to know.



I’ll give a look to these tutorials today! Thank you!
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piet Souris wrote:This is an assignment, concerning possibly partly filled arrays. This is not common . . .

That means there are going to be nulls in the array somewhere, and that is impossible to sort using conventional methods as taught atColleges.

putting it in a utility class seems not obvious. But why isn't the variable 'nbrAuthors' a parameter in this case?

You can still use utility methods to sort arrays containing nulls. You can't use Comparable obects, however, because you cannot compare null references like that. You can however create a Comparator<Author> that copes well with nulls. I can't remember whether the tutorials link I gave tells you anything about nulls, nor whether the static methods in Comparable<T> that cope with nulls are permissible, but you can write a Comparator like this:-I am presuming that OP doesn't know how to turn that anonymous class into a λ. If both  arguments are null, that Comparator will cause a redundant swap of the two nulls. You can sort that out by inserting a1 == a2 ? 0 : after return.
 
Piet Souris
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jordi Sedo wrote:Sorry Piet, the nbrAuthors it’s a variable I have defined in the OnlineCourse, and initialized it at the constructor. So I had to stop using the static modifier for my sorting method so I could use nbrAuthors as the limit of my for


I understand, but the Author array is a parameter to the method ('private void sortStringBubble(Author b[]) {...'), so can be any Author array (that is why Campbell said this method could be static), and in the body you are using a variable that is specific to a certain array. That is incorrect. So, either make this nbrAuthors a parameter too, or in the body of the method, check from what index the nulls start.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic