0

My HTML and JS are all here:

https://gist.github.com/RoloRobot/b2e15af9ab0d8c1bdbdd

My quicksort works perfectly well but when I try to test it, I can only view it in console. It generates random numbers everytime. I am trying to make it so that when I press the "Commence Quicksort" button, the sorted random sequence will appear below the button and will continue for however many times I press the button.

Any help would be appreciated!

4
  • Add this to your sort function: var div = document.getElementById('QuickTimes'); div.innerHTML = div.innerHTML + array + '<br />'; By the way, you have mistakes in the HTML. Commented Nov 24, 2015 at 23:45
  • Please copy your code into your question. Links can break over time (though I doubt github is going anywhere soon) and future readers of this question may not be able to see your code. Thanks, and welcome to Stack Overflow. Commented Nov 24, 2015 at 23:52
  • I'll look over the html. As for this, I tried it before and got the same error of "cannot read property of innerHTML of null" Commented Nov 24, 2015 at 23:53
  • Thank you for the advice SnareChops, i'll do that Commented Nov 24, 2015 at 23:54

3 Answers 3

1

Stack snippets are your friend!

Your code seems to work great when I replace console.log with document.getElementById("QuickTimes").insertAdjacentHTML("beforeend",array);

function quickSort(array, left, right){
   var len = array.length, 
   pivot,
   partitionIndex;


  if(left < right){
    pivot = right;
    partitionIndex = partition(array, pivot, left, right);
    
   quickSort(array, left, partitionIndex - 1);
   quickSort(array, partitionIndex + 1, right);
  }
  return array;
}

function partition(array, pivot, left, right){
   var pivotValue = array[pivot],
       partitionIndex = left;

   for(var i = left; i < right; i++){
    if(array[i] < pivotValue){
      swap(array, i, partitionIndex);
      partitionIndex++;
    }
  }

  swap(array, right, partitionIndex);
  return partitionIndex;
}

function swap(array, i, j){
   var temp = array[i];
   array[i] = array[j];
   array[j] = temp;
}

function RandNum(array, quantity) {
    var num;
    for (var i = 0; i < quantity; i++) {
        num = Math.floor(Math.random() * (100 - 50 + 1)) + 10;
        if (num !== array[i - 1]) {
            array.push(num);
        } else {
            i--;
        }
    }
}

function sort(array){
    quickSort(array,0,array.length - 1);
    document.getElementById("QuickTimes").insertAdjacentHTML("beforeend",array+"<br/>");
}
<button onclick="var a = []; RandNum(a, 9); sort(a);">Quicksort Commence!</button> 
<div id="QuickTimes"> </div> 

Sign up to request clarification or add additional context in comments.

3 Comments

I did this but I did not use insertAdjacentHTML, this works perfectly, thx
you should use inserAdjacentHTML. ;-) developers.google.com/web/updates/2011/08/…
Oh I see, it better helps define where you want to put your content. Thanks for the info :)
1

I think you are looking for insertAdjacentHTML

<!-- YOUR HTML -->
<button onclick="var a = []; RandNum(a, 9); sort(a);">
Quicksort Commence!</button>

<div id="QuickTimes">
</div>

========================================================

//The javascript you need

function sort(array){

    //find the element you want to insert html into
    var el = document.getElementById('QuickTimes'); 

    quickSort(array,0,array.length - 1);

    //for each item in your array insert a p element with the item in the array as the text
    array.forEach(function (item) {
        el.insertAdjacentHTML('beforeend', '<p>' + item + '</p>');     
    });

 }

Reference: https://developer.mozilla.org/en-US/docs/Web/API/Element/insertAdjacentHTML

4 Comments

I tried this and when I clicked "Commence Quicksort!", nothing happened like before. To be clear, what I am trying to do is make it so that the quicksorted numbers appear on screen. If you run my code and check console, everytime you click the button a new array of sorted numbers is created. I am trying to get that onto the html of the page.
I'm sorry, I thought you knew what this would do. Did you just copy and paste the exact code? You have to actually modify the code slightly to get it to do exactly what you want.
I have updated it, someone else has also posted a different version of the same solution. The difference between their solution and mine is that mine will produce an element for each item in your array which can then be individually modified. Theirs will produce a single element with the entire array inside. Good luck!
One more thing. Try to stay away from naming your variables things like "array." There are reserved words in javascript and until you know what those are you should just at least make sure you use something unique and descriptive for your variable names
0

Add the following code after your console.log();

var quickTimes = document.getElementById("QuickTimes");
var child = document.createTextNode(array);
quickTimes.appendChild(child);

var linebreak = document.createElement('br');
quickTimes.appendChild(linebreak);

1 Comment

Glad to hear! Be sure to upvote if you'd be so kind.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.