Sorting Algorithms: For Loops

    Sorting Algorithms: For Loops

      When Shmoop was a kid, we loved watching the family classic Paul Blart: Mall Cop. So much that when the credits rolled, we hit "Replay." Entire Saturdays were cut into hour-and-a-half long stretches of an inept man riding a Segway.

      Those were the days.

      Little did we know that we could model our love for mall security with a for loop. Every good movie marathon and loop has three parts:

      • Initialization: when we press play the first time
      • Iteration: every time we re-watch the movie
      • End condition: when it's time to call it a night and go to bed

      In initialization, we create a temporary variable to count the number of times we've gone through the loop. When it's time to axe the loop, we'll know because of that variable.

      Each iteration means that we've gone through the loop one more time. Before iterating, the loop checks the variable to make sure it isn't time to end. Then it makes the variable bigger. Once the variable reaches the end condition, the for loop ends and the program moves on.

      Most for loops look something like this:

      FOR i in range 0 to 100:

      In this case, the temporary variable i is initialized to zero and ends the loop when it reaches 100. Every time the for loop repeats itself, i gets one step closer to 100.

      That's a lot of Vince Vaughn.

      In general, pseudocode assumes that we'll add one to the variable every time we run the loop. In practice, we might want to multiply by five or subtract by two.

      Dream big.

      Let's look at a for loop in Java syntax.

      for(int i = 0; i<5; i++){	//++ is shorthand for "i=i+1"
      	System.out.println("Hit me Baby "+i+" more times.");
      }

      This loop will print out:

      Hit me Baby 0 more times. 
      Hit me Baby 1 more times. 
      Hit me Baby 2 more times. 
      Hit me Baby 3 more times. 
      Hit me Baby 4 more times. 

      We know: thrilling. But trust us, loops get more interesting—and less Britney Spears focused.

      Notice how the code only prints out numbers up to four. Because we check the variable before printing, it never asks Baby to hit it 5 more times.

      Count them, and you'll see that we still have five print lines. That's because we began at zero, which is surprisingly normal in computer science.

      Why?

      …We aren't sure, but we think it has to do with integer division. For instance:

      5÷2 = 2

      If we're dealing with two integers, the result will often be an integer—meaning that most programming languages just cut off everything after the decimal point.

      It could also be because most data structures—the places we store information—start at index zero. So if we have a list with five elements, the indices look like this:

      01234

      Counting from 0 tends to make code nicer. Coders just have to take one for the team on that one.

      Now if you'll excuse us, a certain family-friendly comedy starring a bumbling male in a position of power is calling our name.