mildurait
asked on
Sorting Javascript Array using the QuickSort Algorithm
I have the following javascript code which sorts <tr> elements within a <tbody> container.
As I have a large number of <tr> elements to sort, I am wondering if the quick sort method would be appopriate, if anyone can translate the code I have.
The wikipedia reference to Quicksort is here http://en.wikipedia.org/wiki/Quicksort
As I have a large number of <tr> elements to sort, I am wondering if the quick sort method would be appopriate, if anyone can translate the code I have.
The wikipedia reference to Quicksort is here http://en.wikipedia.org/wiki/Quicksort
var trTagsArray = new Array();
function initTBody(tdBodyID){
var tbodytag = document.getElementById(tdBodyID);
var tbodytagChilds = tbodytag.childNodes
var i = 0;
for (i=0;i<tbodytagChilds.length;i++){
if (tbodytagChilds[i].nodeName == "TR"){
trTagsArray.push(tbodytagChilds[i]);
}
}
}
function reOrder(tbodyid,o1,o2,o3){
var tbodytag = document.getElementById(tbodyid);
var tbodytagChilds = tbodytag.childNodes;
var splicedTrTag;
var oValue;
var i = 0;
var smallestSortValue = "ZZZZZZZZZZ";
var smallestIndex;
var tempArray = new Array().concat(trTagsArray);
var tempArray2 = new Array();
var sortValue="";
while (tbodytagChilds.length > 0){
tbodytag.removeChild(tbodytagChilds[0]);
}
for (var k=3;k>=1;k--){
oValue = eval("o" + k);
while (tempArray.length > 0){
smallestSortValue = "ZZZZZZZZZZ";
for (i=0;i<tempArray.length;i++){
sortValue = getSortValue(tempArray[i],oValue);
if (sortValue < smallestSortValue){
smallestSortValue = sortValue;
smallestIndex = i;
}
}
splicedTrTag = tempArray[smallestIndex];
tbodytag.appendChild(splicedTrTag);
tempArray.splice(smallestIndex,1);
tempArray2.push(splicedTrTag);
}
tempArray = new Array().concat(tempArray2);
tempArray2 = new Array();
}
}
function getSortValue(trTag,tagIndex){
var trTagChilds = trTag.childNodes;
var indexedTdTag;
var numTdTag = 0;
var j=0;
var returnValue = "";
for (j=0;j<trTagChilds.length;j++){
if (trTagChilds[j].nodeName == "TD"){
++numTdTag;
if (tagIndex == numTdTag){
indexedTdTag = trTagChilds[j];
break;
}
}
}
try{
returnValue = indexedTdTag.attributes['sortvalue'].value;
}catch(err){
returnValue = "";
}
return returnValue;
}
ASKER
@fsze88
So if I hear you right
a) this is a burden which is way too heavy and impossible for you to carry alone?
b) only Jesus, the living Son of God, who has the authority over all Heaven and Earth would have the ability to solve the insolvable?
So if I hear you right
a) this is a burden which is way too heavy and impossible for you to carry alone?
b) only Jesus, the living Son of God, who has the authority over all Heaven and Earth would have the ability to solve the insolvable?
mildurait,
I will keep try to solve this.... give me times. Also, I interested in this question but not guarantee I can/able to solve it out....
I will keep try to solve this.... give me times. Also, I interested in this question but not guarantee I can/able to solve it out....
ASKER
@fsze88
You've done great things for me so far, and I appreciate your help.
We might need to develop a replacement language for JavaScript, called JesusScript which would by definition support an unlimited number of concurrent recursive calls.
You've done great things for me so far, and I appreciate your help.
We might need to develop a replacement language for JavaScript, called JesusScript which would by definition support an unlimited number of concurrent recursive calls.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
did you tried it? It works on QuickSort Algorithm
ASKER
Well done again!
Thanks for your time and expertise.
Thanks for your time and expertise.
I have tried to solve it out...
But... I facing several problems
1) pass arrays to the function Quicksort Recursively
2) threading problem, I can't wait until first job was finished then fire another one ....
Sorry, I am not Jesus !
Open in new window