Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I would use the following function to sort them</p> <pre><code>class Kakira { static int preferred(int a, int b) { if(a == b) return a; // doesn't matter which String sa = a+""; String sb = b+""; for(int i = 0; i &lt; sa.length() &amp;&amp; i &lt; sb.length(); i++) { char ca = sa.charAt(i); char cb = sb.charAt(i); if(ca &lt; cb) return b; if(ca &gt; cb) return a; } // we reached here - the larger one must start with the smaller one // so, remove the small one from the start of the small one, and // that will tell us which is most appropriate. if(a &lt; b) { String choppedB = sb.substring(sa.length()); if(preferred(Integer.parseInt(choppedB),a) == a) return a; else return b; } else { String choppedA = sa.substring(sb.length()); if(preferred(Integer.parseInt(choppedA),b) == b) return b; else return a; } } // using a very simple sort because I'm being lazy right now public static void sort(int[] data) { while(!isSorted(data)) { for(int i = 0; i &lt; data.length - 1; i++) { int a = data[i]; int b = data[i+1]; int p = preferred(a,b); if(p == b) { data[i] = b; data[i+1] = a; } } } } public static boolean isSorted(int[] data) { for(int i = 0; i &lt; data.length - 1; i++) { int a = data[i]; int b = data[i+1]; int p = preferred(a,b); if(p != a) return false; } return true; } public static void main(String[] args) { int[] data = new int[]{9,1,95,17,5}; sort(data); for(int i : data) System.out.print(i); System.out.println(""); } } </code></pre> <p>For the humans: First: I'm going to sort them using a special comparison function. This will put them in the most desired order, so we can just print them out.</p> <p>I'm not worrying about the sort algorithm, because once you have the comparison you can use whatever sorting you want. The comparison is the important part.</p> <p>So let's walk through the decision process.</p> <p>First, if the two numbers are equal, no one cares which one it is. So we just return it.</p> <p>Next, we get the string representation of the numbers so we can start comparing digits. We walk down the digits until we run out of digits on one of the strings. Once they are no longer equal, we want the one with the larger value, because we want high digits early (I think it's obvious why).</p> <p>If we get to the end of one of the numbers and we don't yet have a winner, we know the longer one must start with the short one. They CAN'T be of equal length, because if the first digits are equal, and they're equal length, then the numbers themselves are equal, and they would have been caught earlier in the process. So the question is, which one goes first? Well, consider the ramifications:</p> <pre><code>12345 first: 12345123 123 first: 12312345 </code></pre> <p>Obviously we want the first one. But how to know this? We take the 123 away from the long one. So we have 45 and 123.</p> <p>Now, run these through the algorithm again, determine which was the winner, and whichever won the second round also wins the first round. If it were to go deeper, say (12312312 and 123) then it would continue to chain down.</p> <p>Make sense?</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.
 

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