## [Repost] What is Code? by Paul Ford

I was recently assigned to read this entire article/essay/magazine in a class of mine and was blown away by how comprehensive it is. If you guys are just beginning to dip your toes into IT or software development, then this is a must-read!

https://www.bloomberg.com/graphics/2015-paul-ford-what-is-code/

## APCS Ch 6: Sorting and Searching Methods

Sorting methods as it pertains to arrays isn’t the simplest thing to wrap your head around but the concept and the way they should work should be pretty intuitive. You would need to know about two main sorting methods in APCS; the selection sort and the insertion sort. Like the different types of loops, both of these sorting methods have pros and cons attached to them.

## Selection Sort

A selection sort is a tedious affair. Basically, what it does is takes each value in each index position and compares that value to all the other values to the right of it (after it) in the array. When it finds the smallest/largest value that exists to the right of its index position, it swaps places with it. In this way, this method sorts all of its value in either ascending or descending order of value.

Let’s take a look at what a selection sort in ascending order looks like in code:

//nums is an array of int values

//the variables min and minIndex both hold int values

for (int i = 0; i < nums.length – 1; i++)

{

min = nums[i];

minIndex = i;

for (int j = i; j < nums.length; j++)

{

if (nums[j] < min)

{

min = nums[j];

minIndex = j;

}

}

int temp = nums[i];

nums[i] = nums[minIndex];

nums[minIndex] = temp;

}

This is a daunting bit of code at first. However, if we understand mechanically how this sorting method works, it might help you understand better. For example, if I had an array of int values, this is what the array would look like after every pass of the outer loop:

(1) The outer loop started at index position 0, took the number 16 at index 0 and started to loop through the values after it to find the smallest number, which happened to be the number 1 at index position 5. Then, the numbers 1 and 16 swapped places due to the inner loop. That was the first pass of the outer loop.

(2) Then, the outer loop looked at index position 1 and picked up the number 3 at index 1 and looped through the values to the right to find the smallest number. Since there were no values to the right that was smaller than 3, the inner loop swapped the 3 at index 1 with 3 at index 1, which meant the 3 swapped with itself and stayed where it is.

This process would repeat until it got to the second to last number by which time, the last number in the array would already be in its final sorted position so the number of passes the outer loop needs to fully sort an array is always the length of the array minus 1. By this rule, if we were on the fourth pass of the outer loop, the first four values of the array would have been in their final sorted places.

Let’s break down the code and see what the first pass of the outer loop looks like:

for (int i = 0; i < nums.length – 1; i++)

The outer loop just goes through the whole length of the array. In the first pass, i would be set to 0.

min = nums[i];

minIndex = i;

This sets min to the number currently at the index position of whatever element the outer loop is currently looking at. So, min would be set to the number at index 0 on the first pass, which would be the number 16. The index of the smallest number would be default set to the index of the number that we’re currently looking at, which would be index 0.

for (int j = i; j < nums.length; j++)

This inner loop would loop through all the index positions to the right of the number at the index position we’re currently looking at, which would be all the values stored in the index positions after index 0.

if (nums[j] < min){

min = nums[j];

minIndex = j; }

This if statement compares the current min value (16) to all the values right of its index position (0). If it finds a number that is smaller, it will set the smaller value to min and set its index position to minIndex. The inner loop would cause this if statement to compare whatever smallest number it has found so far to the numbers in the index positions to the right. When it came to the number 1 at index 5, it set min to 1 and minIndex to 5. After going through the rest of the array, it didn’t find any smaller value so those variables stayed the same and we exit out of the inner loop.

int temp = nums[i];

nums[i] = nums[minIndex];

nums[minIndex] = temp;

This piece of code should look familiar; after all, it should be a simple puzzle that confronts many beginner programmers. Basically, what this does is it swaps the two numbers at the two index positions. In this case, temp would be set to 16, then the index position 1 would be overridden with the value at minIndex(5) which is 1. Then, temp (16) would override what was at index 5, effectively swapping the 16 and 1. This repeats until it has touched all but the last index position in the array, at which point the array is fully sorted and the outer loop terminates.

See next page for Insertion Sort

## Maze Runner: I Made a Game!

TL;DR — Basically, I made a game on code.org for my AP Computer Science Principles (APCSP) class and it’s a maze game and I’ve included the code for it, which can give you a couple of ideas for your own creations and serve as an example, and I’ve also included a video that shows you how it plays and a link for you to play it. There is one big problem with the game code but I’m too lazy to figure it out.

Besides the unoriginal title of this game, I think this little creation of mine is quite decent. There is a problem with the timer compounding every time the game is restarted without the countdown first reaching zero (and a problem which I was too lazy to fix) but otherwise, this simple program is able to execute a game with four screens and multiple game mechanics.

I used AppLab (which uses JavaScript) to make this game and I’ll show you the code used to make it in a second. This was part of the AP project we have to do for AP Computer Science Principles (APCSP) and although I think it’s simplistic considering what you can do on Game Creator, I think it turned out pretty well. In any case, here’s the program code (if you want to use any part of this code in your game, make sure to add a reference to here because otherwise, it is considered plagiarism and the CollegeBoard people doesn’t like plagiarists):

As part of the AP project, I also had to make a video with a voiceover since just looking at the program code won’t easily tell you what the game actually does. I also had to make the video under a minute in length so there were some serious limitations on what I could do and I ended up editing the video in a lot of places. Here it is:

That’s it. If you want to play the game for yourself, click below and from there, you can see also check out the program code. If any of you guys find a way to fix the timer problem, please let me know.

I’ll talk to you later.

Play the game here!