var data = [8, 3, 4, 1, 18, 22, 11, 3, 5, 6, 2, 1, 77]
quickSort(data, 0, data.length - 1)
console.log(data)
var index = binarySearch(data, 18);
console.log(index)
function quickSort(data, left, right) {
if(data.length < 2)
{
return;
}
var index = partition(data, left, right);
if (left < index - 1) {
quickSort(data, left, index - 1);
}
if (right > index) {
quickSort(data, index, right);
}
}
function partition(data, left, right) {
var i = left;
var j = right;
var pivot = data[Math.floor(left + (right - left) / 2)];
while (i <= j) {
while (data[j] > pivot) {
j--;
}
while (data[i] < pivot) {
i++;
}
if (i <= j) {
var temp = data[i];
data[i] = data[j];
data[j] = temp;
i++;
j--;
}
}
return i;
}
function binarySearch(data, target) {
var left = 0;
var right = data.length - 1;
while(left <= right)
{
var midIndex = Math.floor(left + (right - left) / 2);
if(data[midIndex] < target)
{
left = midIndex + 1;
}
if(data[midIndex] >= target)
{
right = midIndex - 1;
}
}
return left;
}