• maratcolumn1 (unregistered) in reply to xasperated

    Worse, well let's see... I looked very and very hard... At least it's easy to read and understand, and it should work, even with arrays of lengths zero and one (I wonder what would happen if some strings are shorter than 10 though). And it follows Java naming standards. And he could have accepted filesVector.size() as parameter (or it would be improvement?).

    WTF's I found:

    1. Does not use standard functions

    2. Function name makes no sense
      3. And it is not static

    3. Does not accept String[]

    4. Vector, not ArrayList

    5. Converts from Vector to String[] manually

    6. Uses bubble sort, 4 times

    7. Of which 3 can be omitted

    8. Does not compare strings
      A. Catches exceptions better to let go
      B. In the most inner loop
      C. And continues

      Who noticed more?

      An yes, can we just stop mentioning bubble sort anywhere? I mean, like conspiracy. Start explanation with Shell sort for instance? I mean none of these people invented it themselves, they read about it somewhere...


  • (cs)

    Ya know, I think he has actually twigged onto one of the least efficient variants of the bubblesort.

    Damn this sucker REALLY takes me back, it has been litterally DECADES since I've seen a bubblesort in code.

  • (cs) in reply to ferrengi

    ferrengi:
    OK,
    So does anyone want to take a stab at why he had to do the exact same sort 4 times?
    He could've done it just once and accomplish the exact same thing so what is special about the number 4?
    I could not come up with any theory about this.
    Any ideas?

    I'm guessing it went something like this:

    • The coder saw four fields: YYYY | MM | DD | HH
    • The coder remembered that American dates (mmddyy) have to be reformatted before they sort correctly and applied the same logic to this problem, ignoring or overlooking the correct left-to-right layout of ordering information.
    • The coder started sorting from left-to-right, but found that the data wasn't sorted after sorting the first field.  Therefore, they added a completely new sorting loop on the next field and subsequent fields.
    • When the data still didn't order correctly after four sorting loops, they added logic in the loops to include comparisons to the left-of-current field.
    • Since the result produced a correctly ordered list, they stopped coding and never cleaned up their code.
    • The coder probably used the first sort code they encountered or remembered from their Introduction to Programming class.

    I suspect there was either no code review or the reviewers were equally clueless.  Someone would/should have looked for a built-in sorting method.

    Here are some of my general ramblings if a Sort method was not available in the language/environment:

    1. Sorting on all 10 characters, in their filename order, in a single loop would have worked.  Reduction from 4 loops down to one.
    2. Casting the 10 character filenames into long integers would improved the speed of any sorting algorithm tremendously, since integer variables compare so much faster than character strings.
    3. Sometimes GUI developers can cheat on this problem by populating a hidden filelistbox control on a form with the files, which will be sorted by the control.  Voila!  Codeless sorting.  Not always available to all operating environments.
    4. Event faster than the Quicksort would be some form of a bucket sort, where the mmddhh would present 8928 unique possible values (12 * 31 * 24).  All items in the same bucket are identical.  YYYY produces separate lists to sort, with an assumption that there aren't too many different years' worth of files in a directory.

    Mea Culpa: I admit that I cheated in a Windows scripting environment by shelling "DIR /B /On > dirlist.txt" and then reading the contents of the dirlist.txt file, containing sorted filenames.  It worked.  I am lazy.

  • iWantSimpleLife (unregistered)

    Is it just me? Or is it that Java tend to promote ugly coding? Most of the Java codes that I have seen people write ends up as being a huge mess of logic and formatting.

  • (cs) in reply to aikimark

    This whole thing reminds me of the sorters used in the pre-computer days of punch cards.  The sorter would accept a set of punch cards, and put each of those cards into a separate bin depending on the value in a particular column.  Then the operator would collect the cards form each bin in order, reload them into the feeder, and sort them by the previous column.  This process would continue, sorting by each column of greater significance until finally the deck was sorted.

    So if you wanted to sort on a 20 character name field, you'd need 20 passes through the sorter.

  • maratcolumn1 (unregistered) in reply to aikimark

    I admit that I cheated in a Windows scripting environment by shelling "DIR /B /On > dirlist.txt" and then reading the contents of the dirlist.txt file, containing sorted filenames.  It worked.  I am lazy.

    Now consider the fact that in Windows environment file names are always sorted (NTFS stores them sorted by design). You were not just lazy.
  • (cs) in reply to dshiznit
    dshiznit:
    dshiznit:
    First!

    Sorry. I was just so frickin excited to see there were no posts yet!


    You're a moron.

    Sincerely,
    Richard Nixon
  • (cs) in reply to ferrengi

    ferrengi:
    OK,
    So does anyone want to take a stab at why he had to do the exact same sort 4 times?
    He could've done it just once and accomplish the exact same thing so what is special about the number 4?
    I could not come up with any theory about this.
    Any ideas?

     

    You mean other than the file name to sort was broken up into 4 logical sub parts(yyyy, mm, dd, and hh)?  Maybe an attempt at optimization?  Group everything by matching sub parts?  Smoked some crack that day?

  • bailey (unregistered) in reply to iWantSimpleLife
    Anonymous:
    Is it just me? Or is it that Java tend to promote ugly coding? Most of the Java codes that I have seen people write ends up as being a huge mess of logic and formatting.


    nahh, its just poor coders. and its a fairly popular language. the more popular the language the more likely you'll find people coding in it that don't know what they are doing.
  • (cs) in reply to kipthegreat
    kipthegreat:
    Ytram:
    dshiznit:
    dshiznit:
    First!

    Sorry. I was just so frickin excited to see there were no posts yet!


    I fully endorse the genocide of people like yourself that get excited by being the first to post, or think that such an accomplishment is worthy of any kind of kudos or acknowledgement.


    I also endorse the genocide of the people who add another ten stupid posts saying "I hate people who post 'first'".  And the people who make posts bitching about people who bitch about people who make 'first' posts.  And yes, I realize I am embracing my own genocide.  If that is how it must be then that is how it must be.  I will be remembered as a martyr!


    Now we have some recursion that 'The Specialist' could have used.
  • (cs)
    <font size="3">The thing I dont get is this:</font>
    <font size="3">int len = filesVector.size();</font>
    <font style="font-family: Arial;" size="3">len = filesArray.length - 1;
    </font><font size="3">[snip some lame code]
    for (i = 0; i < len; i++)
    [snip some lame code]
    </font>
    <font style="font-family: Arial;" size="3">Doesnt he loose an element here?</font>
    


  • (cs) in reply to ray
    ray:
    <font size="3">The thing I dont get is this:</font>
    <font size="3">int len = filesVector.size();</font>
    <font style="font-family: Arial;" size="3">len = filesArray.length - 1;
    </font><font size="3">[snip some lame code]
    for (i = 0; i < len; i++)
    [snip some lame code]
    </font>
    <font style="font-family: Arial;" size="3">Doesnt he loose an element here?</font>
    


    Sorry got it !!!! See the +1 on valueB
  • (cs) in reply to xasperated

    Is there any worse way? Why yes! Instead of four bubble sorts, he could have used four bogo sorts ;-)

  • (cs) in reply to htowninsomniac

    I'm guessing that in his mind, he felt like he needed to sort four times because each substring of the YYYYMMDDHH string has a different cycle. HH goes from 00 to 23 (or 01 to 24), DD goes from 01 to 31, MM goes from 01 to 12, and YYYY increments. He probably didn't think about the fact that the value representing YYYYMMDDHH increments continuously as a whole.

    Now, before flaming this post, I'm not justifying what he did. I'm simply trying to recreate his thought process...

    [:|]

  • A_little_pain (unregistered) in reply to KraGiE
    KraGiE:

    dunno why, but I just thought of something..

    For all the developers that talk about genocide of dumb developers, I wonder if any of you guys are the ones that are making these types of manslaughter to coding.  It was definitely off topic, but yeah. 



    Me, me, me, ME!

    The last time I coded with Java I was only able to use language features from 1.0.2 (or was it .8?) due to having to target several VMs for pocketPCs (You know, like the Beta version Sun released that was up to 10 times faster than any commercial option, but was quietly dropped....). "Cross platform Solution". Bollocks!! I wrote so much WTF code on that project that Ive never voluntarily touched Java since. It wouldnt suprise me to find out *this* WTF was due to having to target early JVMs.....

    Anyways, now I get to create WTFs with XSLT and perl....lovely.
  • Basher (unregistered) in reply to John V.

    >>"What's your vector, Victor?"

    Shirley you can't be serious????

  • JustaBloke (unregistered) in reply to Ytram

    Ytram:
    dshiznit:
    dshiznit:
    First!

    Sorry. I was just so frickin excited to see there were no posts yet!


    I fully endorse the genocide of people like yourself that get excited by being the first to post, or think that such an accomplishment is worthy of any kind of kudos or acknowledgement.

     

    and yet you managed to acknowledge him while telling him what a fool he is [^o)]

    kinda weakens your staement a little don't cha think [8-)]

    [;)]

     

     

  • (cs) in reply to ferrengi

    <font size="2">don't you sort everything 4 times just to make sure it's properly sorted? I always do. I swear.</font>
    <font size="2"></font>

  • (cs) in reply to maratcolumn1

    maratcolumn1:

    Now consider the fact that in Windows environment file names are always sorted (NTFS stores them sorted by design). You were not just lazy.

    Your statement incorrectly assumes the file system was NTFS.

    Kudos to my fellow dinosaur, RevMike, who worked with tab equipment back in the good old days when users didn't have computers.  IT professionals had a computer.  A multi-field sort and an inability to compare multiple fields' data, requires you to sort the fields in 'bottom up' order.  In this case, sorting each of the 'fields' in each of the four loops, in right-to-left sequence, would produce a correctly sorted list.  If there is only one year (YYYY) value, the sorting can stop after the third pass (MM).

    In my earlier comment, I stated that the entire filename numeric value could be contained in a long integer variable.  This statement is true until the year 2148, which would exceed the upper limit of a signed long integer.  You could use an unsigned long integer if the language supported it or you could subtract the first file's year value from all (string -> int) values.  Or you could just leave it to future programmers, since you would likely to have passed to that great one-way hash in the sky. :-)

  • (cs) in reply to John V.
    Anonymous:

    Nand:
    Anonymous:
    Ah! I see the WTF - he's using a Vector...


    Of course. Vector fileVector. This is obviously a vector of files... no wait. Strings.

    /not like naming it filenameVector would make the function justifyable..

    "What's your vector, Victor?"

    "Check our Clearance, Clarence"

  • Steve (unregistered) in reply to TankerJoe
    TankerJoe:


    The only difference is that I keep the reference from when I created the array, whereas you get the reference by casting the return object from the toArray method.  Maybe we can chalk this up to programmer preference/style?  Programming is an art form after all.



    Think about thread safety too. If there's no danger of "c" changing size in the middle of your operation, then you're fine. Otherwise, there's a chance that toArray(Object[]) will need to return a bigger array than the "a" you passed in, and it will allocate a new one. Because you've discarded the return value, you don't know this, and you're left thinking you've got the contents of the collection in your array when in fact you haven't. Likewise, "c" could shrink, in which case there will be some nulls at the end of the array. These might surprise you if you assume that "a.length" is the number of values returned from the collection.

    For the sake of re-usability and refactoring, since the difference between the two cases is pretty negligible, personally I'd tend to use the thread-safe approach even where it's not needed - you have less chance of creating trouble for yourself later. But as long as your thread exclusively owns "c", it's not necessary.
  • (cs) in reply to ParkinT
    ParkinT:
    Anonymous:

    Nand:
    Anonymous:
    Ah! I see the WTF - he's using a Vector...


    Of course. Vector fileVector. This is obviously a vector of files... no wait. Strings.

    /not like naming it filenameVector would make the function justifyable..

    "What's your vector, Victor?"

    "Check our Clearance, Clarence"



    "This is getting silly, Sally!"

  • ServerDude (unregistered) in reply to RevMike
    RevMike:
    kipthegreat:
    Anonymous:
    kipthegreat:


    At least in Java 5 code like this is no longer needed (just the snippet here, not whole wtf). I've had to do it several times and always found it a little annoying.

    Now (assuming filesVector is defined as Vector<string>), all that can be replaced with-

    String[] filesArray = filesVector.toArray();

    or something like that, I forget exact syntax



    Yet another Java programmer who doesn't know what he is talking about. Vector#toArray() is there since ancient Java 1.2. Particular the version were you supply your own array as an argument works like a charm since ages when you need to get a special type of array. </string>



    Cool, I didn't realize that. I had tried casting the result to a special array   (String[])vector.toArray() ... which obviously doesn't work since Object[] can't be casted to String[].  I guess I stopped there since a loop was simple and I didn't catch that the one with the parameter would correctly handle a String[] argument.

    At least I know I'm not the only one that doesn't know this..  I've seen a loop like this in other people's code many times, but I've never seen the toArray(Object[]) method used.

    The toArray(Object[]) method is available in everything that implements java.util.Collection.

    The first time I tried to use this method, I struggled to get the syntax correct.  Here is an example to keep handy...

    import java.util.Collection;
    import java.util.Vector;

    public class ToArrayDemo {
        public static void main(String[] args) {
            Collection c = new Vector();
           
            c.add("Foo");
            c.add("Bar");
            c.add("Baz");

            String[] a = (String[]) c.toArray(new String[]{});

            for (int i = 0; i < a.length; i++) {
                System.out.println(a[i]);
            }
        }
    }



    Which makes you wonder whether he wrote it before Java 1.2 (the birth of Collections), which would excuse him for not using Collections.sort().

    The beautiful wtf is it doesn't work - it's not even bubblesort.Assume the vector is filled with:
    2005122315.ext, IamGonnaBleepYourCode.ext, 2004010101.ext, getThis.ext, 2006444444.ext

    Because he's compairing neighbours only and will skip these due to his "errorhandling" the 4 elements above will not be sorted - not even the numerical ones.

    I assume he tested the code - well, if he tested - with few elements based on a Vector filled with filenames he got from an already sorted list .. mmm... test data input on output form.


  • schabi (unregistered) in reply to Steve

    Your approach still is not thread safe, as the size of the container still can change between the internal array allocation and the array fill inside the toArray() method. Hey, it could even change during the internal copying operation, probably making the toArray() fail with an ArrayIndexOutOfBoundsException.

  • (cs) in reply to KraGiE
    KraGiE:

    dunno why, but I just thought of something..

    For all the developers that talk about genocide of dumb developers, I wonder if any of you guys are the ones that are making these types of manslaughter to coding.  It was definitely off topic, but yeah. 



    When I do something stupid like that, I'm well aware that I'm not doing it the best way, and put in comment why.

    I was only recently introduced to a C++ compiler that was powerful enough to handle the standard template library.   (We just drop OS/2 support this year, there are a lot of WTFs in our codebase to reflect the need to compile on a platform where most compilers have not been updated in 10 years)

    Just after it happened I wrote a comment in my code "This is a O(N^2) algorithm because I'm too lazy to write my own hash table when is isn't expected to exceed 16 in the real world.  Still the ideal solution would be a hash table".    A couple months latter I realized that the above code could use stl::vector, but by then I had debugged it.

    I guess my point is that a good programmer may in fact write awful code from time to time, but the good programmer will know he did so, and write a comment to explain why.   Future maintainers will then know that why it is that way, and feel free to change to something else if there is a need.
  • (cs) in reply to hank miller
    hank miller:
    [A couple months latter I realized that the above code could use stl::vector, but by then I had debugged it.


    s/std::vector/std::map/


  • (cs) in reply to ParkinT
    ParkinT:
    Anonymous:

    Nand:
    Anonymous:
    Ah! I see the WTF - he's using a Vector...


    Of course. Vector fileVector. This is obviously a vector of files... no wait. Strings.

    /not like naming it filenameVector would make the function justifyable..

    "What's your vector, Victor?"

    "Check our Clearance, Clarence"



    "Roger, Roger."

  • adsf (unregistered) in reply to xasperated
    Anonymous:
    Is there any possible way of doing that worse?  Just in performance with all the loop interations it must be a joy. And debugging that monstrosity would be horrifying.



    Well, I guess you could nest the loops somehow so that it'd run in O(n^8) time and make it move the actual file contents around..
  • (cs) in reply to ferrengi

    ferrengi:
    OK,
    So does anyone want to take a stab at why he had to do the exact same sort 4 times?
    He could've done it just once and accomplish the exact same thing so what is special about the number 4?
    I could not come up with any theory about this.
    Any ideas?

    that's an easy one, the specialist thought that if he sorted by year first, then the second round (by month) would be much faster, then the third round (by day) faster and so on

    after all he was the specialist, whatever he came up with, was the greatest invention

  • Chief Typist (unregistered)

    I guess O(n^2) wasn't bad enough for this guy...

  • Veej (unregistered) in reply to schabi

    Well, no on the last part.  It's a Vector.  Vectors are thread safe (and all versions of the toArray() method are synchronized)...so it can't change inside the method.

    Then again (and going back to the WTF), it was 'fileVector' but contained Strings...so maybe it's not even a Vector after all. :P

  • Eq (unregistered) in reply to ferrengi

    "Barley-compilable" code could be Perl. You know... Pearl barley...?

    I'll get me coat.

  • (cs) in reply to Chief Typist

    Chief Typist:
    I guess O(n^2) wasn't bad enough for this guy...

    O(4 * n^2)

    misquoting the bard: "Double, bubble, toil and code.  Index flail and coder chode."

  • (cs) in reply to aikimark

    Luckily bubble sort is stable.

  • (cs) in reply to aikimark
    aikimark:

    Chief Typist:
    I guess O(n^2) wasn't bad enough for this guy...

    O(4 * n^2)

    misquoting the bard: "Double, bubble, toil and code.  Index flail and coder chode."

     

    Come what come may, time and the hour sorted in a drunken haze.

  • (cs) in reply to Steve

    Anonymous:
    TankerJoe:


    The only difference is that I keep the reference from when I created the array, whereas you get the reference by casting the return object from the toArray method.  Maybe we can chalk this up to programmer preference/style?  Programming is an art form after all.



    Think about thread safety too. If there's no danger of "c" changing size in the middle of your operation, then you're fine. Otherwise, there's a chance that toArray(Object[]) will need to return a bigger array than the "a" you passed in, and it will allocate a new one. Because you've discarded the return value, you don't know this, and you're left thinking you've got the contents of the collection in your array when in fact you haven't. Likewise, "c" could shrink, in which case there will be some nulls at the end of the array. These might surprise you if you assume that "a.length" is the number of values returned from the collection.

    For the sake of re-usability and refactoring, since the difference between the two cases is pretty negligible, personally I'd tend to use the thread-safe approach even where it's not needed - you have less chance of creating trouble for yourself later. But as long as your thread exclusively owns "c", it's not necessary.

    You make good points.  However, if thread safety is an issue,  It would probably be wise to get an sychronized lock on "c" before  creating the array, and executing the toArray method.  Not every class in the Collections api is thread safe.

     

     

  • (cs) in reply to RevMike
    RevMike:
    This whole thing reminds me of the sorters used in the pre-computer days of punch cards.  The sorter would accept a set of punch cards, and put each of those cards into a separate bin depending on the value in a particular column.  Then the operator would collect the cards form each bin in order, reload them into the feeder, and sort them by the previous column.  This process would continue, sorting by each column of greater significance until finally the deck was sorted.

    So if you wanted to sort on a 20 character name field, you'd need 20 passes through the sorter.


    The sorters that I saw needed two passes to sort a column alphabetically.  Ah, the joys of the radix sort.

    Sincerely,

    Gene Wirchenko

  • (cs) in reply to Basher
    Anonymous:
    >>"What's your vector, Victor?"

    Shirley you can't be serious????


    I'm quite serious.  And don't call me Shirley.

  • (cs) in reply to Ytram
    Ytram:
    dshiznit:
    dshiznit:
    First!

    Sorry. I was just so frickin excited to see there were no posts yet!


    I fully endorse the genocide of people like yourself that get excited by being the first to post, or think that such an accomplishment is worthy of any kind of kudos or acknowledgement.


    Oh, come on. Fess up to it. You know you're just jealous. You wanted to be first.


    And the [image] goes to dshiznit!
  • bramster (unregistered) in reply to Ytram
    Ytram:
    dshiznit:
    dshiznit:
    First!

    Sorry. I was just so frickin excited to see there were no posts yet!


    I fully endorse the genocide of people like yourself that get excited by being the first to post, or think that such an accomplishment is worthy of any kind of kudos or acknowledgement.


    Don't be to hard on him.    His father's sperm was "first"


  • (cs) in reply to bailey
    Anonymous:
    Anonymous:
    Is it just me? Or is it that Java tend to promote ugly coding? Most of the Java codes that I have seen people write ends up as being a huge mess of logic and formatting.


    nahh, its just poor coders. and its a fairly popular language. the more popular the language the more likely you'll find people coding in it that don't know what they are doing.

    Visual Basic being the world's worst offender.
  • (cs) in reply to TankerJoe
    TankerJoe:

    QuinarySort      -    YYYYMMDDHHMM.ext


    I like the Quinnery sort.

  • Alistair Wall (unregistered)

    This will break down in the year 10000.

  • (cs) in reply to aikimark
    aikimark:

    Chief Typist:
    I guess O(n^2) wasn't bad enough for this guy...

    O(4 * n^2)

    misquoting the bard: "Double, bubble, toil and code.  Index flail and coder chode."



    O(4*n^2) = O(n^2).
  • (cs) in reply to aikimark
    aikimark:
    1. Sometimes GUI developers can cheat on this problem by populating a hidden filelistbox control on a form with the files, which will be sorted by the control.  Voila!  Codeless sorting.  Not always available to all operating environments.

    I never thought I'd ever see someone admit they've done that.

    <too loudly>well, I have never done that in my career!</too>

  • FunnyMan (unregistered) in reply to ennuija
    ennuija:

    O(4*n^2) = O(n^2).


    I think the point of the original comment was that even O(n^2) wasn't bad enough, so they had to increase the coefficient too!

    Actually, the bit that gets me the most is that in the sort (all four instances) the coder uses a while loop to implement a for loop, and then immediately inside uses a for loop!

    Alex Papadimoulis:

        len = filesArray.length - 1;
        // [[Some variable declarations]]
        while (len > 0) {
          for (i = 0; i < len; i++) {
            // [[Loop body]]
          }
          len--;
        }


    Why not just use:
    // [[Some variable declarations]]
    for (len = filesArray.length - 1; len > 0; len--)
      for (i = 0; i < len; i++) {
        // [[Loop body]]
      }
    }
    ?  It's what the for loop was designed for, after all!

    Makes me wonder if the inside is a cut&paste job from a textbook sorting algorithm (and that the coder didn't understand for loops).  It'd certainly explain the choice of algorithm.
  • FunnyMan (unregistered) in reply to FunnyMan

    Bah, the less-than sign triggered HTML issues.  You get the idea.
    -FM

  • (cs) in reply to FunnyMan

    triso:
    Visual Basic being the world's worst offender.

    I'm not sure that it is the fault of either the IDE or language.  VB made Windows programming something that hobbyists could do.  With no formal CompSci education or mentoring, it isn't any wonder that bad code was borrowed from public examples with no regard to appropriateness.

    Anonymous:
    This will break down in the year 10000.

    Reminds me of a great joke (circa 1999) about Bill Gates being revived in the year 9998 from his cryogenic slumber.  The heads of the world body was so glad to have him on hand to help them avert the looming cyber crisis, since he'd survived the Y2K crisis.  Bill asked, in wonder, what possible crisis could exist in such a mature (by this time) field as IT.  The world leaders replied, the "Y10K crisis, of course!"

    Jenny Simonds:

    I never thought I'd ever see someone admit they've done that.

    well, I have never done that in my career!

    Sometimes, you've got to do whatever will produce the next version of the program for your client to see, with little regard for what other IT professionals will think of it.  Such is the world of RAD thinking.

    Is there nothing you've done for which you aren't (now) proud? 

    FunnyMan:

    ...and that the coder didn't understand for loops. 

    I don't think that's all the coder didn't understand :-)

  • plugwash (unregistered) in reply to ferrengi

    its simple, he didn't understand that the whole POINT of arranging dates like that was to make them easy to sort so he did the sort by breaking the date up and dealing with each part seperately!


  • plugwash (unregistered) in reply to plugwash

    oops didn't notice the second page that reply was to the last post on the first page.

Leave a comment on “The Quadrasort”

Log In or post as a guest

Replying to comment #:

« Return to Article