wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes sort by ascending order Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "sort by ascending order" Watch "sort by ascending order" New topic
Author

sort by ascending order

Supun Lakshan Dissanayake
Ranch Hand

Joined: Oct 26, 2012
Posts: 132

Question 1 : I need to sort values of an array by ascending order. I've tried following code to do it and it works fine for array which contains only 3 elements. But if the length of array is greater than 3 how do i sort values by ascending order between the minimum and the maximum.

Question 2 : Is there a better way to sort 3 numbers by ascending order than this?



Thanks!


Are you better than me? Then please show me my mistakes..
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
By making i < 3 the condition of your loops, you have hard-coded 3 as the number of iterations. This means that whenever the length of the array changes, you have to go change every loop. Better is to write i < x.length. That way no matter how long the array is, the loop will iterate through the entire array.

Is there a better way? Yes, there are quite a few sorting algorithms which are better. But to fully understand why those algorithms are better, and why they were developed in the first place, it's good to go through the pain of sorting inefficiently. The classic sorting algorithm, the one which I was first taught, and I suspect the majority of computer science students were, is the bubble sort. It's sub-optimal, but easy to grasp, so it's a good introduction to sorting.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38107
    
  22
Yes, there is a better way. You could do with sorting out your code style a little, however. Your code would be a lot easier to read with some spacing, for example around the < and > operators.
If you are certain there are going to be three values, you can use nested ? : statements. As DD implies, that constrains you to sorting 3‑element arrays only.
He has also told you that there are lots of different algorithms. You should search for them. Find an algorithms book. You will find that although everybody curses bubble sort, it is not at all slow for small arrays like that. There is far too much information about sorting for us to tell you here.
Supun Lakshan Dissanayake
Ranch Hand

Joined: Oct 26, 2012
Posts: 132

thanks guys!!!
Chamara Madhushan
Greenhorn

Joined: Nov 17, 2012
Posts: 4
is there any way to get this using one for loop

Phil English
Ranch Hand

Joined: Jun 18, 2012
Posts: 62

Supun Lakshan Dissanayake wrote:Question 1 : I need to sort values of an array by ascending order. I've tried following code to do it and it works fine for array which contains only 3 elements. But if the length of array is greater than 3 how do i sort values by ascending order between the minimum and the maximum.


Can you not put the values into a List and use the Collections.sort() method (this is a mergesort which is on average more efficient than a bubble sort)? Or is designing the sort algorithm core to your goal?
Ryan McGuire
Ranch Hand

Joined: Feb 18, 2005
Posts: 1006
    
    3
Chamara Madhushan wrote:is there any way to get this using one for loop


There is a way to do anything with a single loop.* In the case of a bubble sort, it might not be easy to read and the execution time will still be O(n*n), but it will be technically one loop.

Just figure out what the values for the two index variables, i and j, are at the start of each iteration of the code inside the inner loop. Make just a single for() loop that has complicated initialization, condition and increment expressions to generate the same series of values for those two variables.

Specifically, the values of i and j just before you execute the inner if() are (i,j) = (0,0) (0,1) (0,2) (1,0) (1,1) (1,2) (2,0) (2,1) (2,2). Can you figure out how to make a single for loop that will set i and j to those nine pairs of values?

* Arguably, everything executed on a single-threaded single-processor machine already is done in a single-loop:
 
 
subject: sort by ascending order
 
Similar Threads
Descending order and associating Array Elements in sort method to another's elements
Cannot find symbol error in switch-case
Sorting a row in a two deminsional array
Help with writing to a file!