• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

Problem with sorting an integer array

 
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.

 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • 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 Windows XP Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
google for bubble sort algorithm,basic algorithm.
 
Rancher
Posts: 3414
33
  • 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.
 
Marshal
Posts: 65425
248
  • 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.
 
Ranch Hand
Posts: 1179
Mac OS X Eclipse IDE
  • 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
Marshal
Posts: 65425
248
  • 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
Mac OS X Eclipse IDE
  • 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
Marshal
Posts: 65425
248
  • 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
 
Ranch Hand
Posts: 492
Firefox Browser VI Editor Linux
  • 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
Marshal
Posts: 65425
248
  • 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
Mac OS X Eclipse IDE
  • 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
Marshal
Posts: 65425
248
  • 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 VI Editor Linux
  • 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
Marshal
Posts: 65425
248
  • 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.
 
Eliminate 95% of the weeds in your lawn by mowing 3 inches or higher. Then plant tiny ads:
Enterprise-grade Excel API for Java
https://products.aspose.com/cells/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!