I'm trying to implement quick sort in javascript and need await sleep() inside a function, but when I'm using async the function is not getting correct result.
On removing async from the partition function code is working fine.
Can anyone help?
async function partition(low, high) {
var pivot = random_numbers[low]; // random_number is a global variable contains 10 random number
var i = low+1;
var j = high;
while (i <= j){
if (random_numbers[i] < pivot){
i++;
}else{
swap_values(i, j); // this will swap values in random_numbers
swap_boxes_sq(i, j); // this will perform some animation
await sleep(MAX_SLEEP_TIME); // MAX_SLEEP_TIME = 2000 and sleep is defined in other file
j--;
}
}
swap_values(low, i-1);
return i-1;
}
function sort_q( low, high) {
if (low < high){
var pi = partition( low, high);
sort_q(low, pi-1);
sort_q(pi+1, high);
}
}
function quick_sort(low, high) {
low = 0;
high = number_of_box -1; // number_of_box = 10
sort_q(low, high);
console.log(random_numbers);
}
sleep()?