Sorting Algorithms: Pseudocode

    Sorting Algorithms: Pseudocode

      Pseudocode—code that's written for humans, not machines—is a pretty broad category, but to keep everyone on the same page, we're going to use some standard conventions when writing about sorting algorithms.

      Why use pseudocode? Because it lets us focus exclusively on the steps of the algorithm, so we don't need to worry about things like setting variables or semicolons.

      Specifically, we need to know what

      • loops, a.k.a. the lines that let you repeat yourself without copying and pasting code
      • control statements, a.k.a. the lines that keep your code from running when it shouldn't
      • lists, a.k.a. the places that hold all your data

      look like.

      First things first: key words that show the main function of a line of code are always capitalized. Here are some of the most common ones:

      • FOR
      • IF-ELSE
      • DO
      • WHILE
      • BEGIN
      • END

      Other things to know?

      • Things like IF statements and loops use tabs and colons to show scope and nesting. Get your indent on.
      • Pseudocode is never something that the computer can actually read. Ever. Any time you want to turn an algorithm into code, you'll have to turn it into a real, live programming language.

      Now that that's out of the way, let's look at some real-live pseudocode. A basic function looks something like this:

      DEFINITION elementInList( List L, searchValue)
      	//Tells you whether or not the variable searchValue's in List L
      FOR each element in L:
      		IF element equals searchValue:
      			RETURN True
      		ELSE:
      			DO continue
      	END FOR
      	RETURN False

      This function's steps are outlined just like how they would be in code, but they take out all the language-specific syntax and semantics. And that's the key to a good piece of pseudocode: it's detailed enough to describe the algorithm and abstracted enough to be easily read.

      Unless, you know, unreadable code's your jam.