Algorithm for ordering a set [closed]
I have a set of 50 items and many conditions that specificy which element should come before other.
How to I create a ordered list?
Will like it in C# though can translate it from other languages.
Topological Sort
Translate the "many conditions" into a comparison function, and then use that in conjunction with a comparison-based sort (in the general case).
The best comparison-based sorting algorithms are O(nlogn) in the best case. Merge sort is one such algorithm and is pretty easy to implement... there are many others.
If your conditions constitute a partial ordering (rather than a total ordering), Topological sort might be most appropriate.
There are a number of sorting algorithms you can look into. The two that come to mind off the top of my head are the bubble sort and the quick sort.
精彩评论