Results 1 to 9 of 9

Thread: Good sorting in JavaScript

  1. #1

    Thread Starter
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Good sorting in JavaScript

    Does anyone know of a free JavaScript implementation of a fast sorting algorithm? MergeSort, Quicksort, any of those.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  2. #2
    Frenzied Member Acidic's Avatar
    Join Date
    Sep 2003
    Location
    UK
    Posts
    1,090
    Is Bubble sort too slow?
    You could make it go through one item less each time, this would speed it up a bit.
    eg, first it checks all 10 items, then the 9 first, the 8, 7 etc...
    That speeds it up a bit.
    Have I helped you? Please Rate my posts.

  3. #3

    Thread Starter
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Bubble sort is too slow. I need a n*logn approach, not a n² approach.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  4. #4
    Frenzied Member Acidic's Avatar
    Join Date
    Sep 2003
    Location
    UK
    Posts
    1,090
    I found this:
    http://www.concentric.net/~ttwang/sort/quick.c
    Which is supposedly quite quick. I cannot read Java so I can't convert it to JavaScript though.
    Will keep looking.

    edit:
    http://cg.scs.carleton.ca/~morin/misc/sortalg/
    gives lots of algorithms, MergeSort seems the best.
    Last edited by Acidic; Dec 17th, 2003 at 04:51 PM.
    Have I helped you? Please Rate my posts.

  5. #5

    Thread Starter
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Thanks, but
    1) It's C, not Java (not that that matters, I can read both).
    2) I can find source in other languages easily, it's JavaScript that poses a problem
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  6. #6
    Frenzied Member Acidic's Avatar
    Join Date
    Sep 2003
    Location
    UK
    Posts
    1,090
    well, give me the psuedo code and I'll do it.
    Have I helped you? Please Rate my posts.

  7. #7

    Thread Starter
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Found one
    Code:
    function quickSort(arrTable, intColumn, sp, zg) {
           var k = arrTable[Math.round((sp+zg)/2)][intColumn];
           var i = sp;
           var j = zg;
    
           while  (j > i) {
                  while (arrTable[i][intColumn] < k)
                         ++i;
    
                  while (k < arrTable[j][intColumn])
                         j = j - 1;
    
                  if (i <= j) {
                         var d = arrTable[i];
                         arrTable[i] = arrTable[j];
                         arrTable[j] = d;
                         ++i;
                         j = j - 1;
                  }
           }
    
           if (sp<j)
                  quickSort(arrTable, intColumn, sp, j);
    
           if (i<zg)
                  quickSort(arrTable, intColumn, i, zg);
    
     }
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  8. #8
    Frenzied Member Acidic's Avatar
    Join Date
    Sep 2003
    Location
    UK
    Posts
    1,090
    I presume that arrTable is the table you want sorted, but what are:intColumn, sp and zg?
    Have I helped you? Please Rate my posts.

  9. #9

    Thread Starter
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    intColumn is because this function can sort multidim arrays, sp and zg are sort internals.

    I have to rewrite it for DOM nodes anyway...
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width