• 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 an array

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 95
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.....
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
shashi booshan
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks alot guys.. i got it.tc
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 176
Mac Chrome Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
I found some pretty shells, some sea glass and this lovely tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic