performance - Determing if two lists contain the same numeric items without sorting -


i have 2 lists , need determine if contain same values without sorting (ie. order of values irrelevant). i know sorting work, part of performance critical section.

item values fall within range [-2, 63] , we're comparing equal size lists, list sizes range [1, 8].

example lists:

a = (0,   0, 4, 23, 10) b = (23, 10, 0,  4,  0) c = (0,   0, 4, 27, 10)  == b true == c false 

i think possible solution compare product of 2 lists (multiply values together), there problems solution. 0 , negative numbers. workaround adding 4 every value before multiplying. here's code have far.

bool equal(int a[], int b[], int size) {     int suma = 1;     int sumb = 1;      (int = 0; < size; i++) {         suma *= a[i] + 4;         sumb *= b[i] + 4;     }     return (suma == sumb) } 

but, work no matter order/contents of list were? in other words following mathematically true? i'm asking following (unless there's way solve problem):

given 2 equal sized lists. if products (multiplying values together) of lists equal lists contain same values, long values integers greater 0.

assuming know range ahead of time, can use variation on counting sort. scan through each array , keep track of how many times each integer occurs.

procedure compare-lists(a, b, min, max)   domain := max - min   count := new int[domain]   in a:     count[i - min] += 1   in b:     count[i - min] -= 1     if count[i - min] < 0:       // in b not       return "different"   in a:     if count[i - min] > 0:       // in not b       return "different"   return "same" 

this linear in o(len(a) + len(b))


Comments

Popular posts from this blog

ASP.NET/SQL find the element ID and update database -

jquery - appear modal windows bottom -

c++ - Compiling static TagLib 1.6.3 libraries for Windows -