Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You need to make sure the data types match, as mentioned before your BubbleSort class operates only on an int array, this is the code as modified to work with double arrays (which is what diff contains). </p> <p>When you changed dif to an int and received the "loss of precision" message, it's because you're doing division (lines 43-47 in GolfScores.java) and then assigning the result to an int (which discards fractional values). </p> <p>Here is a version of BubbleSort that compiles without errors or warnings. Changes I made (there are 3 of them) are noted as comments:</p> <pre><code>public class BubbleSort { public static void sort(double[] a, int numberUsed) //change here { int index; for ( int i =0; i&lt;numberUsed; i++) { for (index = 0; index&lt;a.length - 1; index++) { if (a[index]&gt; a[index + 1]) { interchange(index, index + 1, a); } //end of if () } //end of for () } //end of for () } private static void interchange(int i, int j, double[] a) //change here { double temp1; //change here, important temp1 = a[i]; a[i] = a[j]; a[j] = temp1; } } </code></pre> <p>The code could be made more flexible by using objects instead of primitive types (like, sort an array of Object instead of double), and perhaps using a user-specified function to compare elements, this way you wouldn't need to change the class to handle different data types. That said, if you're going to use many more of Java's facilities and standard libraries, you might as well go the Arrays.sort method. <a href="http://www.leepoint.net/notes-java/data/arrays/70sorting.html" rel="nofollow">This page</a> explains use of built-in sort methods (as well as why you shouldn't write your own sort routines <em>unless</em> it's some sort of academic assignments) better than I can.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload