Hey,

Good job.  Nice trick. I messed around with an ArrayList and
Collections.sort which was faster then my original (the 15 minute job) but
yours was still faster.  I tried your trick only to find out that in
ArrayList the elementData field is private.  :0(  Short of customizing an
implementation  List myself.... you win.  for now....

What was the deal with making your array 1 element shorter then mine?
Competitive edge right?

Good luck and thanks for the idea.

By the way, how is your Tomcat doing?  hehe

Got to get back to work.

Regards,
Craig


--The comparison code -- (includes ArrayListSort)

import java.util.*;

class TestVectorSort    {

        Vector vec = new Vector();
        SortableVector vVec = new SortableVector();
        ArrayList arrList = new ArrayList();

        public static void main(String[] args)  {
                TestVectorSort v = new TestVectorSort();
                v.arraySimpleSort();
        }

        void arraySimpleSort()  {
                // the hack...hehe
                for (int i = 10000; i >0; i--)  {
                        vec.add(new String("" + i));
                }
                long timeIn = System.currentTimeMillis();
                Object[] obArr = vec.toArray();
                Arrays.sort(obArr);
                vec.removeAllElements();
                for (int i = 0; i < obArr.length; i++)  {
                        vec.add(obArr[i]);
                }
                long timeOut = System.currentTimeMillis();
                obArr = null;
                System.out.println("Time to sort CraigsVectorSort: " + (timeOut - 
timeIn)
+ " ms");

                // VladsVectorSort
                for (int i = 10000; i >0; i--)  {
                        vVec.add(new String("" + i));
                }
                long timeIn2 = System.currentTimeMillis();
                vVec.sort();
                long timeOut2 = System.currentTimeMillis();
                System.out.println("Time to sort VladsVectorSort: " + (timeOut2 - 
timeIn2)
+ " ms");

                // ArrayListSort
                for (int i = 10000; i >0; i--)  {
                        arrList.add(new String("" + i));
                }
                long timeIn3 = System.currentTimeMillis();
                Collections.sort(arrList);
                long timeOut3 = System.currentTimeMillis();
                System.out.println("Time to sort ArrayList: " + (timeOut3 - timeIn3) + 
"
ms");

                arrList = null;
                vVec = null;
                vec = null;
                System.gc();
        }
}

class SortableVector extends java.util.Vector
        {
          public synchronized void sort()
          {
            java.util.Arrays.sort(elementData, 0, elementCount);
          }
        }


-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]]
Sent: Monday, March 05, 2001 9:59 AM
To: [EMAIL PROTECTED]
Subject: Re: QuickSorting Vectors


Ooops...
Replace

java.util.Arrays.sort(elementData, 0, elementCount - 1);

with

java.util.Arrays.sort(elementData, 0, elementCount);



----------------------------------------------------



I claim this is faster (inspired by the Vector's source code, he-he):

public class VladsVectorSort  {

        SortableVector vec = new SortableVector();

        public static void main(String[] args)  {
                VladsVectorSort v = new VladsVectorSort();
                v.arraySimpleSort();
        }

        void arraySimpleSort()  {
                vec.add(new String("abbot"));
                vec.add(new String("silium"));
                vec.add(new String("randy"));
                vec.add(new String("goofy"));
                vec.add(new String("zenia"));
                vec.add(new String("pokie"));
                vec.add(new String("xylaphone"));
                vec.add(new String("dogface"));
                vec.add(new String("friggen-joe"));
                vec.add(new String("penuckle"));
                vec.add(new String("charlie"));
                vec.add(new String("manna"));

                System.out.println("before: " + vec);
                vec.sort();
                System.out.println("after :0) : " + vec);
        }

        class SortableVector extends java.util.Vector
        {
          public synchronized void sort()
          {
            java.util.Arrays.sort(elementData, 0, elementCount - 1);
          }
        }
}


This is it (at least it seems to be!)
Of course you need to make sure all Vector elements are mutually
Comparable...

--V.



Craig O'Brien wrote:
>
> Vlad and I were thinking along the same lines.  This is easy but may not
be
> the best for commercial applications.
>
> Here is a simple program I just wrote:
>
> class CraigsVectorSort  {
>
>         Vector vec = new Vector();
>
>         public static void main(String[] args)  {
>                 CraigsVectorSort v = new CraigsVectorSort();
>                 v.arraySimpleSort();
>         }
>
>         void arraySimpleSort()  {
>                 vec.add(new String("abbot"));
>                 vec.add(new String("silium"));
>                 vec.add(new String("randy"));
>                 vec.add(new String("goofy"));
>                 vec.add(new String("zenia"));
>                 vec.add(new String("pokie"));
>                 vec.add(new String("xylaphone"));
>                 vec.add(new String("dogface"));
>                 vec.add(new String("friggen-joe"));
>                 vec.add(new String("penuckle"));
>                 vec.add(new String("charlie"));
>                 vec.add(new String("manna"));
>
>                 System.out.println("before: " + vec);
>                 Object[] obArr = vec.toArray();
>                 Arrays.sort(obArr);
>                 vec.removeAllElements();
>                 for (int i = 0; i < obArr.length; i++)  {
>                         vec.add(obArr[i]);
>                 }
>                 obArr = null;
>                 System.out.println("after :0) : " + vec);
>         }
> }
>
> It works efficiently.  You may want to consider using one of the
collections
> rather then Vector.  You may not need the synchronization so an ArrayList
> would be faster.  Many many considerations.
>
> Let me know what you think.
>
> Regards,
> Craig O'Brien
>
> Java Programmer/Web Developer
> [EMAIL PROTECTED]
>
> -----Original Message-----
> From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]]
> Sent: Friday, March 02, 2001 6:40 PM
> To: [EMAIL PROTECTED]
> Subject: Re: QuickSorting Vectors
>
> Don't have a sample but you can
> take a look at the java.util.Arrays's sort(Object[]) method.
> May be you can use that in combination with Vector.toArray()
> which calls System.arraycopy, so this should be fast, may be
> even faster than iterating over Vector's elements during
> custom sort if you decide to sort Vectors directly.
>
> method...
>
> --V.
>
> Ryan wrote:
> >
> > I am a student but this is not for a class.. I am trying to keep a
thread
> > running that holds files in a directory. I just want to maximize my
> bandwith
> > and CPU usage for my web site.
> >
> > -ryan
> >
> > ----- Original Message -----
> > From: "Stefán F. Stefánsson" <[EMAIL PROTECTED]>
> > To: <[EMAIL PROTECTED]>
> > Sent: Friday, March 02, 2001 5:21 PM
> > Subject: RE: QuickSorting Vectors
> >
> > > R U a student?
> > >
> > > When is the deadline?
> > >
> > > -----Original Message-----
> > > From: Ryan [mailto:[EMAIL PROTECTED]]
> > > Sent: 3. mars 2001 00:46
> > > To: [EMAIL PROTECTED]
> > > Subject: QuickSorting Vectors
> > >
> > >
> > > Does anyone have a quicksorting class that sorts Vectors containing
> > > Strings that wouldn't mind letting me look at?
> > >
> > >
> > > -ryan
> > >
> > >
> > > ---------------------------------------------------------------------
> > > To unsubscribe, e-mail: [EMAIL PROTECTED]
> > > For additional commands, email: [EMAIL PROTECTED]
> > >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: [EMAIL PROTECTED]
> > For additional commands, email: [EMAIL PROTECTED]
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, email: [EMAIL PROTECTED]
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, email: [EMAIL PROTECTED]

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, email: [EMAIL PROTECTED]


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, email: [EMAIL PROTECTED]

Reply via email to