Reputation: 391596
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:
i ← j-1
when A[i+1] = key
? That is, why ←
in some cases, and =
in another? Note that in the above code, the ←
is used for the latter too, but in the handouts, available on the web, =
is used, is this simply a typo? (I assume so)do key ← A[j]
when i ← j-1
? What is so special that it requires a do
command like that, and an indentation?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
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