Last active
February 12, 2018 22:12
-
-
Save thomasvaeth/f5c4874682304f2cfbe77ef8d38f2c0b to your computer and use it in GitHub Desktop.
"Curiosity will conquer fear even more than bravery will." - James Stephens
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function productArrayValues(arr1, arr2) { | |
return quickSort(arr1, 0, arr1.length - 1)[arr1.length - 1] * quickSort(arr2, 0, arr2.length - 1)[0]; | |
} | |
function quickSort(arr, left, right) { | |
if (arr.length < 2) return arr; | |
var pivot, partitionIndex; | |
if (left < right) { | |
pivot = right; | |
partitionIndex = partition(arr, pivot, left, right); | |
quickSort(arr, left, partitionIndex - 1); | |
quickSort(arr, partitionIndex + 1, right); | |
} | |
return arr; | |
} | |
function partition(arr, pivot, left, right) { | |
var pivotValue = arr[pivot], | |
partitionIndex = left; | |
for (var i = left; i < right; i++) { | |
if (arr[i] < pivotValue) { | |
swap(arr, i, partitionIndex); | |
partitionIndex++; | |
} | |
} | |
swap(arr, right, partitionIndex); | |
return partitionIndex; | |
} | |
function swap(arr, i, j) { | |
var temp = arr[i]; | |
arr[i] = arr[j]; | |
arr[j] = temp; | |
} | |
productArrayValues([6, 5, 4], [3, 2, 1]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment