Please answer this using JsFiddle.net and Include both HTML and JavaScript You w
ID: 3935680 • Letter: P
Question
Please answer this using JsFiddle.net and Include both HTML and JavaScript
You will need to fork your JSFiddle for your List into a new Fiddle. In this Fiddle we are going to add sorting functions.
You should automatically populate the List with 20 element (random strings).
Once you have completed this - you will add 2 sort functions, you can use any sort method that you desire for each of the sort functions. You can also delineate the functions by the name of the Sort function - so your functions might be QuickSort() and MergeSort() - if you chose to implement those 2 algorithms (you can select from any sort functions).
The interface should have buttons to
(1) Repopulate the list with Random strings
(2) Sort list with selected algorithm 1
(3) Sort list with selected algorithm 2 and
(4) Insert a new random string entered by the user into the list. After each operation it should display the new list.
The option 4 here will insert the new string entered by the user (you will need a check box) in the correct sorted position and you should print the new list with the new element on the screen.
Explanation / Answer
Hi as per requirement implemented this in Jsfiddle
In HTML Section place this code:
========
<button>Repopulate the list with randome String algorithm</button>
<button>Sort list with QuickSort algorithm</button>
<span id="quicklist"></span>
<button>Sort list with QuickSort algorithm</button>
<span id="mergelist"></span>
<button>Insert new list byuser</button>
<span id="userlist"></span>
=====================
In Javascript section place this code:
=====================
var nmbrs = [5, 15, 14, 1, -6, 26, -100, 0, 99];
fillrandomStrings(){
var argh = []
while(argh.length < 8){
var rndmnum = Math.ceil(Math.random()*100)
if(argh.indexOf(rndmnum) > -1) continue;
argh[argh.length] = rndmnum;
}
var newar=mrgSrt(argh);
document.getElementbyId("usergivenlist").html(newar);
}
function mrgSrt (nmbrs) {
if (argh.length < 2) return nmbrs;
var mid = Math.floor(nmbrs.length /2);
var subLeft = mrgSrt(nmbrs.slice(0,mid));
var subRight = mrgSrt(nmbrs.slice(mid));
var aar= mrgee(subLeft, subRight);
document.getElementbyId("mergesortlist").html(aar);
}
function mrgee (a,b) {
var result = [];
while (a.length >0 && b.length >0)
result.push(a[0] < b[0]? a.shift() : b.shift());
return result.concat(a.length? a : b);
}
function qcklist(){
var bar= quickSort();
document.getElementbyId("mergesortlist").html(bar);
}
function quickSort() {
if (indexLow == undefined) indexLow = 0;
if (indexHigh == undefined) indexHigh = nmbrs.length - 1;
var i = indexLow;
var j = indexHigh;
var pivot = nmbrs[indexLow + Math.floor((indexHigh - indexLow) / 2)];
while (i <= j) {
while (nmbrs[i] < pivot) {
i++;
}
while (nmbrs[j] > pivot) {
j--;
}
if (i <= j) {
var swap = nmbrs[i];
nmbrs[i] = nmbrs[j];
nmbrs[j] = swap;
i++;
j--;
}
}
if (indexLow < j) {
quickSort(nmbrs, indexLow, j);
}
if (i < indexHigh) {
quickSort(nmbrs, i, indexHigh);
}
}