Lasse V. Karlsen
Lasse V. Karlsen

Reputation: 391596

Pseudo-code from some MIT courseware

I've never had much need for writing large quantities of formal pseudo-code but the need has arisen, so I thought I'd pick some standards in order to stay consistent across code.

To that effect I picked up some "iTunes U" courseware videos, amongst other things the 6.046J / 18.410J Introduction to Algorithms (SMA 5503).

In the very first lecture video, the lecturer writes Insertion Sort on the blackboard, and he writes this:

Insertion-Sort(A, N) // Sorts A[1..n]
  for j ← 2 to n
    do key ← A[j]
      i ← j-1
      while i > 0 and A[i] > key
        do A[i+1] ← A[i]
          i ← i-1
      A[i+1] ← key

So, my questions:

In other words, why isn't the above pseudo-code written like this (with my highlights):

Insertion-Sort(A, N) // Sorts A[1..n]
  for j ← 2 to n
    key ← A[j]                  <-- lost the do here
    i ← j-1                     <-- no indentation
    while i > 0 and A[i] > key
      A[i+1] ← A[i]             <-- lost the do here
      i ← i-1                   <-- no indentation
    A[i+1] ← key

Final question: Does anyone have a code standard for pseudo-code handy somewhere? My main goal is consistency, so that I only have to "teach" the recipients once.

Upvotes: 0

Views: 910

Answers (2)

user2748680
user2748680

Reputation: 1

the arrow serve as = in normal code.

equal sign in pseudo serve as == in normal code

so j <- 1 mean j = 1

and j = 1 mean if( j == 1)

Upvotes: 0

Will
Will

Reputation: 75673

Structured English is a 'standardised' pseudo-code language.

Upvotes: 1

Related Questions