1

How can I make this sorting algorithm?

modules = {
    2:[4],   # element 2 must be after element 4
    1:[],
    3:[2,4], # element 3 must be after 2 and 3 elements
    4:[1]    # element 4 must be after element 1
}

Result [1, 4, 2, 3] by conditions.

1
  • If this is HW please tag it as such Commented Aug 2, 2011 at 20:10

1 Answer 1

4

That's called a topological sort. Makefiles do it, and there's a standard UNIX utility called tsort that will do it.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.