• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1263
  • Last Modified:

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

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;
}

Open in new window

0
mildurait
Asked:
mildurait
  • 4
  • 3
1 Solution
 
fsze88Commented:
mildurait,
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 !

<table>
     <tr>
          <td onclick="reOrder('myTBody',1,0,0);">ID</td>
          <td onclick="reOrder('myTBody',2,3,1);">Name</td>
          <td onclick="reOrder('myTBody',3,2,1);">DOB</td></tr>
     </tr>
     <tbody id="myTBody">
         <tr ordernum=1>
            <td sortvalue="0000001">1</td>
            <td sortvalue="Peter">Peter</td>
            <td sortvalue="19750119">19/01/1975</td>
          </tr>
            <tr ordernum=3>
                <td sortvalue="0000004">4</td>
                <td sortvalue="Jane">Jane</td>
                <td sortvalue="19800201">01/02/1980</td>
            </tr>
           <tr ordernum=2>
              <td sortvalue="0000002">2</td>
              <td sortvalue="Mark">Mark</td>
              <td sortvalue="19880731">31/07/1988</td>
           </tr>
            <tr ordernum=3>
                <td sortvalue="0000003">3</td>
                <td sortvalue="Jane">Jane</td>
                <td sortvalue="19600201">01/02/1960</td>
            </tr>
           <tr ordernum=4>
              <td sortvalue="0000002">2</td>
              <td sortvalue="Mark">Mark</td>
              <td sortvalue="19890731">31/07/1989</td>
           </tr>
            <tr ordernum=5>
                <td sortvalue="0000003">3</td>
                <td sortvalue="Jane">Jane</td>
                <td sortvalue="20000201">01/02/2000</td>
            </tr>
     </tbody>
</table>
 
<script type="text/javascript">
var trTagsArray = new Array();
 
function initTBody(){
  var tbodytag = document.getElementById("myTBody");
  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);
//    alert("oValue : " + oValue);
    tempArray = Quicksort(tempArray,oValue);
  }
 
 
//  tempArray = Quicksort(tempArray,3);
 
    for (i = 0; i< tempArray.length;i++){
        tbodytag.appendChild(tempArray[i]);
    }
}
 
function Quicksort(sortArray,voValue){
 
      var tempArray = new Array().concat(sortArray);
      var lessArray = new Array();
      var moreArray = new Array();
      var resultArray = new Array();
      var sortValue="";
 
      if (sortArray.length == 1){
        return sortArray[0];
      }else{
 
      pivot = tempArray.pop();
      pivotValue = getSortValue(pivot, voValue);
 
      for (i=0;i<tempArray.length;i++){
        if (voValue ==3){
          sortValue = parseInt(getSortValue(tempArray[i],voValue));
        }else{
                sortValue = getSortValue(tempArray[i],voValue);
        }
        if (sortValue <= pivotValue){
          lessArray.push(tempArray[i]);
        }else{
          moreArray.push(tempArray[i]);
        }
      }
 
      
      
      if (lessArray.length > 0){
        resultArray = resultArray.concat(Quicksort(new Array(lessArray),voValue));
      }
 
      resultArray = resultArray.concat(pivot);
 
 
      if (moreArray.length > 0){
        resultArray = resultArray.concat(Quicksort(new Array(moreArray),voValue));
      }
 
      return resultArray;
      }
 
}
 
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;
}
 
initTBody();
</script>

Open in new window

0
 
milduraitAuthor Commented:
@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?
0
 
fsze88Commented:
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....
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
milduraitAuthor Commented:
@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.
0
 
fsze88Commented:
please try this,
note : this is not responsible for error and/or guarantee faster than bubble sort
have a fun....
<table>
     <tr>
          <td onclick="reOrder('myTBody',1,0,0);">ID</td>
          <td onclick="reOrder('myTBody',2,3,1);">Name</td>
          <td onclick="reOrder('myTBody',3,2,1);">DOB</td></tr>
     </tr>
     <tbody id="myTBody">
         <tr ordernum=1>
            <td sortvalue="0000001">1</td>
            <td sortvalue="Peter">Peter</td>
            <td sortvalue="19750119">19/01/1975</td>
          </tr>
            <tr ordernum=3>
                <td sortvalue="0000004">4</td>
                <td sortvalue="Jane">Jane</td>
                <td sortvalue="19760201">01/02/1976</td>
            </tr>
           <tr ordernum=2>
              <td sortvalue="0000002">2</td>
              <td sortvalue="Mark">Mark</td>
              <td sortvalue="19880731">31/07/1988</td>
           </tr>
            <tr ordernum=3>
                <td sortvalue="0000003">3</td>
                <td sortvalue="Jane">Jane</td>
                <td sortvalue="19600201">01/02/1960</td>
            </tr>
           <tr ordernum=4>
              <td sortvalue="0000006">6</td>
              <td sortvalue="Mark">Mark</td>
              <td sortvalue="19990731">31/07/1999</td>
           </tr>
            <tr ordernum=5>
                <td sortvalue="0000005">5</td>
                <td sortvalue="Jane">Jane</td>
                <td sortvalue="19610201">01/02/1961</td>
            </tr>
     </tbody>
</table>
 
<script type="text/javascript">
var trTagsArray = new Array();
 
function initTBody(){
  var tbodytag = document.getElementById("myTBody");
  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);
    tempArray = Quicksort(tempArray,oValue);
  }
 
////      tempArray = Quicksort(new Array().concat(tempArray),3);
////      alert("final tempArray.length : " + tempArray.length);
 
/*
  try{
      for (i=0;i<tempArray.length;i++){
        document.write("result : " + getSortValue(tempArray[i],1) + "<br/>");
      }
  }catch(err){
    document.write('abc');
  }
*/  
 
 
    for (i = 0; i< tempArray.length;i++){
        tbodytag.appendChild(tempArray[i]);
    }
 
}
 
function Quicksort(sortArray,voValue){
 
      var resultArray = new Array();
 
      var tempArray = new Array().concat(sortArray);
 
      var lessArray = new Array();
      var moreArray = new Array();
      var pivot;
      var pivotValue;
      var sortValue="";
      
//      var element = sortArray[0];
      
//      alert("element : " + element + "<br/>");
//      alert("tempArray[0].nodeName : " + tempArray[0].nodeName + "<br/>");
 
      
      if (sortArray.length == 1){
        return sortArray[0];
      }else{
      
        pivot = tempArray.pop();
        pivotValue = getSortValue(pivot, voValue);
  
        for (i=0;i<tempArray.length;i++){
          sortValue = getSortValue(tempArray[i],voValue);
////          alert("pivotValue : " + pivotValue + " sortValue : " + sortValue + "<br/>");
          if (sortValue <= pivotValue){
            lessArray.push(tempArray[i]);
          }else{
            moreArray.push(tempArray[i]);
          }
        }
 
/*
        for (i=0;i<lessArray.length;i++){
          document.write("left : " + getSortValue(lessArray[i],voValue) + "<br/>");
        }
*/        
        if (lessArray.length > 0){
          resultArray = new Array().concat( resultArray.concat(Quicksort( new Array().concat(lessArray) , voValue) ) );
        }
  
////        alert("pivotValue : " + pivotValue);
        resultArray = new Array().concat(resultArray.concat(pivot));
        
/*  
        for (i=0;i<moreArray.length;i++){
          document.write("right : " + getSortValue(moreArray[i],voValue) + "<br/>");
        }
  */
        
        if (moreArray.length > 0){
          resultArray = resultArray.concat(Quicksort(new Array().concat(moreArray) , voValue));
        }
        return resultArray;
      }
 
}
 
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;
}
 
initTBody();
</script>

Open in new window

0
 
fsze88Commented:
did you tried it? It works on QuickSort Algorithm
0
 
milduraitAuthor Commented:
Well done again!
Thanks for your time and expertise.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now