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

Sorting an array

shashi booshan
Greenhorn

Joined: Jun 13, 2008
Posts: 14
Hi guys can any one just tel me how to sort an array in ascending order? i worked a lot but dint get.
Here is my code :



Thanks

regards
shashi


Thanks & Regards
- Shashi
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11480
    
  16

What does this code do or not do? What are you expecting it to do?

and how can you tell if this array is sorted or not? Just by eyeballing it, it would appear you have an array of 5 ints, but since you never set any of the elements, they're all 0.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19762
    
  20

I doubt you can do it with just one single loop.
Consider an array {4, 7, 3}. After not-swapping 4 and 7, the index moves one forward and you swap 7 and 3. The array is then {4, 3, 7}. Doesn't look sorted, does it?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Andre Brito
Ranch Hand

Joined: Dec 13, 2007
Posts: 95

Hi.

As the other boys said, it's hard to implement a sorting algorithm that takes O(n) time. But there are two algorithms that try to do that: Bucket Sort and Radix Sort. If you're not concerned about time complexity, you should try Insertion Sort, Bubble Sort or Quicksort.

Hugs.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18997
    
  40


The code actually looks like it implements one iteration of a bubble sort -- the only guarantee is that the largest item is last. Implement the other iterations, and it will be done...

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Tom Johnson
Ranch Hand

Joined: May 11, 2005
Posts: 142
As this looks like an assignment, once you have sorted ( ) this problem, consider how you would sort, say, a list of integers, without having to write the code to do it from scratch....have a look at the Collections class.....


<a href="http://faq.javaranch.com/java/UseCodeTags" target="_blank" rel="nofollow">Use Code Tags!!</a>
Omar Nasir
Greenhorn

Joined: Jul 11, 2005
Posts: 5
Rob Prime wrote:I doubt you can do it with just one single loop.
Consider an array {4, 7, 3}. After not-swapping 4 and 7, the index moves one forward and you swap 7 and 3. The array is then {4, 3, 7}. Doesn't look sorted, does it?


hi guys, im new here and new to java. based on what rob said, i think you should add 1 more loop to check the reverse way.

eg : you got {4,7,3}

currently it will sort to {4,3,7}.

then you check from reverse,

i think you will get

{3,4,7}

because 7 and 3 will not swap, but 3 and 4 will.

then add 1 more loop to check both sides of array[n], to make sure that its in the right spot.



im still new, but this is my input.


javaNoob me = new javaNoob();<br />String i = "i";<br />i += "love java!";
shashi booshan
Greenhorn

Joined: Jun 13, 2008
Posts: 14
Thanks alot guys.. i got it.tc
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39869
    
  28
shashi booshan wrote:Thanks alot guys.. i got it.tc
Please tell us all how you solved it. You do realise there are many different algorithms; you can find them by googling.
Olivier Legat
Ranch Hand

Joined: Nov 17, 2007
Posts: 176

I simply don't see what's the point of a boolean "swapped". Since it's not declared in the method I'm assuming it's a private/public field of the class. If so, why are you declaring outside the method?


Olly
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Sorting an array