I have been working on this project for a while now and I would appreciate it if you guys could help me out.
The quick sort algorithm works but since it uses recursion I don't know how or where I should rearrange the HTML divs created.
I have tried rearranging the divs after each swap function is called but that does not seem to work any ideas on how I might solve this problem?
class Element{
constructor(value , id){
this.value = value;
this.id = id;
}
}
function generateElemenets(){
for(let i = 0 ; i < 10; i++ ){
let randomValue = generateRandomValue();
values[i] = new Element(randomValue , i);
const newDiv = document.createElement("div");
newDiv.classList.add("element");
newDiv.setAttribute("id", i);
container.appendChild(newDiv);
document.getElementById(i).style.height = `${randomValue}px`;
}
}
function generateRandomValue(){
let x = Math.floor((Math.random() * 100) + 20);
return x;
}
function quickSort(arr, start, end){
if(start >= end){
return;
}
let index = partition(arr,start, end);
quickSort(arr, start, index-1);
quickSort(arr, index + 1, end);
return arr;
}
function partition(arr, start, end){
let pivotIndex = start;
let pivotValue = arr[end].value;
for (let i = start; i < end ; i++){
if(arr[i].value < pivotValue){
swap(arr, i, pivotIndex);
pivotIndex ++;
}
}
swap(arr,pivotIndex, end);
return pivotIndex;
}
function swap(items, leftIndex, rightIndex){
var temp = items[leftIndex];
items[leftIndex] = items[rightIndex];
items[rightIndex] = temp;
}