Search code examples
c#recursionfactorialdvd

C#: Code to fit LOTS of files onto a DVD as efficiently as possible


I need to write an application that will take a list of files (some large, some small) and fit them onto DVDs (or CDs, or whatever) as efficiently as possible. The whole point of this application is to use up as much of the 1st disc before moving onto the 2nd disc, filling the 2nd disc up as much as possible before moving onto the 3rd disc, etc.

(Note: The application doesn't have to do the actual burning to the DVD, it just has to figure out the best possible fit).

I initially thought I had a good game-plan by generating a permutation of the files and then checking each combination to see what fits the best. (My request for help on this can be found HERE)

But the more files there are, the longer it takes... exponentially. So I wanted some of your opinions on how to best achieve this.

Any ideas? And, as always, C# code is always appreciated.


Solution

  • Simple algorithm:

    1. Sort the file list by file size
    2. Find the largest file smaller than the remaining free space on the DVD, and add it to the DVD.
    3. If the remaining DVD free space is smaller than any remaining files, start a new dvd.
    4. Repeat from 2.