//-----------------
values = new Array();
function randomString(len) {
var text = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
for( var i=0; i < len; i++ )
text += possible.charAt(Math.floor(Math.random() * possible.length));
return text;
}
// to populate the original list
function populateList() {
var originalList = document.getElementById("originalList");
for(var i=0; i<20; i++) {
var li = document.createElement('li');
li.innerHTML = randomString(10);
values.push(li.innerHTML);
originalList.append(li);
}
}
populateList();
// On add new item
document.getElementById("addItem").addEventListener("click", function(){
var itemContent = document.getElementById("newItem").value;
if ( (itemContent==null) || (itemContent=="") ) {
alert("Cannot add empty values");
return;
}
var originalList = document.getElementById("originalList");
var li = document.createElement('li');
li.innerHTML = itemContent;
values.push(li.innerHTML);
originalList.append(li);
});
function emptySortedList() {
var lis = document.querySelectorAll('#sortedList li');
for(var i=0; li=lis[i]; i++) {
li.parentNode.removeChild(li);
}
}
function fillSortedList(values) {
var sortedList = document.getElementById("sortedList");
for(var i=0; i < values.length; i++) {
var li = document.createElement('li');
li.innerHTML = values[i];
sortedList.append(li);
}
}
// on bubble sort
document.getElementById("bubbleSort").addEventListener("click", function(){
emptySortedList();
// bubble sort
var result = bubbleSort(values);
fillSortedList(result);
});
//Insertion
document.getElementById("insertionSort").addEventListener("click", function(){
emptySortedList();
// insertion sort
var result = insertionSort(values);
fillSortedList(result);
});
//Merge Sort
document.getElementById("mergeSort").addEventListener("click",...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.