Does anyone know of a free JavaScript implementation of a fast sorting algorithm? MergeSort, Quicksort, any of those.
Printable View
Does anyone know of a free JavaScript implementation of a fast sorting algorithm? MergeSort, Quicksort, any of those.
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.
Bubble sort is too slow. I need a n*logn approach, not a n² approach.
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.
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 :)
well, give me the psuedo code and I'll do it.
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);
}
I presume that arrTable is the table you want sorted, but what are:intColumn, sp and zg?
intColumn is because this function can sort multidim arrays, sp and zg are sort internals.
I have to rewrite it for DOM nodes anyway...