Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

how do I translate the following pseudocode to java code:

for k:=0 to (j - i - 1)
{

    a[j-k] :=a[j-k-1]

}

This is part of the insertion sort code from a discrete math book, I'm confused if I should put k++ or k-- or something else as part of the java for loop after the to statement

share|improve this question
    
I posted an answer. However, if your only concern was whether or not k++ or k-- was the issue, it would really just be a matter of trying both and seeing which one works. –  Ricky Mutschlechner 45 mins ago
    
Why downvoting this question? –  usar 33 mins ago
    
I didn't downvote, but this is the sort of beginner-level fundamental question that you should be able to figure out from any Java tutorial. Everybody on here is a volunteer, and you are expected to do basic research and NOT ask questions that can be easily answered 1000 other places on the Internet. Please read the FAQ and How to Ask. SO is not a tutorial site. –  Jim Garrison 10 mins ago

1 Answer 1

The pseudocode:

FOR k := 0 to (j - i - 1)
   a[j - k] := a[j - k - 1]
END FOR

would convert to the Java code:

for (int k = 0; k <= (j - i - 1); k++){
   a[j - k] = a[j - k - 1];
}

(the <= might need to be a <, depending on whether or not "to" implies inclusivity in this case)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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