functionmergeSort (arr) { var len = arr.length if (len < 2) { return arr } var middle = Math.floor(len / 2) var left = arr.slice(0, middle) var right = arr.slice(middle) return merge (mergeSort(left), mergeSort(right)); } functionmerge (left, right) { var result = [] while (left.length && right.length) { if (left[0] < right[0]) { result.push(left.shift()) } else { result.push(right.shift()) } } while (left.length) { result.push(left.shift()) } while (right.length) { result.push(right.shift()) } return result } var arr=[3,44,38,5,47,15,36,26,27,2,46,4,19,50,48]; console.log(mergeSort(arr));
functionqSort (arr) { if (arr.length == 0) { return [] } var left = [] var right = [] var pivot = arr[0] for (var i = 1; i < arr.length; i++) { if (arr[i] < pivot) { left.push(arr[i]) } else { right.push(arr[i]) } } return qSort(left).concat(pivot, qSort(right)) } var arr=[3,44,38,5,47,15,36,26,27,2,46,4,19,50,48]; console.log(qSort(arr));