Ad
  • Custom User Avatar

    Did you have the using System; statement?

  • Custom User Avatar

    Was it a build or runtime error? What was the specific error?

  • Custom User Avatar

    Reasons you may have been downvoted include:

    1. This is a 7 kyu kata marked Fundamentals and Arrays; it is not marked for Optimization.

    2. "Avoid premature optimization"; this code is very easy to understand and therefore should be very easy to maintain.

    3. A good sorting algorithm will be O(n ln(n)) versus the O(n) of going through the list once. Yes, sorting is expected to take longer but is it significant?

    I wrote a program that
    (a) generates N random numbers,
    (b) solves by a minimum selection method, and
    (c) solves by sorting the array.

    For N = 2,000,000 sorting the array was taking about 1/4 second (versus perhaps 30 ms for the other method). Unless I'm looking to run this a lot of times in an outer loop, that time isn't worth fussing over. AND, if I am running it many times I would first ask whether this is important for the overall program. If those 2,000,000 values are coming from a slow hard drive the program will appear slow with either algorithm.

    If you're interested, at N = 20,000,000 the sorting took about 2 seconds (versus 80 ms). Here I would look at whether to optimize. Further, it took me a while to find my typo in the faster algorithm, which supports the maintainability aspect mentioned earlier.

  • Custom User Avatar

    Can you provide an example of such a number?

    I had a similar first impression, but thinking it through I changed my mind. However, I have not yet proved it to myself.