![]() |
comparing integer lists
I have two small arrays of integers. (I don't expect more than 16 elements in each array) No integer occurs more than once in an array. Integers are sorted from least to greatest, though they are not necessarily consecutive.
What's an efficient way to diff them, so I get the elements in each array that aren't in the other? (I do want two resulting arrays, for the elements that were only in array 1, and only in array 2) The simple way I thought of was to iterate through the first away searching for matching elements in the second array. If one is found, both elements are deleted. The remaining arrays are the elements that were only in each array. |
Re: comparing integer lists
Is this LabView, Java or C++??
In Java (the only language I know), I would use nested for loops that would compare an element in the first array wiht each element in the second array. An example is below, where any number that is found in the other array is changed to -1. Code:
public class ArraySort |
Re: comparing integer lists
Say the arrays are labled 'A' and 'B'. Just two pointers 'a' and 'b' (pointing in their respective arrays). Without loss of generality, say that 'B' starts with a larger element. Keep on advancing 'a' until *a < *b is no longer true. As these arrays are sorted, these values must be unique to A. Now advance 'b' in a similar manner. In the case where *a == *b, advance both pointers. Don't output the element as it exists in both.
Alternately, add all elements of A into a hashtable. Now iterate through B outputting all elements that are not in the hashtable. Both algorithms have the same asympotic complexity but the first doesn't require any extra memory and should run a bit faster. Here is algorithm 1 Code:
#include <stdio.h> |
Re: comparing integer lists
Quote:
Leveraging the facts that a) the arrays are already sorted and b) there are no repetitions in the individual arrays, you can use a stack-matching algorithm. This will run in O(max(n,m)) time instead of the naive methods which will run O(n*m) for arrays sizes n and m. Here's implementations in C and Java. Code:
#include <stdlib.h>Code:
import java.util.Arrays;EDIT: Just read [Chris27]'s post again. I think I'm doing the same thing he is, just entirely iteratively instead of the combination iterative/recursive approach he took. --Ryan |
Re: comparing integer lists
My solution isn't really recursive as a modern compiler will turn the tail recursion into iterative code.
It could be rewritten as Code:
void mydiff(int * arr1, int * end1, int * arr2, int * end2, int * diff1, int * diff2) { |
Re: comparing integer lists
You already have some good ideas, and as stated, 16 elements is nothing, but I'll point out that the task is essentially the same as merging sorted tapes. So now you can look it up in some of the classic algo books if you like.
Greg McKaskle |
Re: comparing integer lists
Quote:
I have Knuth's 3-volume set here in the library. I had a copy of Sedgewick years ago but can't find it. Must have loaned it out without a homing device. |
| All times are GMT -5. The time now is 03:26. |
Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi