Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem with sorting an integer array

 
Nelson Sam
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to sort an array.Below is the code.But I cant get it working properly.

 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm. sounds like you are trying Bubble sort[you need to improve your for loop]. why cant you use Arrays.sort ?
 
Nelson Sam
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Seetharaman Venkatasamy

I know using sort() method.
But I want to sort programatically.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
google for bubble sort algorithm,basic algorithm.
 
Norm Radder
Ranch Hand
Posts: 733
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I cant get it working properly

Can you show the output and explain?

A suggestion: For ease of testing use a hardcoded array of ints vs prompting the user.

int[] anArray = new int[] {3,4,2,6};

It'll make the debugging easier.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I always thought bubble sort/sinking sort needs two for loops, inside each other.
 
Rene Larsen
Ranch Hand
Posts: 1179
Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:I always thought bubble sort/sinking sort needs two for loops, inside each other.

You are correct - they do! (if the code should be readable)

I think it is possible to do with only one for loop - but I don't want to read the code afterward.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A bubble sort with only one loop? That sounds interesting. I never knew that was possible.
 
Rene Larsen
Ranch Hand
Posts: 1179
Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I haven't tried to coded a sort with just one for loop - but with some "spaghetti code", it should be possible (to do a bubble sort with recursive calls are also possible - with only one for loop, but it wasn't what I was thinking about).

But then you are right - it will no longer be a bubble sort, if it is done with "spaghetti code"
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You mean a spaghetti sort? That sounds like a new algorithm, which ought to be published
 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Would that spaghetti code actually improve the performance of the sort? Or would the one-loop bubble sort still be O(n**2)?

Hunter
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So much better if it runs in O(n^3) time. Then you can say it is something really new and get a better publication
 
Rene Larsen
Ranch Hand
Posts: 1179
Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hunter McMillen wrote:Would that spaghetti code actually improve the performance of the sort? Or would the one-loop bubble sort still be O(n**2)?

Hunter

If you look in the java.util.Array.java source file - http://kickjava.com/src/java/util/Arrays.java.htm - you will see that there is not only one implementation of a sort - it depends on the size of the array to sort.

A small array where the size is < 7 - is a bubble sort.

 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, even though bubble sort runs in quadratic time, it is faster than merge sort (which is used for larger arrays) for sorting very small arrays.
 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My question was if you managed to implement bubble sort with one for loop wouldn't that make it O(n) instead of O(n**2) ? So shouldn't it be more effective at sorting.


Hunter
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If it really is bubble sort still, it will surely still run in quadratic time. I should be very interested to see how it is implemented with a single loop.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic