0% found this document useful (0 votes)
2 views

POGIL-09-BasicSortingAlgorithms-fillable complete

The document discusses three sorting algorithms: bubble sort, selection sort, and insertion sort, explaining their mechanisms and complexities. It provides examples and simulations for each method, detailing their operations and performance in terms of big-O notation. The overall complexity for bubble sort and insertion sort is O(n^2), while selection sort also exhibits the same complexity.

Uploaded by

garrettfoltyn
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

POGIL-09-BasicSortingAlgorithms-fillable complete

The document discusses three sorting algorithms: bubble sort, selection sort, and insertion sort, explaining their mechanisms and complexities. It provides examples and simulations for each method, detailing their operations and performance in terms of big-O notation. The overall complexity for bubble sort and insertion sort is O(n^2), while selection sort also exhibits the same complexity.

Uploaded by

garrettfoltyn
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Bubble, Selection, & Insertion Sort pg 1 of 8

start 1233
Bubble, Selection, & Insertion Sort time:

In computing, we often need to sort a set of items. As computer scientists, we study


ways to sort very large sets, with thousands or millions of values, since searching and
other operations are often easier when the set of values is sorted.

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

Team Roles Team Member


Recorder: records all answers & questions,
and provides copies to team & facilitator. Garrett
Speaker: talks to facilitator and other teams.

Manager: keeps track of time and makes sure


everyone contributes appropriately. Aidan
Reflector: considers how the team could
work and learn more effectively. Armaan
Reminders:
1. Note the time whenever your team starts a new section or question.
2. Write legibly & neatly so that everyone can read & understand your responses.
Bubble, Selection, & Insertion Sort pg 2 of 8

start 1233
Model A. (15 min) Bubble Sort time:

Consider an array of unsorted data:

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.

public void compareAndSwap(Comparable[] arr, int a, int b){


if( arr[a].compareTo( arr[b] ) >= 1 ){
Comparable temp = arr[a];
arr[a] = arr[b];
arr[b] = temp;
}
}

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.

for( int i = 0; i < arr.length - 1; i++ ){


compareAndSwap( arr, i, i + 1 );
}

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.

New Array = arr;


while (Array != arr){
Array = arr;
for(){compareAndSwap()}
}

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.

public void findMinAndSwap( Comparable[] arr, int start ){


int minIndex = start;
for( int i = start+1; i < arr.length - 1; i++ ){
if( arr[minIndex].compareTo( arr[i] ) >= 1 )
minIndex = i;
}
Comparable temp = arr[start];
arr[start] = arr[minIndex];
arr[minIndex] = temp;
}

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.

public void selectionSort( Comparable[] arr ){


for( int i = 0; i < arr.length - 1; i++ ){
findMinAndSwap( arr, i );
}
}
a. How many times will the selectionSort method call findMinAndSwap on a list of
size n?

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.

Value of i 0 1 2 n-4 n-3 n-2

# Comparisons n-1 n-2 n-3 3 2 1


8. (7 min) You may notice a pattern between the first and last values in the table above, as well
as a similar pattern between the middle two values. That pattern would exist for all values of i if
we could write them all out.

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)

d. From that equation, determine the big-O performance of selectionSort.

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.

public void insert(Comparable[] arr, Comparable val, int size){


arr[size] = val;
for(int i = size-1; i >= 0; i-- ){
if( val.compareTo( arr[i]) >= 1 )
return; //quit if val is in place
//otherwise, swap val downward
Comparable temp = arr[i];
arr[i] = arr[i+1];
arr[i+1] = temp;
}
}
9. (2 min) Demonstrate the steps that would be performed by insert( arr, 12, 4 ) on
the array below.

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.

for( int i = 1; i < arr.length - 1; i++ ){


insert( arr, arr[i], i );
}

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?

c. How many times must insert be performed 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)

You might also like