Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

sort by ascending order

 
Supun Lakshan Dissanayake
Ranch Hand
Posts: 135
Android Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49789
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Android Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks guys!!!
 
Chamara Madhushan
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is there any way to get this using one for loop

 
Phil English
Ranch Hand
Posts: 62
MySQL Database Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 1073
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic