Implementing the difference between two sorted Lists of comparable items in java -


implement method in java compute difference () between l1 , l2. l1 \ l2 = { x | x ∈ l1 , x ∉ l2 }.

this implementation far:

public static <anytype extends comparable<? super anytype>>      void difference(list<anytype> l1, list<anytype> l2, list<anytype> difference){          if(l1 == null){             difference = null;         }         else if(l2 == null){             difference = l1;         }         else if(l1.size()==0 || l2.size()==0){             difference = l1;         }         else{             iterator<anytype> it1 =l1.listiterator();             iterator<anytype> it2 =l2.listiterator();              anytype = it1.next();             anytype b = it2.next();              while(true){                 if(a.compareto(b)>0){                     if(it2.hasnext()){                         b = it2.next();                     }                     else{                         difference.add(a);                         while(it1.hasnext()){                             = it1.next();                             difference.add(a);                         }                         break;                     }                 }                 else if(a.compareto(b)<0){                     difference.add(a);                     if(it1.hasnext()){                         = it1.next();                     }                     else break;                 }                 else {                     if(it1.hasnext()){                         =it1.next();                     }                     else break;                 }             }         }         system.out.println("difference set: " + arrays.tostring(difference.toarray()));     } 


this not trivial solution implement 2 nested loops , save result list right elements, solution o(n^2).
another possible solution search second list using binary search, algorithm o(n*log(n))
solution here implemented, attempts go through lists once , finish in first pass, o(n) linear.
the algorithm works fine, feels use optimization still, feels spaghetti code lol. pointers me optimize this?

the first thing see perform a.compareto(b) twice each iteration through loop. instead, use compareto once , store result use in both if statements.

secondly, use consistent naming scheme obja , itera instead of a , it1. it'll make little easier follow. matter, don't afraid of longer names. lista l1 might couple characters, it's worth readability. (also, don't forget shouldn't start variable capital letter, l1 doubly-uncool.)

finally, comment comment comment. know algorithm, can follow code pretty well, it's far self-documenting. comment each if statement , loop document condition is, why you're checking, , you're doing results.


Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -