We propose and study algorithms for computing minimal models, stable models and answer sets of 2- and 3-CNF theories, and normal and disjunctive 2- and 3-programs. We are especially interested in algorithms with non-trivial worst-case performance bounds. We show that one can nd all minimal models of 2-CNF theories and all answer sets of disjunctive 2-programs in time O(m1:4422::n), generalizing a similar result we obtained earlier for computing stable models of normal 2-programs (n is the number of atoms in an input theory or program and m is its size). Our main results concern computing stable models of normal 3-programs, minimal models of 3-CNF theories and answer sets of disjunctive 3-programs. We design algorithms that run in time O(m1:6701::n), in the case of the rst problem, and in time O(mn22:2720::n), in the case of the latter two. All these bounds improve by exponential factors the best algorithms known previously. We also obtain closely related upper bounds on the number of m...