Click to See Complete Forum and Search --> : Good sorting in JavaScript
CornedBee
Dec 17th, 2003, 02:08 AM
Does anyone know of a free JavaScript implementation of a fast sorting algorithm? MergeSort, Quicksort, any of those.
Acidic
Dec 17th, 2003, 11:25 AM
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.
CornedBee
Dec 17th, 2003, 03:28 PM
Bubble sort is too slow. I need a n*logn approach, not a nē approach.
Acidic
Dec 17th, 2003, 03:38 PM
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.
CornedBee
Dec 17th, 2003, 03:51 PM
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 :)
Acidic
Dec 17th, 2003, 03:52 PM
well, give me the psuedo code and I'll do it.
CornedBee
Dec 17th, 2003, 03:54 PM
Found one
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);
}
Acidic
Dec 17th, 2003, 03:58 PM
I presume that arrTable is the table you want sorted, but what are:intColumn, sp and zg?
CornedBee
Dec 17th, 2003, 04:03 PM
intColumn is because this function can sort multidim arrays, sp and zg are sort internals.
I have to rewrite it for DOM nodes anyway...
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.