0

I'm implementing a program to sort large files that maybe don't fit in memory. All files will be sorted by lines so I'm thinking in use a List to do it.

I already calculated how many lines I can have in memory to split the files in smaller files, but I don't know how many space I need in memory to sort a List of N elements.

The question is, knowing the maximum number of elements (strings of known size) and the available memory, how much space in memory will the List.Sort method will need?

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
Sheol
  • 164
  • 1
  • 3
  • 13
  • Side note: for future question skip putting thank you notes and upvote/accept useful answers. I.e. in this one in addition to commenting "thank you" on bubble sort suggestion you could upvoted it. – Alexei Levenkov Jul 13 '12 at 15:57
  • I can't, I don't have enough reputation to do that. – Sheol Jul 13 '12 at 15:59

3 Answers3

3

List<T>.Sort uses a quicksort algorithm, which is O(log n) space.

http://en.wikipedia.org/wiki/Quicksort#Space_complexity

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
2

Quick sort is O(logn) space complexity, because it stores constant information (beginning and end of the partition) at every recursive level, and there's at most logn levels.

The merge itself is in-place, as is the bubble sort fallback, so they're O(1) and as such don't count.

Blindy
  • 65,249
  • 10
  • 91
  • 131
2

Do some research: List.Sort() uses the QuickSort algorithm by default, which has O(logn) space complexity.

If you are really worried about space complexity and don't mind a slower algorithm you could use Bubble sort which has O(1) space complexity.

NominSim
  • 8,447
  • 3
  • 28
  • 38
  • I'm not that worried about space complexity, I just needed to know how much space I needed. I will take that in consideration though. Thanks. – Sheol Jul 13 '12 at 15:52