POGIL-09-BasicSortingAlgorithms-fillable complete
POGIL-09-BasicSortingAlgorithms-fillable complete
start 1233
Bubble, Selection, & Insertion Sort time:
One way to design a sorting algorithm is to choose a single operation that makes the data slightly
closer to being sorted, and then to repeat it until the data is fully sorted. We will examine three
algorithms that are each centered around a different base operation: bubble sort, selection sort,
and insertion sort.
Before you start, complete the form below to assign a role to each member.
If you have 3 people, combine Speaker & Reflector.
Team
start 1233
Model A. (15 min) Bubble Sort time:
5 2 1 10 7
A program could get the array slightly closer to being sorted if it swapped any two neighboring
values that were out of order (such as index 0 and index 1 here.) The method compareAndSwap
below performs this operation for two indices in an array, if the two values are out of order.
1. (5 min) The compareAndSwap operation only makes the data slightly closer to being sorted
on its own, but repeating it might improve the situation.
a. In the table, simulate what would happen to the array values if the loop above were
executed on that array.
5 2 1 10 7
2 1 5 7 10
b. What is the big-O complexity of this code for an array of size n, in terms of comparisons?
n
Bubble, Selection, & Insertion Sort pg 3 of 8
2. (3 min) We can’t count on a single iteration through the data to fully sort the list using this
method. Repeatedly iterating over the data in this way until it is sorted is called bubble
sort. Below, repeatedly iterate over the three different versions of our array until they are
sorted: the original array, a sorted one, and a reverse-sorted one. Write down what the array
looks like after each iteration of bubble sort.
5 2 1 10 7
2 1 5 10 7
1 2 5 7 10
1 2 5 7 10
1 2 5 7 10
1 2 5 7 10
1 2 5 7 10
1 2 5 7 10
1 2 5 7 10
1 2 5 7 10
10 7 5 2 1
7 5 2 1 10
5 2 1 7 10
2 1 5 7 10
1 2 5 7 10
3. (5 min) As we see in our simulations above, we don’t know before we begin sortng how many
iterations it will take to sort the data.
a. What is the stopping condition for bubble sort?
When the new array created is the same as the old one
b. What would need to be changed about the compareAndSwap method to be able to tell
that the algorithm should stop?
create a clone of the orginal array befor it is run through the for loop and then compare
that to the original and if they are the same, stop
Bubble, Selection, & Insertion Sort pg 4 of 8
c. Describe with words or pseudocode how an outer loop for bubble sort could track
whether the stopping condition has been met.
4. (1 min) How many times would the algorithm need to iterate through a reverse-sorted list of
size n?
n-1
5. Given this information, what is the big-O performance of the overall bubble sort
algorithm?
0(n^2)
Bubble, Selection, & Insertion Sort pg 5 of 8
start 1248
Model B. (15 min) Selection Sort time:
Another way to move slightly closer to having a sorted list is to find the minimum value and to
put it at the start of the list, giving a list with a single value in exactly the correct place.
findMinAndSwap does this a bit more generally by finding the minimum in a portion of an
array, then swapping the minimum into the first index within that portion.
6. (3 min) Simulate the steps performed by findMinAndSwap on the array below if it was
called with 0 as start, then with 1 as start.
2 5 7 10 1
start = 0 1 5 7 10 2
start = 1 1 2 7 10 5
7. (5 min) The selection sort algorithm consists of finding the lowest value, second lowest,
and so on, placing each at their correct index until the entire list is sorted. Answer the
questions for the selectionSort method below.
n -1
Bubble, Selection, & Insertion Sort pg 6 of 8
b. Fill in the table for how many comparisons must be done in the worst case (the minimum
is always the last value) by findMinAndSwap the various times it is called.
a. If we combined each of these pairs by adding the Value of i and the # Comparisons
together, what would we get each time?
n-1
b. Given what you know about the number of times findMinAndSwap is called, in terms of
n, how many pairs (not individual values) are there to combine?
n/2
c. Use the information above to formulate an equation for the total number of comparisons.
n/2*(n-1)
n^2
Bubble, Selection, & Insertion Sort pg 7 of 8
start 101
Model C. (15 min) Insertion Sort time:
If we already had an array of sorted values (with extra space), we could insert new values into
the correct location within that array. The method below inserts a new value into the correct
position in an array relative to all the values currently in that array. The arr parameter is the
sorted array. The val parameter is the object to be inserted. The size parameter indicates how
many values are already in the array.
2 7 14 17
2 7 14 17 12
2 7 12 14 17
10. (3 min) If we take every item from an existing array and call an insert method to perform
the same task you simulated above, this is called insertion sort. Given the following input
array and blank output array, simulate the algorithm, using an insertion process similar to
what you did above for the first value, second, etc.
5 2 1 7
2 1 5 7
1 2 5 7
1 2 5 7
1 2 5 7
Bubble, Selection, & Insertion Sort pg 8 of 8
11. (5 min) Insertion sort can be performed on a single array, rather than creating an empty
array. Simulate the performance of the following loop on the array shown.
5 2 1 7
2 5 1 7
1 2 5 7
1 2 5 7
12. (5 min) Simulate the performance of insertion sort on a reverse sorted array.
7 5 2 1
5 7 2 1
2 5 7 1
1 2 5 7
a. How many comparisons are done by the first call to insert on an array of size n?
b. How many comparisons are done by the last call to insert on an array of size n?
sumatation of n
d. Given what you know about this pattern, what is the big-O performance of insertion
sort?
O(n^2)