aspose file tools*
The moose likes Beginning Java and the fly likes Problem with sorting an integer array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Problem with sorting an integer array" Watch "Problem with sorting an integer array" New topic
Author

Problem with sorting an integer array

Nelson Sam
Ranch Hand

Joined: Jun 12, 2010
Posts: 30
I am trying to sort an array.Below is the code.But I cant get it working properly.

Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

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

Joined: Jun 12, 2010
Posts: 30
Hi Seetharaman Venkatasamy

I know using sort() method.
But I want to sort programatically.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

google for bubble sort algorithm,basic algorithm.
Norm Radder
Ranch Hand

Joined: Aug 10, 2005
Posts: 690
    
    1
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

Joined: Oct 13, 2005
Posts: 40052
    
  28
I always thought bubble sort/sinking sort needs two for loops, inside each other.
Rene Larsen
Ranch Hand

Joined: Oct 12, 2001
Posts: 1179

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.


Regards, Rene Larsen
Dropbox Invite
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
A bubble sort with only one loop? That sounds interesting. I never knew that was possible.
Rene Larsen
Ranch Hand

Joined: Oct 12, 2001
Posts: 1179

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

Joined: Oct 13, 2005
Posts: 40052
    
  28
You mean a spaghetti sort? That sounds like a new algorithm, which ought to be published
Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

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 the facts don't fit the theory, get new facts" --Albert Einstein
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
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

Joined: Oct 12, 2001
Posts: 1179

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

Joined: Oct 13, 2005
Posts: 40052
    
  28
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

Joined: Mar 13, 2009
Posts: 492

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

Joined: Oct 13, 2005
Posts: 40052
    
  28
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem with sorting an integer array