sort by ascending order

Supun Lakshan Dissanayake
Ranch Hand
Posts: 135
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!

dennis deems
Ranch Hand
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
Posts: 49361
62
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
Posts: 135
thanks guys!!!

Greenhorn
Posts: 4
is there any way to get this using one for loop

Phil English
Ranch Hand
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
Posts: 1068
4
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: