CS173: Intro to Computer Science - Insertion Sort (3 Points)

Developed by Professor Tralie and Professor Mongan.

Exercise Goals

The goals of this exercise are:
  1. To swap two variables in memory
  2. To implement the insertion sort algorithm
Please fill in the swap method to swap arr[i] with arr[j]. Below is some of the code we wrote in the video to setup random arrays, as well as skeleton code for insertion sort. The main method fills in a random array, prints that array, calls the insertionSort method, and then prints the result. Once this method works properly, your insertion sort code will properly sort the array.
Welcome to our online modules system! Be sure to log in with your Urinus ID before you proceed. For example, Professor Tralie's ID is ctralie. If you are not an Ursinus student, that's fine! Just make something up, and you can still run everything

                    



This exercise [1] was developed by Prof. Tralie.

Hints

  • If you just say arr[i] = arr[j], then that will do half of the swap, but now you’ve overwritten what arr[i] used to be, so you’ve lost the information you need to update arr[j]. Try creating a temporary variable to store the value in arr[i] before you overwite it with what’s in arr[j].
  1. Developed by Prof. Chris Tralie