Wednesday, 21 August 2013

algorithm to compare a sorted array

algorithm to compare a sorted array

i have two sorted arrays. i need to find if both are different r not.
these arrays have elements in a specific range.
more that one element may be different.
arrays can have different sizes.in this case i should be able to point out
the differences
a rough example
say array1: 1 2 4 5 8 9 12 array2: 1 4 8 10 12 13 14
here the range is 1-15
what is the most optimum compare algorithm?
i should be able to point out the differences and similarities tooo.
say 4 is there in both...5 is missing in the second array.
my Solution:
1.two pointer to keep track of the index of the array.
2.point them to the start of the array.
3.start compare the first two elements.
4.if both are equal--> move to the next one. else i.find the largest of
the two elements of the array. say array1 has the larger element.
ii.binary search for the element in the other array.(array2) --> pos of
that element in that array say pos. iii.discard the elements of the array
till pos.
Increment pointers. discard that part of array till this pointers. repeat.
this has a complexity of nlogn. (much less than that.this is when you have
to do a search for every element).

No comments:

Post a Comment