CS173: Intro to Computer Science - Sorting Algorithms
Activity GoalsThe goals of this activity are:
- To be able to sort a list using an iterative sorting algorithms (
Supplemental ReadingFeel free to visit these resources for supplemental background reading material.
DirectionsConsider the activity models and answer the questions provided. First reflect on these questions on your own briefly, before discussing and comparing your thoughts with your group. Appoint one member of your group to discuss your findings with the class, and the rest of the group should help that member prepare their response. Answer each question individually from the activity on the Class Activity Questions discussion board. After class, think about the questions in the reflective prompt and respond to those individually in your notebook. Report out on areas of disagreement or items for which you and your group identified alternative approaches. Write down and report out questions you encountered along the way for group discussion.
Consider the array defined in this example: [ 4, 3, 2, 10, 12, 1, 5, 6 ]
- Notice that the pseudocode for InsertionSort begins with
i = 1, not
0. Why might this be?
- After each iteration of the loop, how many elements of
Aare in sorted order? Where are these elements located?
- How many are not yet in sorted order, and where are these located?
- If the inner
jfor loop goes all the way to index
0without finding the key, what do we know about the value we’re inserting in this step?
- What is the purpose of the line
A[j+1] = A[j]inside the inner for loop?
- Describe the execution of this algorithm in your own words.
- Enter the code for Insertion Sort into the Java Visualizer and execute it step-by-step.
- How many steps are required to execute this code on an array of size
10? In other words, how many times is the code inside the inner loop executed? How does this count relate to the size of the array?
- Let's act it out!