There are a lot of posts on the easiest way to find duplicates in two arrays, but what is absolute fastest way? Is there a way to avoid using two for loops and get the function from O(n^2) time to O(n) time? The arrays I have contain ~1000 items each. Before running the function I check which array is longer and use that array as the toCheckAgainst variable.
var containingBoth = [];
function checkArrays(toCheck, toCheckAgainst){
for(var i=0;i<toCheck.length;i+=1){
for(var j=0;j<toCheckAgainst.length;j+=1){
if (toCheck[i] === toCheckAgainst[j]) {
containingBoth.push(toCheck[i]);
}
}
}
}
This can be the another way to achieve this.Your current solution have the looping on both of the arrays and looping on the longest one which slower down the process in large arrays e.g if one longest array has 1000 vals and shorter array has only 2 in that case you are looping 1000 values to find out the dupes which can not be more than 2 in that case.
Below solution loop is only on the array which is shorter than the other array because we are only interested in dupes and even if both arrays are of same length below solution is faster because in your solution you are looping on both arrays whereas below code just loop on one.
Did some testing on comparing difference in speed in Niles code and mine see the results here Niles Code and my code and its approximately 50% faster with the arrays of 1000 values each
var arr1 = ["Test1", "test2", "test3","test5","test6","test4"];
var arr2 = ["test1", "test4","test5","test6","test2","Test1"];
var result = [];
(arr1.length>arr2.length?arr2:arr1).forEach(function(key) {
if (-1 != (arr1.length>arr2.length?arr1:arr2).indexOf(key) && -1 == result.indexOf(key))
result.push(key);
}, this);
alert(result); //duplicate values arr
Edited to add example code
You can do it in O(n) if both arrays--with lengths "k" and "l" respectively--are already sorted. You can do it by merging the two arrays (not in memory, of course, just algorithmically) like in merge-sort with k+l comparisons.
If both arrays are not sorted at all you can do it in O(n log n) (e.g. with the merge-sort mentioned above) with O(n²) comparisons and because you can do your task with brute force in O(n²) already, sorting the arrays would be redundant here.
But not all sets are completely unsorted, especially if they are big. If we take quicksort as an example you can expect O(n log n) on average with O(n log n) comparisons. But be aware that the worst case of quicksort is O(n²) when the set is already sorted!
A very simple way if your arrays are sorted and contain no duplicates:
Take the individual entries of the smaller array and do a binary search for them in the larger array -> O(n log n) (binary search is O(log n) and you do it n times). The smaller array does not even have to be sorted if it is guaranteed that it contains no duplicates.
Summary: you can do it faster than O(n²). It depends on the input how fast "faster" actually gets but you can get it down to O(n) (plus a small constant) in the best case and O(n log n) on average.
'use strict'
var primesieve;
var buffer;
var primelimit;
function clear(where) {
primesieve[where >>> 5] &= ~((1 << (31 - (where & 31))));
}
function get(where) {
return ((primesieve[where >>> 5] >>> ((31 - (where & 31)))) &
1);
}
function nextset(from) {
while (from < primelimit && !get(from)) {
from++;
}
if (from === primelimit && !get(from)) {
return - 1;
}
return from;
}
function fillsieve(n) {
var k,
r,
j;
n = n + 1;
primelimit = n - 1;
k = Math.ceil(n / 32);
if (typeof ArrayBuffer !== 'function') {
buffer = new ArrayBuffer(k * 4);
} else {
buffer = k;
}
primesieve = new Uint32Array(buffer);
while (k--) {
primesieve[k] = 0xffffffff;
}
clear(0);
clear(1);
for (k = 4; k < n; k += 2) {
clear(k);
}
r = Math.floor(Math.sqrt(n));
k = 0;
while (k < n) {
k = nextset(k + 1);
if (k > r || k < 0) {
break;
}
for (j = k * k; j < n; j += 2 * k) {
clear(j);
}
}
}
function approx_limit(prime_pi) {
if (prime_pi < 10) {
return 30;
}
// see first term of expansion of li(x)-li(2)
return Math.ceil(prime_pi * (Math.log(prime_pi * Math.log(prime_pi))));
}
function primes(prime) {
var ret,
k,
count,
i;
ret = [];
k = 0;
i = 0;
count = prime;
while (count--) {
k = nextset(k + 1);
if (k > primelimit || k < 0) {
break;
}
ret[i++] = k;
}
return ret;
}
// very simple binary search
Array.prototype.bsearch = function(needle) {
var mid, lo = 0;
var hi = this.length - 1;
while (lo <= hi) {
mid = Math.floor((lo + hi) / 2);
if (this[mid] > needle) {
hi = mid - 1;
} else if (this[mid] < needle) {
lo = mid + 1;
} else {
return this[mid];
}
}
// assumes no entry "-1", of course
return -1;
};
var limit = 10 * 1000;
var a, b, b_sorted, u;
var a_length, b_length, u_length;
fillsieve(approx_limit(limit));
// the first array is filled with primes, sorted and unique
a = primes(limit);
// the second array gets filled with an unsorted amount of
// integers between the limits 0 (zero) and "limit".
b = [];
for(var i = 0;i < limit;i++){
b[i] = Math.floor( Math.random() * (limit + 1));
}
a_length = a.length;
b_length = b.length;
console.log("Length of array a: " + a_length);
console.log("Length of array b: " + b_length);
var start, stop;
u = [];
// Brute-force
start = performance.now();
for(var i = 0; i < a_length; i++){
for(var j = 0; j< b_length; j++){
if(a[i] == b[j] && a[i] != u[u.length - 1]){
u.push(a[i]);
}
}
}
stop = performance.now();
console.log("Brute force = " + (stop - start));
console.log("u-length = " + u.length);
console.log(u.join(","));
u = [];
b_sorted = [];
// work on copy
for(var i = 0; i < b_length; i++) b_sorted[i] = b[i];
var entry;
// Sort the unsorted array first, than do a binary search
start = performance.now();
// ECMA-script's arrays sort() function sorts lexically
b_sorted.sort(function(a,b){return a - b;});
for(var i = 0; i < a_length; i++){
entry = b_sorted.bsearch(a[i])
if( entry != -1 && entry != u[u.length - 1]){
u.push(entry);
}
}
stop = performance.now();
console.log("Binary search = " + (stop - start));
console.log("u-length = " + u.length);
console.log(u.join(","));
This runs on my little AMD A8-6600K in around 56 seconds with the brute force algorithm and in about 40 milliseconds (yes, milliseconds!) with the binary search and that number even includes the sorting of the array.
Related
I'm currently working on a programming question:
Given a string s and an integer k, reverse the first k characters for every 2k characters counting from the start of the string.
If there are fewer than k characters left, reverse all of them. If there are less than 2k but greater than or equal to k characters, then reverse the first k characters and leave the other as original.
I created a program that solves the first 45 of the 60 test cases, but apparently falls apart on really long strings. When fed strings of 999 characters, the last few came out as nonsense.
I cannot see any fault in my code that may have caused that. Any feedback? Simple solutions or just better ways of constructing my code?
function reverseArrayOfChars(sArray) {
const length = sArray.length;
let temp;
for (let s = 0; s < length / 2; s++) {
temp = sArray[s];
sArray[s] = sArray[length - 1 - s];
sArray[length - 1 - s] = temp;
}
return sArray;
}
function reverseStr(s, k) {
let sArray = s.split("");
let newArray = []; //Final array to be returned
let tempArray = []; //tempArray is used to store returns from reverseArrayOfChars function. These returns are then concatenated onto newArray.
let switchBoard = 1; //Used to 'switch' between two conditions. Changes automatically every iteration of the loop.
for (let counter = 0; counter < sArray.length; counter += k) {
switchBoard = switchBoard === 0 ? 1 : 0;
if (sArray.length - counter < k) {
tempArray = reverseArrayOfChars(sArray.slice(counter));
newArray = newArray.concat(tempArray);
break;
} else if (sArray.length - counter > k && sArray.length < k * 2) {
tempArray = reverseArrayOfChars(sArray.slice(counter, counter + k));
newArray = newArray.concat(tempArray);
tempArray = sArray.slice(counter + k);
newArray = newArray.concat(tempArray);
break;
} else if (switchBoard === 0) {
tempArray = reverseArrayOfChars(sArray.slice(counter, counter + k));
newArray = newArray.concat(tempArray);
} else if (switchBoard === 1) {
tempArray = sArray.slice(counter, counter + k);
newArray = newArray.concat(tempArray);
}
}
return newArray.join("");
Alternatively, you could try this sample code:
var reverseStr = function(s, k) {
if (k > s.length)
return s.split('').reverse().join('');
const split = s.split('');
// reverse the seg. then join it back
for (let i = 0; i < s.length; i += 2*k) {
const reverse = split.splice(i, k).reverse();
split.splice(i, 0, ...reverse);
}
return split.join('');
};
I implemented the Largest Triple Products algorithm, but I use sort which makes my time complexity O(nlogn). Is there a way to implement it without a temporary sorted array?
The problem:
You're given a list of n integers arr[0..(n-1)]. You must compute a list output[0..(n-1)] such that, for each index i (between 0 and n-1, inclusive), output[i] is equal to the product of the three largest elements out of arr[0..i] (or equal to -1 if i < 2, as arr[0..i] then includes fewer than three elements).
Note that the three largest elements used to form any product may have the same values as one another, but they must be at different indices in arr.
Example:
var arr_2 = [2, 4, 7, 1, 5, 3];
var expected_2 = [-1, -1, 56, 56, 140, 140];
My solution:
function findMaxProduct(arr) {
// Write your code here
if(!arr || arr.length === 0) return [];
let helper = arr.slice();
helper.sort((a,b)=>a-b); // THIS IS THE SORT
let ans = [];
let prod = 1;
for(let i=0; i<arr.length; i++) {
if(i < 2) {
prod *= arr[i];
ans.push(-1);
}
else {
if(i === 3) {
prod *= arr[i];
ans.push(prod);
} else if(arr[i] < helper[0]) {
ans.push(prod);
} else {
const min = helper.shift();
prod /= min;
prod *= arr[i];
ans.push(prod);
}
}
}
return ans;
}
Thanks
You don't need to sort it. You just maintain an array of the largest three elements at each index.
For the first three elements it is simple you just assign the product of them to the third element in the result.
For the next elements, you add the current element to the three-largest-element-array and sort it and take the elements from 1 to 3 ( the largest three ) and assign the product of those at that index in result array. Then update the three-element-array with largest three.
Complexity :
This sort and slice of three-element-array should be O(1) because each time atmost 4 elements are there in the array.
Overall complexity is O(n).
You can do it as follows :
function findMaxProduct(arr) {
if(!arr) return [];
if (arr.length < 3) return arr.slice().fill(-1)
let t = arr.slice(0,3)
let ans = arr.slice().fill(-1,0,2) //fill first two with -1
ans[2] = t[0]*t[1]*t[2];
for(let i=3; i<arr.length; i++) {
t.push(arr[i]);
t = t.sort().slice(1,4);
ans[i] = t[0]*t[1]*t[2];
}
return ans;
}
I am keeping the array ordered (manually). Then just get the first 3 elements.
function findMaxProduct(arr) {
let results = [];
let heap = [];
for (let i = 0; i < arr.length; i++) {
// Insert the new element in the correct position
for (let j = 0; j < heap.length; j++) {
if (arr[i] >= heap[j]) {
heap.splice(j, 0, arr[i]);
break;
}
}
// No position found, insert at the end
if (heap.length != i + 1) {
heap.push(arr[i]);
}
if (i < 2) {
results.push(-1);
} else {
results.push(heap[0] * heap[1] * heap[2]);
}
}
return results;
}
You can make an array that holds three currently largest integers, and update that array as you passing through original array. That's how you will always have three currently largest numbers and you will be able to solve this with O(n) time complexity.
I think there's a faster and more efficient way to go about this. This is a similar thought process as #Q2Learn, using Python; just faster:
def findMaxProduct(arr):
#create a copy of arr
solution = arr.copy()
# make first 2 elements -1
for i in range(0,2):
solution[i] = -1
#for each item in copy starting from index 2, multiply item from 2 indices b'4 (notice how each index of arr being multiplied is reduced by 2, 1 and then 0, to accommodate each move)
for i in range(2, len(arr)):
solution[i] = arr[i-2] * arr[i-1] * arr[i]
return solution
check = findMaxProduct(arr)
print(check)
Single Scan Algorithm O(n)
We don't need to necessarily sort the given array to find the maximum product. Instead, we can only find the three largest values (x, y, z) in the given stage of iteration:
JavaScript:
function findMaxProduct(arr) {
let reults = []
let x = 0
let y = 0
let z = 0
for(let i=0; i<arr.length; i++) {
n = arr[i]
if (n > x) {
z = y
y = x
x = n
}
if (n < x && n > y) {
z = y
y = n
}
if (n < y && n > z) {
z = n
}
ans = x*y*z
if (ans === 0) {
results.push(-1)
} else {
results.push(ans)
}
return ans;
}
Python:
def findMaxProduct(arr):
results = []
if not arr:
return []
x = 0
y = 0
z = 0
for i, n in enumerate(arr):
if n > x:
z = y
y = x
x = n
if n < x and n > y:
z = y
y = n
if n < y and n > z:
z = n
ans = x*y*z
if ans == 0:
results.append(-1)
else:
results.append(ans)
print(results)
public int[] LargestTripleProducts(int[] input)
{
var ansArr = new int[input.Length];
var firstLargetst = input[0];
var secondLargetst = input[1];
ansArr[0] = ansArr[1] = -1;
for (int i = 2; i < input.Length; i++)
{
ansArr[i] = firstLargetst * secondLargetst * input[i];
if (firstLargetst < input[i] && firstLargetst < secondLargetst)
{
firstLargetst= input[i];
continue;
}
if (secondLargetst < input[i] && secondLargetst < firstLargetst)
{
secondLargetst= input[i];
}
}
return ansArr;
}
Python solution based on #SomeDude answer above. See explanation there.
def findMaxProduct(arr):
if not arr:
return None
if len(arr) < 3:
for i in range(len(arr)):
arr[i] = -1
return arr
three_largest_elem = arr[0:3]
answer = arr.copy()
for i in range(0, 2):
answer[i] = -1
answer[2] = three_largest_elem[0] * three_largest_elem[1] * three_largest_elem[2]
for i in range(3, len(arr)):
three_largest_elem.append(arr[i])
three_largest_elem = sorted(three_largest_elem)
three_largest_elem = three_largest_elem[1:4]
answer[i] = three_largest_elem[0] * three_largest_elem[1] * three_largest_elem[2]
return answer #Time: O(1) n <= 4, to Overall O(n) | Space: O(1)
Python has it's in-built package heapq, look at it for it.
Credit: Martin
> Helper function for any type of calculations
import math
> Heap algorithm
import heapq
> Create empty list to append output values
output = []
def findMaxProduct(arr):
out = []
h = []
for e in arr:
heapq.heappush(h, e)
if len(h) < 3:
out.append(-1)
else:
if len(h) > 3:
heapq.heappop(h)
out.append(h[0] * h[1] * h[2])
return out
Hope this helps!
We have a input of array having n numbers (non-zero) i.e.
input--> [a1, a2, a3, a4, a5, ...., aN] (not in order)
Now we want the output as
output--> Ai <= Aj >= Ak <= Al >= Am <= ....An.
i have written a code for this problem and it is quite fine but not optimized if i talk about time and space complexity then it must not be good solution.
function test(array){
if(array && array.length){
let newArray = [];
array = array.sort();
let m;
if(array.length % 2){
m = (array.length +1 )/2;
}else{
m = (array.length)/2;
}
newArray.push(array[m-1]);
for(let i=1;i<=m;i++){
if(array[m-1+i])
newArray.push(array[m-1+i]);
if(array[m-1-i])
newArray.push(array[m-1-i]);
}
console.log(newArray);
return newArray;
}else{
throw 'invalid argument';
}
}
test([1,2,3,4]);
Please help me if you any idea to optimise like using no other variables(it will reduce space complexity). Thanks
You don't need to sort the array.
Just do one pass over the array, and in each step fix only a[i], a[i+1].
Suppose a[1] <= a[2] >= a[3]...<= a[i-1] >= a[i]
Now, if a[i] <= a[i+1], continue by increasing i.
if a[i] > a[i+1], swap them.
Symmetrically, when i is even, if a[i] < a[i+1] swap a[i],a[i+1].
Your current algorithm runs on O(n log n) time. The .sort() at the beginning takes O(n log n) time, while the for loop below it runs in O(n) time. So, one possible improvement would be to change the sort function to use counting sort instead (which has time complexity O(n + k), where k is the range from the lowest number to the highest number). This will reduce the overall time complexity from O(n log n) to O(n + k), which will be a significant improvement with larger sets:
function countingSort(array) {
const counts = array.reduce((a, num) => {
a[num] = (a[num] || 0) + 1;
return a;
}, {});
/*
ok, maybe this is cheating
return Object.entries(counts).reduce((arr, [num, occurrences]) => {
for (let i = 0; i < occurrences; i++) {
arr.push(Number(num));
}
return arr;
}, []);
*/
const sorted = [];
const min = Math.min(...array);
const max = Math.max(...array);
for (let i = min; i <= max; i++) {
for (let j = 0; j < counts[i]; j++) {
sorted.push(i);
}
}
return sorted;
}
function test(array){
if(array && array.length){
let newArray = [];
array = countingSort(array);
let m;
if(array.length % 2){
m = (array.length +1 )/2;
}else{
m = (array.length)/2;
}
newArray.push(array[m-1]);
for(let i=1;i<=m;i++){
if(array[m-1+i])
newArray.push(array[m-1+i]);
if(array[m-1-i])
newArray.push(array[m-1-i]);
}
console.log(newArray);
return newArray;
}else{
throw 'invalid argument';
}
}
test([1,2,3,4]);
Counting sort performs best when there aren't large gaps between the numbers. If there are large gaps between the numbers, you could use radix sort instead, which also runs in linear time (but is a bit more complicated to code).
(if you do use the built-in sort function, though, make sure to sort numerically, with .sort((a, b) => a - b), otherwise your resulting array will be sorted lexiographically, eg [1, 11, 2], which is not desirable)
This could be a better logic finally :-
function optimizedSort(array) {
if(array && array.length){
let temp;
for(let i=0;i<array.length;i++){
if((i+1)%2){
// i is odd
if(array[i]>=array[i+1]){
temp = array[i];
array[i] = array[i+1];
array[i+1] = temp;
}
}else{
// i is even
if(array[i]<=array[i+1]){
temp = array[i];
array[i] = array[i+1];
array[i+1] = temp;
}
}
}
console.log('********',array);
return array;
}else{
throw 'invalid argument';
}
}
optimizedSort([1,2,3,4]);
Today in a interview, I was told to write a program which will output the nth highest number in the unsorted array,
I solved this using javascript, the program is as follows,
var fn50 = function(){
var reverseSort = function(myArray,highest){
var x = 0,
y = 0,
z = 0,
temp = 0,
totalNum = myArray.length, // total numbers in array
flag = false, // is the numbers sorted in reverse while iteration
isAchieved = false; // whether we achieved the nth highest
while(x < totalNum){
y = x + 1; // start comparing 'yth' number which is next to 'xth' number.
if(y < totalNum){
// start comparing 'xth' with the next number, and if 'xth' number less than its next position number, just swipe them
for(z = y; z < totalNum; z++){
if(myArray[x] < myArray[z]){
temp = myArray[z];
myArray[z] = myArray[x];
myArray[x] = temp;
flag = true; // if number swiping done ?
}else{
continue;
}
}
}
if(flag){
flag = false;
}else{
x++; // x holds the max number in series, now move to next position to find next highest number
if(x > highest){ // if x is what the desired max number which we want flag it and break the loop to escape further iteration.
isAchieved = true;
}
}
if(isAchieved){
break;
}
}
print(myArray[(highest - 1)]);
};
reverseSort([12,56,78,34,11,100,95],4); // passing the unsorted array of number's, and finding the 4th highest number
};
fn50();
I got the desired output i.e the answer is 56 from the above array which is the 4th highest number.
But the interviewer told for a better solution.
Can you tell me or give me a hint, how can there be a better solution.
Some data structure technique ?
Sorting and selecting the kth highest number needs O(n log(n)) time, where n is the number of elements. In the bibliography, there is the medians of medians algorithm, that allows us to select the kth highest or smallest in linear time, no matter what value k has. You could find out if the interviewer had this kind of algorithm in mind, if you asked if the desired element could be the median of the array. The median is the element at position n / 2, which is considered the hardest case.
But for an interview, it's a complicated algorithm. If k is in general small, you can apply the following algorithm, based on the structure of a heap. You convert the array into a heap in linear time. Then you extract k times the largest element. This will take O(n + k * log(n)) time, which for small k = ο(n / log(n) is linear.
Having k be as small as a constant number, like 4, has an even simpler linear algorithm. Every time we scan the array and remove the largest. This will take O(k * n) time and because k is constant, O(k * n) = O(n).
I tried to implement this with quickselect as JuniorCompressor suggested.
But I wonder if that is really the fastest possible way. I guess the calculation of the pivot could be made more efficient.
var nthLargest = function(list, n) {
var i, a = 0, b = list.length, m, pivot;
if(n < 1) throw new Error("n too small");
if(list.length < n) throw new Error("n too large");
list = list.slice(0);
var swap = function(list, a, b) {
var temp = list[a];
list[a] = list[b];
list[b] = temp;
}
//returns the index of the first element in the right sublist
var partition = function(list, pivot, a, b) {
b--;
while(a <= b) {
if(list[a] <= pivot) a++;
else if(list[b] > pivot) b--;
else swap(list, a, b);
}
return a;
}
while(b - a > 1) {
for(i = a, pivot = 0; i < b; i++) {
pivot += list[i];
}
pivot /= b-a;
m = partition(list, pivot, a, b);
if(b - m >= n) a = m; // select right sublist
else { // select left sublist
if(m === b) return list[a]; // all elements in sublist are identical
n -= b - m;
b = m;
}
}
if(n !== 1) throw new Error();
return list[a];
}
<script>
function nthlargest(array, highest) {
array.sort();
l=array.length;
if(highest>l)
return("undefined");
else
return(array[l-highest+1]);
}
document.write(nthlargest([23, 652, 43, 89, 23, 90, 99, 88], 2));
</script>
Sorting is the simplest way I can think of.
But it appears you created your own sorting implementation.
Why not use the Array.sort function?
function nthHighest(numbers, n) {
var sorted = numbers.sort(function (a, b) {
return a - b;
});
return sorted[sorted.length - n];
}
You could simplify the arithmetic by doing a reverse sort, which just means b - a instead of a - b, then you don't need to pull from the back, which is just a cosmetic improvement.
function nthHighest(numbers, n) {
var sorted = numbers.sort(function (a, b) {
return b - a;
});
return sorted[n - 1];
}
You could also iterate over the array once, copying each element into a new array in sorted order and again, taking the Nth to last element, using underscore to implement a binary search.
function nthHighest(numbers, n) {
var sorted = [];
numbers.forEach(function (number) {
sorted.splice(_.sortedIndex(sorted, number), 0, number);
});
return sorted[numbers.length - n];
}
But this is basically a spin on the same concept: sort and take N. This approach would also perform better with a linked list than a pure array due to the restructuring, but that can be a separate exercise.
I came up with my own solution as it goes:
const nthlargest = (arr, n) => {
let newArr = [arr[0]];
for (let index = 1; index < arr.length; index++) {
const element = arr[index];
// push to end
if (element > newArr[index - 1]) {
newArr.push(element);
} else {
let insertPos = 0;
// if greater than first and less than last
if (newArr[0] < element && element < newArr[index - 1]) {
for (let j = 0; j < newArr.length; j++) {
if (newArr[j] > element) {
insertPos = j;
}
}
}
//insert at specified pos
newArr.splice(insertPos, 0, element);
}
}
return newArr[n];
}
console.log(nthlargest([43, 56, 23, 89, 88, 90, 99, 652], 4));
// counting from 0
// 89
This is without sorting the original array else it would be much easier.
using the sort array method
function nthLargest(array, n){
array.sort(function(a, b) {
return b - a; //organises the array in descending organises
});
let i = n - 1; //i is the index of the nth largest number
console.log(array[i]);
}
def funcc(arr, n):
max=0
k=0
while (n>0):
n-=1
for i in arr:
if (i>max):
max=i
if(n>0):
arr.remove(max)
max=0
return max
a = [1,2,3,4,19,10,5,11,22,8]
k = funcc(a, 3)
print(k)
const data = [30, 8, 2, 350, 4, 63, 98];
let max = 0;
var nth = 5;
for (let i = 0; i < nth; i++) {
max = 0;
for (let j = 0; j < data.length; j++) {
if (data[j] > max) {
max = data[j];
}
}
var ind = data.indexOf(max);
console.log(data);
const key = data.slice(ind, ind + 1);
}
console.log(nth + " largest number is ", max);
So I'm working on Khan Academy's Algorithms course, and am trying to implement a recursive merge sort in Javascript. Here is my code so far:
var mergeSort = function(array, p, r) {
if(r>p) {
var q = floor(r/2);
mergeSort(array, p, q);
mergeSort(array, q+1, r);
merge(array, p, q, r);
}
};
merge is a function provided by Khan Academy to merge the subarrays back together. It is giving me the error: 'Uncaught RangeError: Maximum call stack size exceeded'.
EDIT: More details: I am fairly sure the error is in my code, there code is purposefully obfuscated and unreadable because the user needs to implement it themselves in a later challenge.
Here is the code that actually calls the mergeSort function initially and declares the array:
var array = [14, 7, 3, 12, 9, 11, 6, 2];
mergeSort(array, 0, array.length-1);
println("Array after sorting: " + array);
Program.assertEqual(array, [2, 3, 6, 7, 9, 11, 12, 14]);
And here is the code for the merge function, although it is obfuscared as I mentioned above:
var merge = function(array, p, q, r) {
var a = [],
b = [],
c = p,
d, e;
for (d = 0; c <= q; d++, c++) {
a[d] = array[c];
}
for (e = 0; c <= r; e++, c++) {
b[e] = array[c];
}
c = p;
for (e = d = 0; d < a.length && e < b.length;) {
if (a[d] < b[e]) {
array[c] = a[d];
d++;
} else {
array[c] = b[e];
e++;
}
c++;
}
for (; d < a.length;) {
array[c] = a[d];
d++;
c++;
}
for (; e < b.length;) {
array[c] = b[e];
e++;
c++;
}
};
They also require my code inside of the mergeSort function be of the form:
if (____) {
var ____ = ____;
mergeSort(____,____,____);
mergeSort(____,____,____);
merge(____,____,____,____);
}
Mergesort is a divide and conquer algorithm which splits the range of indices to sort in two, sorts them separately, and then merges the results.
Therefore, middle variable should be the arithmetic mean of from and to, not the half of to.
I have renamed the variables to make it more understandable:
var mergeSort = function(array, from, to) {
if(to > from) {
var middle = Math.floor( (from+to)/2 ); // Arithmetic mean
mergeSort(array, from, middle);
mergeSort(array, middle+1, to);
merge(array, from, middle, to);
}
};
q is supposed to be the half way point between p and r, but you've failed to take into account that the starting point (i.e. p) might not be 0 when you do this:
var q = floor(r/2);
You need to do something like:
var q = floor((r-p)/2) + p;
Although as #Oriol points out the middle point is actually exactly the same as the arithmetic mean and so the calculation can be simplified.
Just for the sake of implementation of the merge sort in JS
function merge(arr){
if(arr.length <= 1) return arr;
let mid = Math.floor(arr.length/2);
let left = merge( arr.slice(0, mid));
let right = merge(arr.slice(mid))
function mergeSort(arr1, arr2) {
let result = [];
let i=0;
let j=0;
while(i< arr1.length && j < arr2.length) {
if(arr1[i] < arr2[j]){
result.push(arr1[i])
i++;
} else {
result.push(arr2[j])
j++;
}
}
while(i < arr1.length) {
result.push(arr1[i])
i++;
}
while(j < arr2.length){
result.push(arr2[j])
j++;
}
return result
}
return mergeSort(left,right)
}
console.log(merge([1,4,3,6,2,11,100,44]))
High level strategy
the merge sort works on the principle that it's better to sort two numbers than to sort a large list of numbers. So what it does is that it breaks down two lists into their individual numbers then compares them one to the other then building the list back up. Given a list of say 1,3,2, it'll split the list into 1 and 3,2 then compare 3 to 2 to get 2,3 and then compare the list of 2,3 to 1. If 1 is less than the first element in list of 2,3 it simply places 1 in front of the list. Just like that, the list of 1,3,2 is sorted into 1,2,3.
pseudocode-steps
1.take first member of first array
2.compare to first member of second array
3.if first member of first array is less than first member
of second array
4.put first member into sorted array
5.now merge first array minus first element
with second array
6.else take first member of second array
merge first array with remaining portion of second array
7.return sorted array
function mergesorted(list1, list2) {
let sorted = [];
if (list1.length === 0 || list2.length === 0) {
return list1.length === 0 ? list2.length === 0 ? [] : list2 : list1;
}
if (list1[0] < list2[0]) {
sorted.push(list1[0])
return sorted.concat(mergesorted(list1.slice(1), list2))
} else {
sorted.push(list2[0])
return sorted.concat(mergesorted(list1, list2.slice(1)))
}
}
console.log(mergesorted([1, 2], [3, 4])) //should: [1,2,3,4]
console.log(mergesorted([1,2], [3])) //should: [1,2,3]
Merge sort implementation, stable and in place
function sort(arr, start, end) {
if (start >= end-1) return;
var mid = start + ~~((end-start)/2);
// after calling this
// left half and right half are both sorted
sort(arr, start, mid);
sort(arr, mid, end);
/**
* Now we can do the merging
*/
// holding merged array
// size = end-start
// everything here will be copied back to original array
var cache = Array(end-start).fill(0);
var k=mid;
// this is O(n) to arr[start:end]
for (var i=start, r=0;i<mid;r++,i++) {
while (k<end && arr[k] < arr[i]) cache[r++] = arr[k++];
cache[r] = arr[i];
}
// k marks the position of the element in the right half that is bigger than all elements in the left
// effectively it tells that we should only copy start~start+k element from cache to nums
// because the rests are the same
for (var i=0;i<k-start;i++) arr[i+start]=cache[i];
}
A nice solution:
const merge = (left, right) => {
const resArr = [];
let leftIdx = 0;
let rightIdx = 0;
while (leftIdx < left.length && rightIdx < right.length) {
left[leftIdx] < right[rightIdx]
? resArr.push(left[leftIdx++])
: resArr.push(right[rightIdx++]);
}
return [...resArr, ...left.slice(leftIdx), ...right.slice(rightIdx)];
};
const mergeSort = arr =>
arr.length <= 1
? arr
: merge(
mergeSort(arr.slice(0, Math.floor(arr.length / 2))),
mergeSort(arr.slice(Math.floor(arr.length / 2)))
);
try this
const mergeTwoSortedArr = (arr1 = [], arr2 = []) => {
let i = 0,
j = 0,
sortedArr = [];
while(i < arr1.length || j < arr2.length) {
if(arr1[i] <= arr2[j] || (i < arr1.length && j >= arr2.length)) {
sortedArr.push(arr1[i]);
i++;
}
if(arr2[j] <= arr1[i] || (j < arr2.length && i >= arr1.length)) {
sortedArr.push(arr2[j]);
j++;
}
}
return sortedArr;
}
const mergeSort = (arr) => {
if(arr.length === 0 || arr.length === 1) {
return arr;
}
var mid = Math.floor(arr.length / 2);
var left = mergeSort(arr.slice(0, mid));
var right = mergeSort(arr.slice(mid));
return mergeTwoSortedArr(left, right);
}
when trying this
mergeSort([5, 2, 1, 4]) //[1, 2, 4, 5]
Time Complexity O(nlogn)
logn --> decomposition
n comaprison
Time complexity O(nlogn)