var arr = [18, 12, 20, 8, 30, 4];
function bubbleSort(arr) {
var len = arr.length;
for (var i = 0; i < len - 1; i++) {
for (var j = 0; j < len - 1 - i; j++) {
if (arr[j] > arr[j + 1]) {
var temp = arr[j + 1];
arr[j + 1] = arr[j];
arr[j] = temp;
}
}
}
console.log(arr);
return arr;
}
bubbleSort(arr);
var arr = [18, 22, 20, 8, 30, 4];
function quickSort(arr) {
if (arr.length <= 1) {
return arr;
}
var index = Math.floor(Math.random() * (arr.length - 1));
var mid = arr.splice(index, 1);
var left = [];
var right = [];
for (var i = 0; i < arr.length; i++) {
if (arr[i] < mid[0]) {
left.push(arr[i]);
} else {
right.push(arr[i]);
}
}
return quickSort(left).concat(mid, quickSort(right));
}
console.log(quickSort(arr));
quickSort(arr);