Software Engineering Stack Exchange is a question and answer site for professionals, academics, and students working within the systems development life cycle. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I've got decent programming experience for an undergraduate. I just started the study of algorithms. I read the description and saw visual examples that make perfect sense in my head. But when it comes time to write them down in code, I'm unable to implement them. I'm talking about basic algorithms like insertion sort. I'm starting to feel that I'm just stupid.

I love programming and the study of algorithms actually really fascinates me even though I haven't really gone into too much depth. But not being able to implement simple algorithms has really stopped me from getting past the beginner stage.

I'm starting with Intro to Algorithm books/tutorials so I don't see how I go down a level. Is there anything else one can do while in this situation?

share|improve this question

closed as off-topic by gnat, Bart van Ingen Schenau, Doc Brown, amon, superM Mar 14 '14 at 10:19

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions seeking career or education advice are off topic here. They are only meaningful to the asker and do not generate lasting value for the broader community. Furthermore, in most cases, any answer is going to be a subjective opinion that may not take into account all the nuances of a (your) particular circumstance." – gnat, Bart van Ingen Schenau, Doc Brown, amon, superM
If this question can be reworded to fit the rules in the help center, please edit the question.

    
How many years of experience writing code do you have? This reminds me of the challenge that someone who has read hundreds of books has difficulty writing one. – JB King Mar 14 '14 at 7:00
    
Roughly four years (1 year in high school + 3 in college) on and off. I've got all the prerequisites one would think needed to learn basic algorithms. – An Alien Mar 14 '14 at 7:24
    
1  
Actually, I don't believe you have really four years of experience in coding. You might have started four years ago to code, but not able to implement insertion sort is just a lack of practical experience. I suggest you start solving the first 50 problems of projecteuler.net - take yourself at least one or two hours to solve that before you google for a solution. The more programs you write, the easier it will become to implement algorithms. – Doc Brown Mar 14 '14 at 8:18
up vote 0 down vote accepted

The process for implementing algorithms is like trying to explain a complex subject to a small child - in this case, the child is the machine. You must understand to concept of the algorithm yourself, and then start to break it into parts, then do it again, and again, until you are left with instructions so simple that they can be written as an instruction in code.

Let's take, for example, an Insertion Algorithm:

  1. Suppose there exists a function called Insert designed to insert a value into a sorted sequence at the beginning of an array. It operates by beginning at the end of the sequence and shifting each element one place to the right until a suitable position is found for the new element. The function has the side effect of overwriting the value stored immediately after the sorted sequence in the array.
  2. To perform an insertion sort, begin at the left-most element of the array and invoke Insert to insert each element encountered into its correct position. The ordered sequence into which the element is inserted is stored at the beginning of the array in the set of indices already examined. Each insertion overwrites a single value: the value being inserted.

Lets take the first step - it describes a function called Insert, so it means we'll need a function called Insert. That's simple enough. What should it do? lets write it down (simply copying the sentence above, breaking it into steps):

function Insert(array, new_item)
  1. begin at the end of the sequence
  2. shift each element one place to the right until a suitable position is found for the new element
end

Step 1 is simple enough, but step 2 is still a little complex - let's break it down: it describes a loop ("each element"), an operation ("shift one place to the right"), and a stop condition ("until a suitable position is found")

2'. each element
  2'a. shift one place to the right
  2'b. until a suitable position is found

or, if we want to turn it into a condition within a loop, we'll write it like:

2''. is element larger than new_item?
  yes -> a. shift one place to the right
         b. go to previous element
  no -> a. insert new_item in next position
        b. break

starting to look like code, right? Let's try to write code to match what we wrote:

function Insert(array, new_item)
  current_index = array.last
  while (true)
    if(array[current_index] > new_item)
      array[current_index+1] = array[current_index]
      current_index = current_index-1
    else
      array[current_index+1] = new_item
      return
    end
  end
end

This is the first step! This code (almost) works - now you switch hats to a developer, and ask yourself - are all the edge conditions handled (no - what if all elements are larger than the new_item?), is this the proper way to handle an array (only if it has more positions then elements - otherwise the first element will "fall off" the array...), how do I need to enhance its functionality to accommodate the next step of the algorithm? Can I refactor it to look/work better, and do the same thing?

function Insert(array, last_item_index, new_item)
  if (last_item_index >= array.length) raise exception
  for (current_index = last_item_index; current_index >= 0; current_index--)
    if(array[current_index] > new_item)
      array[current_index+1] = array[current_index]
    else
      break
    end
  end
  array[current_index+1] = new_item
end

(this is, of course, all written in pseudocode. your implementation will be according to your language of choice, its idioms, capabilities and conventions)

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.