Best algorithm to sort semi-sorted arrays?

I am trying my hand at the hutter prize (http://prize.hutter1.net/)
My current compression algorithm sorts an array of Comparable Objects every main cycle.
-This array of objects can potentically increase by one object per cycle.
-Each object's comparision value can change per cycle, however its order is mostly kept.
This means that the over all order of the objects are similar between cycles.
Is there an sorting algorithm which can take advantage of the semi-sorted state of the array?
I was using the default Arrays.sort() however it does not seem to make much use of the semi-sorted input array and is too slow for my use.
In order to speed up the sorting i have relaxed the need to be in strict order. I.e there can be some objects out of order.
To this end I have developed a BucketSort which i thought should speed up the sorting however it seems to have the opposite effect!
Is there any reason why this implementation is quite slow? (three times as slow as Array.sort() )
   private static int MAX_BUCKET=65535;
   private static Word[] buckets=new Word[MAX_BUCKET];
   public static void bucketSortReverse (Word[] a, int n)
        int i;
        int count=1;
        int index;
        Word curr;
        // fill up the buckets with the input words
        for (i=0;i<n;i++)
             curr=a;
          index=(int) (curr.probablility*MAX_BUCKET);
          curr.next=buckets[index];
          buckets[index]=curr;
     // iterate over the buckets and construct the sorted words
     for (i=0;i<MAX_BUCKET;i++)
          curr=buckets[i];
          if (curr!=null)
               while (curr!=null)
                    a[n-count++]=curr;
                    curr=curr.next;
               buckets[i]=null;
NOTE Currently my input Word array contains a maximum of 6000 words.
Lowering the number of bins reduces the accuraccy of the sort and does not provide much increase in speed :(
I have found some Open Source code which by default performs very similarly to Arrays.sort in terms of speed and have modified it to be non-strict in its ordering.
This modified sorting algothim is approximately 15% faster but is still really not fast enough.
By reducing the strict ordering of the sorting algorithm i have also reduced the level of compression. Ultimately i would like to have a fast sorting algothrim which takes into acount semi-sorted objects with out reducing strictness of the sort.
Any ideas?

Tree removal takes O(logn) and insertion takes O(logn). Compare this to an array where potentially a large number of elements need to be shifted O(n).
Usually it takes hundreds of thousands of elements to notice a difference. So if your data is relatively small, then it may not be worth trying to optimize it.
test of the parent may need to be performed to determine The balanced binary tree (TreeSet) that comes with the JDK doesn't allow access to the internal structure of the tree, so testing the parent isn't available. Even if it was, it would still take an O(logn) lookup to find the node and get its parent, so it wouldn't be a significant improvement.

Similar Messages

  • Best algorithm for Sort ?

    Hi ,
    My application involves the large amount of data that needs to be sorted often. Application interacts with the other environment who is
    passing large amount of arithmetic results at run time that I need to sort and display on the screen.
    Can anybody let me know, which sort algorithm will be best suited for me ? Will it be Bubble sort/Bidirectional Bubble sort/ Quick Sort ?
    Thanks in advance.
    Warm regards,

    Radix sort is always good. Although an optimised radix sort can still only barely beat the quicksort in java.util.Array.sort. And it uses extra memory while suicksort is inmemory. However, if you want to use radix sort you can modify this piece of code to suite your type:
    public class WHATEVER{
    //rearranges the data in order
    public void radixSort(int[] data, int nrData){//We use base 16
    if(sorted) return;
    //get highest value
    int maxValue=0;
    for(int i=0; i<nrData; i++) if(data>maxValue) maxValue=data[i];
    //initial length about double the average spread
    int[][] buckets=new int[16][((nrData>>3)>0?(nrData>>3):1)];
    int length[]=new int[16];//A list with the lengths of the lists
    int digits=1;//the max digit needed to be considered
    while((1<<(digits<<2))<maxValue && digits<8) digits++;
    for(int digit=0; digit<digits; digit++){
    for(int i=0; i<16; i++) length[i]=0;
    for(int i=0; i<nrData; i++){
    int index=(data[i]>>(digit<<2))&15;//15 is a mask ...00001111
    if(length[index]>=buckets[index].length){
    buckets[index]=doubleVector(buckets[index]);
    buckets[index][length[index]++]=data[i];
    //link the lists back into data
    int index=0;
    for(int i=0; i<16; i++)
    for(int j=0; j<length[i]; j++)
    data[index++]=buckets[i][j];
    protected int[] resizeVector(int[] vector, int newSize){
    int[] newVector=new int[_newSize];
    System.arraycopy(_vector,0,newVector,0,(_vector.length<_newSize ? vector.length : newSize));
    return newVector;
    protected int[] doubleVector(int[] _vector){
    return resizeVector(_vector, _vector.length<<1);
    Gil

  • Sort algorithm: how can I sort two arrays according to one

    I have two parallel arrays, similar to this:
    var array1 = [4, 3, 1, 5, 2];
    var array2 = ["four", "three", "one", "five", "two"];
    I want to sort the first array, and have the second array follow the same sort as the first. Any advice for a good algorithm would be appreciated.
    Rick Quatro

    Hi Rick,
    frameexpert wrote:
    Technically, there was no correct answer because the problem could be solved in multiple ways,
    I don't think you're right about not marking the question as correct just because there are multiple ways of solving it.
    People search the forums for solutions and threads that are not marked as correct are somewhat relegated.
    It wouldn't hurt to make bduffy232's day or even mark your own solution as correct which is more the less the same as the one on the link from KuddRoww and more the less the way I would have done it but see the difference in the results in the table at the bottom of this post.
    I actually think that technically there can be multiple correct answers even though you can only  one can be marked.
    One other small point if you use the advance editor on the forum you can have you code formatted makes it much easier to read.
    Anyway here's my half pence worth on the scripting front.
    Your question was how to sort one array according to another, which I understand to mean that you want the originals arrays reordered
    Yours and bduffy232's scripts do that properly the others don't. Jongware's first script is easy to fix up this problem by adding to the end of his script.
    for (i = 0; i<Math.min(array1.length, array2.length); i++)
    array1[i] = array3[i][0];
    array2[i] = array3[i][1];
    alert(array1+"\r"+array2)
    Now in order of appearance
    Jongware said:
    .. I wonder why this does not work .. Perhaps my logic is flawed?
    Hi Jongware
    I tried your  (2nd) script twice and on both occurrences it crashed my ESTK
    From what I  understand (not said with much confidence) the flaw in your logic is you are passing your arrays values to be sorted array1[?] and array2[?+1] to the sort function as 'a' and 'b' but and then refer to the array array1[a].
    So let's examine a bit of the code
    var array1 = [4, 3, 1, 5, 2];
    array3.sort (function(a,b) {
    var swap, diff = array1[a] - array1[b];
    When run this is the equivalent of saying (for the first run of the sort loop)
    diff = array1[4] - array1[3]
    i.e.     diff = 2 - 5
    i.e.     diff = -3
    What you wanted to do was
    diff = a - b
            = 4 -3
    That's the way I see it.
    Hi Marc
    Change the first two lines of your first script to:
    var array1 = [40, 7, 1, 5, 2];
    var array2 = ["forty", "seven", "one", "five", "two"];
    Then run the script
    Now for my offering.
    The fancy script
    This script is not limited to a specific number of arrays.
    after creating the arrays one creates an array of the arrays, specifies the number of the array that one wants to sort by and calls the function.
    like this:
    sortArraysInSync([a1,a2,a3,a4,a5], 1);
    The arrays a1, a2, a3, ..... will get sorted according to the numeric or alphabetical order of the array specified in the 2nd parameter.
    One could easily (I think) change the sort function to first sort by array x and then by array y etc.
    // Advanced Sort Variable Numbers of  Arrays in Sync Script by Trevor
    // http://forums.adobe.com/thread/1062126?tstart=0
    var   a1 = [2, 5, 10, 7, 100, 5, .5, 0]
            a2 = ['two', 'five', 'ten', 'seven', 'one hundred', 'five', 'one half', 'zero'],
            a3 = [ 'deux', 'cinq', 'dix', 'sept', 'cent', 'cinq', 'un demi', 'zéro' ],
            a4 = [ 'twee', 'vijf', 'tien', 'zeven', 'honderd', 'vijf', 'helft', 'nul' ],
            a5 = [ 'tsvey ', 'finef', 'tzien', 'zibn', 'eyn honderd', 'finef', 'eyn halb', 'nul' ];
    alert("\tIndividual Arrays Before Sort\r"+a1+"\r"+a2+"\r"+a3+"\r"+a4+"\r"+a5);
    sortArraysInSync([a1,a2,a3,a4,a5], 1);
    alert("\tIndividual Arrays After Sort\r"+a1+"\r"+a2+"\r"+a3+"\r"+a4+"\r"+a5);
    function sortArraysInSync (myArrays, s /* number or the array to sort by (starting from 1) */)
        arr = []; var l1 = ll1 = myArrays[0].length, i = ll2 = myArrays.length;
        s--;
        while(l1--)
                arr[l1]={}           
                i = myArrays.length;
                while(i--) eval("arr["+l1+"].key"+i+" = myArrays[i][l1]");
        arr.sort(mySort);
        while (ll1--) for (i=0; i<myArrays.length;i++) eval ("myArrays[i][ll1] = arr[ll1].key"+i);
        alert("\tFunction Array After Sort\r"+myArrays.join("\r"))
    function mySort(a, b)
            if (eval("isNumber(a.key"+s+") || isNumber(b.key"+s+")")) return eval("a.key"+s+"- b.key"+s+"");
            else if (eval("a.key"+s+".toLowerCase() > b.key"+s+".toLowerCase()")) return 1 else return -1;
    function isNumber(n) {return !isNaN(parseFloat(n)) && isFinite(n)}; // isNumber function from http://stackoverflow.com/questions/18082/validate-numbers-in-javascript-isnumeric/1830844#1830844
    For the simple 2 array sort I would have done as I said much the same as your one:
    // Simple Sort Arrays in Sync Script by Trevor
    // http://forums.adobe.com/thread/1062126?tstart=0
    var   a1 = ['two', 'five', 'ten', 'seven', 'one hundred', 'five', 'one half', 'zero'],
            a2 = [2, 5, 10, 7, 100, 5, .5, 0]
    alert("Individual Arrays Before Sort\r"+a1+"\r"+a2);
    sortArraysInSync(a1,a2);
    alert("Individual Arrays After Sort\r"+a1+"\r"+a2);
    function sortArraysInSync (x, y)
        var arr = [], l1 = l2 = Math.min(y.length, x.length);
        while (l1--) arr.push({name: x[l1], number: y[l1]});
        arr.sort(function(a, b) { return a.number - b.number; });
        while (l2--)
            x[l2] = arr[l2].name;
            y[l2] = arr[l2].number;
    I did a speed test by putting 2 3000 item arrays into my, your dbuffies and Jongware script and timing them.
    These were the results.
    Test done with alerts removed with program set to real time priority and alerts removed.
    These were the arrays and timing method
    var   a1 = ['zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two', 'zero', 'one half', 'five', 'one hundred', 'seven', 'ten', 'five', 'two'],
            a2 = [, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2, 0, 0.5, 5, 100, 7, 10, 5, 2];
    sdate = new Date();
    startTime = sdate.getTime();
    sortArraysInSync([a1,a2],2); // each scipt to its own.
    edate = new Date();
    endTime = edate.getTime();
    totalTime = (endTime-startTime)/1000;
    alert("Individual Arrays After Sort\r"+totalTime+"\r"+a1+"\r"+a2);
    Script
    Seconds taken to sort 2 Arrays 3000 items each
    Comments
    Trevor's Simple Script
    1.3
    Rick's (Frameexpert) Script
    12.3
    Trevor's Fancy Script
    33
    Bduffy's Script
    68
    Jongware's Script
    Crashed, don't know why?
    Don't forget to pick the correct answer.
    My 'fancy' script is slowed down considerably because of thoses evil  evals. Does anyone have a solution for this?
    Regards
    Trevor

  • Newbie trying to sort 2D string array

    Dear all,
    After read some book chapters, web sites including this, I still don't get the point.
    If I have a file like,
    1 aaa 213 0.9
    3 cbb 514 0.1
    2 abc 219 1.3
    9 bbc 417 10.4
    8 dee 887 2.1
    9 bba 111 7.1
    and I load into memory as a String[][]
    here comes the problem, I sort by column 1 (aaa,...,bba) using an adaptation of the Quicksort algorithm for 2D arrays
    * Sort for a 2D String array
    * @param a an String 2D array
    * @param column column to be sort
    public static void sort(String[][] a, int column) throws Exception {
    QuickSort(a, 0, a.length - 1, column);
    /** Sort elements using QuickSort algorithm
    static void QuickSort(String[][] a, int lo0, int hi0, int column) throws Exception {
    int lo = lo0;
    int hi = hi0;
    int mid;
    String mitad;
    if ( hi0 > lo0) {
    /* Arbitrarily establishing partition element as the midpoint of
    * the array.
    mid = ( lo0 + hi0 ) / 2 ;
    mitad = a[mid][column];
    // loop through the array until indices cross
    while( lo <= hi ) {
    /* find the first element that is greater than or equal to
    * the partition element starting from the left Index.
    while( ( lo < hi0 ) && ( a[lo][column].compareTo(mitad)<0))
    ++lo;
    /* find an element that is smaller than or equal to
    * the partition element starting from the right Index.
    while( ( hi > lo0 ) && ( a[hi][column].compareTo(mitad)>0))
    --hi;
    // if the indexes have not crossed, swap
    if( lo <= hi )
    swap(a, lo, hi);
    ++lo;
    --hi;
    /* If the right index has not reached the left side of array
    * must now sort the left partition.
    if( lo0 < hi )
    QuickSort( a, lo0, hi, column );
    /* If the left index has not reached the right side of array
    * must now sort the right partition.
    if( lo < hi0 )
    QuickSort( a, lo, hi0, column );
    * swap 2D String column
    private static void swap(String[][] array, int k1, int k2){
    String[] temp = array[k1];
    array[k1] = array[k2];
    array[k2] = temp;
    ----- end of the code --------
    if I call this from the main module like this
    import MyUtil.*;
    public class kaka
    public static void main(String[] args) throws Exception
    String[][]a = MyUtil.fileToArray("array.txt");
    MyMatrix.printf(a);
    System.out.println("");
    MyMatrix.sort(a,1);
    MyMatrix.printf(a);
    System.out.println("");
    MyMatrix.sort(a,3);
    MyMatrix.printf(a);
    for the first sorting I get
    1 aaa 213 0.9
    2 abc 219 1.3
    9 bba 111 7.1
    9 bbc 417 10.4
    3 cbb 514 0.1
    8 dee 887 2.1
    (lexicographic)
    but for the second one (column 3) I get
    3 cbb 514 0.1
    1 aaa 213 0.9
    2 abc 219 1.3
    9 bbc 417 10.4
    8 dee 887 2.1
    9 bba 111 7.1
    this is not the order I want to apply to this sorting, I would like to create my own one. but or I can't or I don't know how to use a comparator on this case.
    I don't know if I am rediscovering the wheel with my (Sort String[][], but I think that has be an easy way to sort arrays of arrays better than this one.
    I've been trying to understand the Question of the week 106 (http://developer.java.sun.com/developer/qow/archive/106/) that sounds similar, and perfect for my case. But I don't know how to pass my arrays values to the class Fred().
    Any help will be deeply appreciated
    Thanks for your help and your attention
    Pedro

    public class StringArrayComparator implements Comparator {
      int sortColumn = 0;
      public int setSortColumn(c) { sortColumn = c; }
      public int compareTo(Object o1, Object o2) {
        if (o1 == null && o2 == null)
          return 0;
        if (o1 == null)
          return -1;
        if (o2 == null)
          return 1;
        String[] s1 = (String[])o1;
        String[] s2 = (String[])o2;
        // I assume the elements at position sortColumn is
        // not null nor out of bounds.
        return s1[sortColumn].compareTo(s2[sortColumn]);
    // Then you can use this to sort the 2D array:
    Comparator comparator = new StringArrayComparator();
    comparator.setSortColumn(0); // sort by first column
    String[][] array = ...
    Arrays.sort(array, comparator);I haven't tested the code, so there might be some compiler errors, or an error in the logic.

  • What types of sort performed by sort method of Array class ?

    I use normal bubble sort and method Array.sort() to sort some given data of an Array and then count the time.But Array.sort() method takes more time then normal bubble sort.
    Can anybody tell me what types of sort performed by sort method of Array class?

    I'm pretty sure that in eariler versions (1.2, 1.3
    maybe?) List.sort's docs said it used quicksort. Or I
    might be on crack.You are actually both correct, and wrong :)
    From the documentation of the sort methods hasn't changed in 1.2 -> 1.4 (as far as I can notice), and the documentation for sort(Object[]) says (taken from JDK 1.2 docs):
    "This sort is guaranteed to be stable: equal elements will not be reordered as a result of the sort.
    The sorting algorithm is a modified mergesort (in which the merge is omitted if the highest element in the low sublist is less than the lowest element in the high sublist). This algorithm offers guaranteed n*log(n) performance, and can approach linear performance on nearly sorted lists."
    So, how could you be correct? The documentation for e.g. sort(int[]) (and all other primities) says:
    "Sorts the specified array of ints into ascending numerical order. The sorting algorithm is a tuned quicksort, adapted from Jon L. Bentley and M. Douglas McIlroy's "Engineering a Sort Function", Software-Practice and Experience, Vol. 23(11) P. 1249-1265 (November 1993). This algorithm offers n*log(n) performance on many data sets that cause other quicksorts to degrade to quadratic performance."
    Your memory serves you well :)
    /Kaj

  • About the sort method of arrays

    Hi,
    I found that for primitive types such as int, float, etc., algorithm for sort is a tuned quicksort. However the algorithm for sorting an object array is a modified merge sort according to the java API. What is the purpose of using two different sorting strategy?
    Thanks.

    The language knows how to compare two ints, two doubles, etc. For Object arrays, however, it's not a simple matter of comparing two reference values as ints or longs. We're not comparing the references. We have to compare the objects, according to the class's compareTo() method (if it implements Comparable) or a provided Comparator. The size of an int is known, fixed, and small. The size of an object is unknown, variable, and essentially unbounded. I don't know for sure that that difference is what drove the different approaches, but it is a significant difference, and mergesort is better suited to use cases where we can't fit the entire array into memory at one time. It seems reasonable that we'd get better locality of reference (fewer cache misses, less need to page) with an array of ints than with an array of objects.
    Edited by: jverd on Mar 18, 2011 3:16 AM

  • Sort within an array?

    public class Input3
         private String[] input = new String[40];
         private static int StringNum = -1;
         public static final int NUMBER_OF_TEAMS = 3;
         public static final int NUMBER_OF_PLAYERS = 5;
         public Input3()
              //The first six inputs will be for the first team
              input[0] = "LAKERS";
              input[1] = "Kobe Bryant";
              input[2] = "Derek Fisher";
              input[3] = "Shaquille O'Neal";     
              input[4] = "Karl Malone";
              input[5] = "Brian Cook";
              //The next six inputs will be for the second team
              input[6] = "MAVERICKS";
              input[7] = "Antoine Walker";
              input[8] = "Dirk Nowitzki";
              input[9] = "Tony Delk";
              input[10] = "Shawn Bradley";
              input[11] = "Travis Best";
              //The next six inputs will be for the third team
              input[12] = "KNICKS";
              input[13] = "Mike Sweetney";
              input[14] = "Allan Houston";
              input[15] = "Howard Eisley";
              input[16] = "Kurt Thomas";
              input[17] = "Shanon Anderson";
         //This method returns the strings one after the other.
         public String getNextString()
              StringNum++;
              return input[StringNum];
    }What I'm supposed to do with this is sort that data by last names within each team. By that I mean I have to sort input[1] through input[5] by last names, input[7] through input[11] by last names, and same for the Knicks team. I'm not sure how to do 3 separate sorts within an array.
    The output should look like this:
    KNICKS:
    Sharon Anderson
    Howard Eisley
    Allan Houston
    Mike Sweetney
    Kurt Thomas
    Any help is greatly appreciated!

    Yeah, I don't really understand it. But my professor actually gave us that code to work with to be part of a bigger assignment. I actually just posted another question about the same assignment if you want to look at that to see if it might help.
    I dunno how to use the insert link button on here so I'll just let you copy and paste if you are interested in helping me out... Thanks!
    It's here >>> http://forum.java.sun.com/thread.jspa?threadID=5268839

  • What is the best way to scan and sort old photos in iPhoto

    What is the best way to scan and sort old photos in iPhoto?  They do not have digital dates.

    Hey Chicago Sue,
    Once you scan them and have them on your desktop. You should use Automator and assign the common IPTC tags to the images, so that when you do import them into iPhoto, they get recorded.
    Here is an example of an action in Automator:

  • Sort and Object array of Arrays

    Hi,
    I have an object array that contains a String[] and an int[].
    Object[String[], int[]]
    The values in the Strings array must be in the same position in the array as those in the int array i.e
    String [0] is the string representation of int[0].
    Its for a bar chart.
    That is all working beautifully.
    Now however I want to sort the int array in descending order by the value of the ints in the array and thus the order or the String array has to mirror this.
    I am using a comparator to do this but it just wont work. I'm sure this is the way but how should it look?
    Has anybody got any ideas?
    Thanks in advance,
    L.

    use the bubblesort:
        for (int i = a.length; --i >= 0; ) {
            for (int j = 0; j < i; j++) {
                if (a[j][1] > a[j+1][1]) {
                    int temp = a[j];
                    a[j] = a[j + 1];
                    a[j + 1] = temp;
        }

  • Need to sort an object array using an element in the object.

    hi all,
    i need to sort an object array using an element in the object.can someone throw some light on this.
    Edited by: rageeth on Jun 14, 2008 2:32 AM

    [http://java.sun.com/docs/books/tutorial/collections/interfaces/order.html]

  • Sorting between multiple arrays.

    I thought that I had posted this yesterday; however as I can not find my post...
    I need assistance with part of a project where I need to sort between multiple arrays; using one of the columns as the sort criteria. The arrays contain integers, strings, and doubles; however I would like to sort and display the arrays alphabetically using the string column. I was told that a bubble array may work, however as I can not find specific information on the use of a bubble array, I really don't know where to start. The arrays look like the following:
    productArray[0] = new Product(1, "binder ", 15, 1.5, 0, 0);
    productArray[1] = new Product(2, "marker ", 13, .5, 0, 0);
    productArray[2] = new Product(3, "paper ", 24, .95, 0, 0);
    productArray[3] = new Product(4, "pen ", 5, .25, 0, 0); \
    Any assistance that anyone could provide would be greatly appreciated. I am a newbie when it comes to Java and the course materials are extremely vague.
    Dman

    Thank you for your assistance.
    The site that I found to be most helpful was the http://www.onjava.com/lpt/a/3286 site; however I am still experiencing problems. I have added code to the program as noted below:
    public class Inventoryprogrampart3
    /** Creates a new instance of Main */
    * @param args the command line arguments
    public static void main(String[] args)
    // create Scanner to obtain input from command window
    java.util.Scanner input = new java.util.Scanner( System.in );
    double totalInventoryValue = 0;
    NumberFormat nf = NumberFormat.getCurrencyInstance();
    System.out.println(); // Displays a blank line
    System.out.println( " Welcome to the Inventory Program - Part 2 " ); // Display the string
    System.out.println( " ----------------------------------------- " ); // displays a line of characters
    System.out.println(); // Displays a blank line
    System.out.println( "This program will output an office supply inventory" ); // Display the string
    System.out.println( "listing that includes item numbers for the" ); // Display the string.
    System.out.println( "inventoried products, the items product names, the" ); // Display the string.
    System.out.println( "quantity of each product in stock, the unit price" ); // Display the string
    System.out.println( "for each product, the total value of each products" ); // Display the string
    System.out.println( "inventory, and a total value of the entire inventory." ); // Display the string
    System.out.println(); // Displays a blank line
    System.out.println( "*****************************************************" ); // Displays a line of characters
    System.out.println(); // Displays a blank line
    Product[] productArray = new Product[ 7 ]; // creates 7 product arrays
    // adds data to the 7 arrays
    productArray[0] = new Product();
    productArray[0].setitemNumber(1);
    productArray[0].setproductName ( "binder" );
    productArray[0].setitemQuantity(15);
    productArray[0].setitemPrice(1.5);
    productArray[0].setinventoryValue(0);
    productArray[1] = new Product();
    productArray[1].setitemNumber(2);
    productArray[1].setproductName( "paper" );
    productArray[1].setitemQuantity(24);
    productArray[1].setitemPrice(.95);
    productArray[1].setinventoryValue(0);
    productArray[2] = new Product();
    productArray[2].setitemNumber(4);
    productArray[2].setproductName( "pen" );
    productArray[2].setitemQuantity(5);
    productArray[2].setitemPrice(.25);
    productArray[2].setinventoryValue(0);
    productArray[3] = new Product();
    productArray[3].setitemNumber(3);
    productArray[3].setproductName( "marker" );
    productArray[3].setitemQuantity(13);
    productArray[3].setitemPrice(.5);
    productArray[3].setinventoryValue(0);
    productArray[4] = new Product();
    productArray[4].setitemNumber(5);
    productArray[4].setproductName( "pencil" );
    productArray[4].setitemQuantity(28);
    productArray[4].setitemPrice(.4);
    productArray[4].setinventoryValue(0);
    productArray[5] = new Product();
    productArray[5].setitemNumber(7);
    productArray[5].setproductName( "tape" );
    productArray[5].setitemQuantity(14);
    productArray[5].setitemPrice(1.65);
    productArray[5].setinventoryValue(0);
    productArray[6] = new Product();
    productArray[6].setitemNumber(6);
    productArray[6].setproductName( "staples" );
    productArray[6].setitemQuantity(13);
    productArray[6].setitemPrice(1.25);
    productArray[6].setinventoryValue(0);
    System.out.println( "Inventory listing prior to sorting by product name:" );
    System.out.println(); // Displays a blank line
    System.out.println( "Item #"+"\t"+"Product Name"+"\t"+"Stock"+"\t"+"Price"+"\t"+"Total Value"); // Displays a header line for the inventory array display
    System.out.println(); // Displays a blank line
    System.out.println( "-----------------------------------------------------" ); // Displays a line of characters
    System.out.println(); // Displays a blank line
    for (int i=0; i<=6; i++)
    Product products = productArray;
    String productName = products.getproductName();
    int itemNumber = products.getitemNumber();
    int itemQuantity = products.getitemQuantity();
    double itemPrice = products.getitemPrice();
    double inventoryValue = products.getinventoryValue();
    System.out.println( productArray[i].getitemNumber() +"\t"+ productArray[i].getproductName() +"\t"+"\t" + productArray[i].getitemQuantity() +"\t"+ nf.format(productArray[i].getitemPrice()) +"\t"+ nf.format(productArray[i].getinventoryValue()) );
    Arrays.sort(productArray);
    System.out.println( "-----------------------------------------------------" ); // Displays a line of characters
    System.out.println(); // Displays a blank line
    System.out.println( "Inventory listing after being sorted by product name:" );
    System.out.println(); // Displays a blank line
    System.out.println( "Item #"+"\t"+"Product Name"+"\t"+"Stock"+"\t"+"Price"+"\t"+"Total Value"); // Displays a header line for the inventory array display
    System.out.println(); // Displays a blank line
    System.out.println( "-----------------------------------------------------" ); // Displays a line of characters
    for(int i=0; i <= 6; i++)
    totalInventoryValue = totalInventoryValue + productArray[i].getinventoryValue(); // calculates the total value of the entire products inventory
    System.out.println( productArray[i].getitemNumber() +"\t"+ productArray[i].getproductName() +"\t"+"\t"+ productArray[i].getitemQuantity() +"\t"+ nf.format(productArray[i].getitemPrice()) +"\t"+ nf.format(productArray[i].getinventoryValue()) );
    }// end for
    System.out.println(); // Displays a blank line
    System.out.println( "The total value of the entire inventory is: " + nf.format(totalInventoryValue) ); // Displays the entire inventory value
    System.out.println(); // Displays a blank line
    System.out.println( "*****************************************************" ); // Displays a line of characters
    } // end public static void main
    }// end public class Inventoryprogrampart3 main
    The following utilities have been set:
    import java.io.*;
    import java.text.*;
    import java.util.Scanner;
    import java.util.*;
    import java.util.Arrays;
    import java.util.ArrayList;
    import java.util.Comparator;
    The program compiles, however when I try to run the program I receive the following error (which outputs about 1/2 way through the program:
    Exception in thread "main" java.lang.ClassCastException: Product can not be cast to java language comparable.
    (along with a listing of other errors - I can't even get my cut and paste to work on my command prompt window).
    I've tried about 50 different iterations and nothing seems to work.

  • Group sort / merge sort

    Is there any way i can do group sort/merge sort thru PL/SQL.
    Output Example:
    number course
    1000 MATH
    SCI
    LANG
    1040 MATH
    SCI
    Can we do this?

    are you looking for this?
    here is example from emp table at sqlplus
    SQL> break on dno
    SQL> select dno,ename from emp where dno in(10,20)
      2  order by 1;
           DNO ENAME
            10 CLARK
               KING
               MILLER
            20 SMITH
               ADAMS
               FORD
               SCOTT
               JONES

  • How can i sort out the N lowiest elements from an 1D array faster than using the build in sort function (1D-array) in Labview?

    I need an algorithm that is based on the same sorting-algorithm that Labview uses (or one that is just as fast) but it only needs to sort out the N lowiest elements (and the N-element output array don't need to be sorted).
    /Jonas

    I want all three zeros in the output. You can se the algorithm that I'm looking for as a soft version of Labviews own sort algorithm that only gives you the N (always less then the length of the array) lowiest values as output.If two elements contain the same small value both should be sorted out.
    /Jonas

  • How to call a C sort function to sort a Java Array.

    My name is David, I'm interning this summer doing some High Performance Computing work. I'm significantly out of my comfort zone here; I am primarily a network/network security geek, not a programming guy. I took one Java based class called problem solving with programming where we wrote like 3 programs in Java and did everything else in pseudocode and using a program called Alice http://www.alice.org/ to do things graphically. Learned basically no actual programming syntax. Also have done some self-taught perl, but only through one book and I didn't finish it, I only got about half way through it. So my expertise in programming are pretty much null.
    That being said, I currently am tasked with having to figure out how to make JNI work... specifically at this time I am tasked with writing an array in Java, and designing a C program that can be called by means of JNI to sort the array. I have chosen to work with the Merge Sort algorithm. My method of coding is not one where I write the entire thing from scratch, I don't particularly have a need to master languages at this point, rather I just need to make them work. I am interested in learning, but time is of the essence for me right now. So thus far what I have done is take sample codes and tweak them to meet my purpose. However, I currently am unable to make things work. So I am asking for help.
    I am going to paste 3 codes here, the first one will be my basic self-written instructions for JNI (Hello World Instructions), the second one will be my Java Array, and the third one will be my MergeSort function. I am not asking for you to DO my work for me by telling me how to manipulate my code, but rather I am asking for you to send me in the direction of resources that will be of some aid to me. Links, books (preferrably e-books so I don't have to go to a library), anything that you can send my direction that may help will be deeply appreciated. Thanks so much!
    JNI Instructions:
    /*The process for calling a C function in Java is as follows:
    1)Write the Java Program name. Eg. HelloWorld.java
    2)Compile it: javac HelloWorld.java
    3)Create a header file: javah -jni HelloWorld
    4)Create a C program eg. HelloWorld.java
    5)Compile the C program creating a shared library eg. libhello.so (My specifc command is cc -m32 -I/usr/java/jdk1.7.0_05/include -I/usr/java/jdk1.7.0_05/include/linux -shared -o libhello.so -fPIC HelloWorld.c
    6) Copy the library to the java.library.path, or LD_LIBRARY_PATH (in my case I have set it to /usr/local/lib.
    7)Run ldconfig (/sbin/ldconfig)
    8)Run the java program: java HelloWorld. */
    //Writing the code:
    //For the HelloWorld program:
    //In java:
    //You need to name a class:
    class HelloWorld {
    //You then need to declare a native method:
    public native void displayHelloWorld();
    //You now need a static initializer:
    static {
    //Load the library:
    System.loadLibrary("hello");
    /*Main function to call the native method (call the C code)*/
    public static void main(String[] args) {
    new HelloWorld().displayHelloWorld();
    //In C:
    #include <jni.h> //JNI header
    #include "HelloWorld.h" //Header created by the javah -jni command parameter
    #include <stdio.h> //Standard input/output header for C.
    //Now we must use a portion of the code provided by the JNI header.
    JNIEXPORT void JNICALL
    Java_HelloWorld_displayHelloWorld(JNIENV *env, jobject obj)
    //Naming convention: Java_JavaProgramName_displayCProgramName
        printf("Hello World!\n");
        return;
    }Java Array:
    class JavaArray {
         private native int MergeSort(int[] arr);
         public static void main(String[] args)
             int arr[] = {7, 8, 6, 3, 1, 19, 20, 13, 27, 4};
         static
             System.loadLibrary("MergeSort");
    }Hacked and pieced together crappy C Merge Sort code:
    #include <jni.h>
    #include <stdio.h>
    #include "JavaArray.h"
    JNIEXPORT jint JNICALL
    Java_JavaArray_MergeSort(JNIEnv *env, jobject obj, jintArray arr[],jint low,jint mid,jint high)
       jint i,j,k,l,b[10];
    l=low;
    i=low;
    j=mid+1;
    while((l<=mid)&&(j<=high))
        if(arr[l]<=arr[j])
           b=arr[l];
    l++;
    else
    b[i]=arr[j];
    j++;
    i++;
    if(l>mid)
    for(k=j;k<=high;k++)
    b[i]=arr[k];
    i++;
    else
    for(k=l;k<=mid;k++)
    b[i]=arr[k];
    i++;
    for(k=low;k<=high;k++)
    arr[k]=b[k];
    void partition(jint arr[],jint low,jint high)
    jint mid;
    if(low<high)
    mid=(low+high)/2;
    partition(arr,low,mid);
    partition(arr,mid+1,high);
    sort(arr,low,mid,high);

    You're doing OK so far up to here:
    Java_JavaArray_MergeSort(JNIEnv *env, jobject obj, jintArray arr[],jint low,jint mid,jint high)This is not correct. It is not what was generated by javah. It would have generated this:
    Java_JavaArray_MergeSort(JNIEnv *env, jobject obj, jintArray arr,jint low,jint mid,jint high)A 'jintArray' is already an array, embedded in an object. You don't have an array of them.
    So you need to restore that, and the header file, the way 'javah' generated them, then adjust your code to call GetIntArrayElements() to get the elements out of 'arr' into a local int[] array, sort that, and then call ReleaseIntArrayElements() to put them back.

  • Sorting the Message Array

    Hi
    Does anyone know whats the best way to sort an array of Message[] after getting this array from the Folder.
    Sorting by date is straight forward (ie sort by msg #, ascending / descending).
    How about sorting by FROM, SUBJECT or SIZe (in each case ascending/descendin)?
    Any idea would be much appreciated
    many thanx

    You could sort "by hand" using e.g. the simple bubble-sort but might also want to
    have a look at collections (java.util.Collection and the like) above version 1.2.

Maybe you are looking for

  • Past Itunes Purchases, Help.

    I'm trying to download past purchases from my iTunes account onto my computer. It's telling me, "You can download past purchases on this computer with just one Apple ID every 90 days. This computer can be used with a different Apple ID in 75 days." D

  • How to configure email Alerts in OEM Cloud 12c for Database Servers up/down

    Hi everybody, How to configure email Alerts in OEM Cloud 12c for Database Servers up/down status? Regards, Miguel Vega

  • IPhoto '11 crashes after rebuilding

    So I have gone through the boards to search for a solution.  I too am getting the KERN_PROTECTION_FAILURE exception.  I have downloaded the iPhoto Library Manager, rebuilt the library, but nothing works.  Faces seems to break my library each time.  W

  • ITSM: Cancelled Change Documents - Number range goes missing

    Hi Experts, We create RFCs, Urgent, Normal, Admin changes. however when we "create change record/change doc" a number is assigned to the record/doc. Sometimes we cancel and come out of the Creation process and then again restart our process. This tim

  • Old comp crashed library lost!

    My old computer (with itunes library) crashed and i lost all of the information. I have a new computer but cant fully restore all the old songs onto new library. The old songs are still on the ipod mini but can i add new ones without deleting existin