Complete these steps with roles Person 1 (P1) and Person 2 (P2):
- P1 selects 5 of your numbers at random
- P2 walks through the algorithm sorting those five numbers out loud to practice without counting steps
- P1 adds three new cards, shuffles, and sorts the set out loud. P2 counts the steps using the guidelines below.
- P2 adds two more cards, shuffles, and sorts the set out loud. P1 counts the steps using the guidelines below
Increment your number of steps for each of these operations:
- Compare two numbers
- Making a space in the set (one step no matter how many elements seem to move)
- Insert into the output set
- Start an empty output set
- Add the first element from your input to the output set.
- If there are no more elements in the input set, you're done!
- Take the next element from your input and call it
current
. - Call the first element of your output set
target
. - Compare
current
totarget
:
- If
target
is larger thancurrent
, insertcurrent
just beforetarget
then go to Step 3. - Else if it is not larger, move the name
target
to point to the next element in the output set and repeat Step 6.
Discuss and write answers to these questions in your notebook:
- What would the worst case input set be for this algorithm? What would be the best?
- How would the number of operations grow as the number of inputs increases?
- Would this be a good algorithm to use to sort a set of one million elements? Does your answer change if you're a junior developer versus a senior developer?