Ad
  • Custom User Avatar

    no i dont think i included that part, thankyou for your help with this. For future reference, if I'm wanting to use a function I know exists but won't work, how do I find what I need to include for it to work?

  • Custom User Avatar

    Did you have the using System; statement?

  • Custom User Avatar

    says "The name 'Array' does not exist in the current context."

    I'm guessing I missed a header or something? I'm quite new to coding so I'm not used to including headers or knowing which ones to have

  • Custom User Avatar

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

  • Custom User Avatar

    i attempted this solution in exactly the same way, yet it threw an error so i had to build the function myself in order for it to work :/

  • Custom User Avatar

    wrap the math in "unchecked{}"

    Edit - nevermind, I read your comment wrong and thought you were talking about an overflow

  • Custom User Avatar

    What I meant with my comment is just pure information: using O(N * logN) sorting is slower and that's it. Maybe I shouldn't have written "horrible".
    In most business applications where a code is not in hot path (read: inside of a tight loop), performance difference like this does not matter, and I never wrote it does.
    So my important point is that my comment never said a sorting solution is bad, I only sad it has worse performance and for many cases it is fine, but I wanted for people to be aware of this.

    To give another perspective for this: in katas on this site, business context are usually not mentioned, so we wouldn't know what constraints should we program to, so we wouldn't know if a solution would be running on a performance-critical system or just a hello world application, so both concise lazy solutions and high-performance solutions and ideas should be supported for the sake of learning.

    People basically downvoted my informative comment which is not lying and giving further information to the learner...

    By the way, thanks for quasi-proving my point.

  • 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

    Writing a reply since people greatly downvoted my comment.
    For those downvoting, I have a small note: compare the runtime performance of a modified minimum selection algorithm and the best sorting algorithm for this scenario...

  • Custom User Avatar
  • Custom User Avatar

    True, but the prompt mentions that no zero or negative values will be sent.

  • 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.

  • Custom User Avatar

    this will throw an index out of bounds exception if the array is just 1 element

  • Custom User Avatar

    Any solution that contains sorting the values is horrible in terms of performance and should be avoided.