Here's a simple implementation of MergeSort, which is a divide and conquer algorithm that can be used to sort a list in C#:
main.cs803 chars40 lines
This implementation uses recursion to divide the list into two halves, sort them separately, and then merge them back together to produce the final sorted list. The MergeSort function is called repeatedly on each half of the list until the base case (a single-element list) is reached. The Merge function then uses two pointers, i and j, to iterate through the two input arrays, comparing elements and adding them to the output array in order.
To use this implementation, simply call the MergeSort function on your input list:
main.cs110 chars5 lines
This will output the sorted list: 1 2 3 4 5 6 7.
gistlibby LogSnag