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

ICS4U0 Unit4 Package

Uploaded by

angadsingh2989
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

ICS4U0 Unit4 Package

Uploaded by

angadsingh2989
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Arrays Introduction & Big Oh Notation 3.

1 T

Name: _________________________
Angad
0. Look at the following array and answer these questions about it.
(b) What is the value of m.length? ....................
int m[]={1, 8, 3, 4, 20};
(c) What is the value of m.length()? ....................
int t = 3; (d) What is in m[2]? ....................
(e) What is in m[t]? ....................
//Print backwards (f) What is in m[t+1]? ....................
String rev = ""; (g) What is in m[t-1]? ....................
for(int i=m.length-1; i>=0; i--)
rev+=m[i]; (h) What is in element 0? ....................
result.setText(rev); (i) What is the index of 8? ....................
(j) What is the index of 20? ....................
(a) Fill in the array memory diagram for ‘m’: (k) What is in m[m.length]? ....................
[0] [1] [2] [3] [4] (l) What is in m[m.length-1]? ....................

1. Why are arrays useful? (Circle the most correct answer)


(a) They allow the grouping of variables so that they can be loops through quickly.
(b) They allows you to change the flow of the code through the program.
(c) They allow the division of code into small sections to organize it.
(d) They allow the programmer to make notes to themselves about the running of the code.

2. Suppose that you have a sorted array and wish to find all the repeated values:
(a) What are the
int sort[]={1,2,3,5,5,6,7,8,9,11,13,13,17,20,20,21};
repeated values
String repeat = ""; in the array?
(b) What is the
for(int i=0; i<sort.length-1; i++){
array type?
if(sort[i]==sort[i+1]) (c) What is the
array name?
repeat+= sort[i]+" ";
(d) What does the
result.setText(repeat); loop start at?
(e) What does the
}
loop end at?

(f) Write code to print the minimum of the sorted array in a TextView named min. (handle inflation first)

(g) Write code to print the maximum of the sorted array in a TextView named max. (handle inflation first)
3. What is an algorithm? (Circle the most correct answer).
(a) Computer code that sorts things. (c) A flowchart diagram.
(b) A series of steps to complete a task. (d) Computer code.

4. In Big Oh notation (for arrays)


(a) what does the O stand for? .................. (b) what does the n stand for? ..................

5. Why do we measure algorithm speed in terms of number of operations as opposed to seconds?


...................................................................................................................
6. Put these algorithm speeds in order. 7. What speeds are these array algorithms?
(1 is fastest, 7 is slowest) (a) Finding the minimum _________
___ O(n2) (b) Printing all values _________
___ O(n) (c) Swapping two values _________
___ O(n log n) (d) Selection Sort _________
___ O(2n) (e) Finding the maximum _________
___ O(log n) (f) Finding the average _________
___ Constant time (g) Quicksort _________
___ O(n3) (h) Binary search _________
8. A common coding interview question based on algorithms is:
“How do you find the missing number in integer array of 1 to 100?”
(a) This code puts the values from 1-100 into an array . Fill in the blanks.
______ hundred[]=new int[_______];
for(int i=____; i<hundred._______; i++){
hundred[_____]=i+1;
}
(b) This code un-sorts the array from part a. Put the code in order.
______ int a = (int)(Math.random()*100);
______ hundred[a]=hundred[b];
______ int swap = hundred[a];
______ }
______ hundred[b]=swap;
______ for(int i=0; i<60; i++){
______ int b =(int)(Math.random()*100);

(c) Assume that after the array is sorted, a user randomly selects a number and replaces it with 0.
int randomPlace=(int)(Math.random()*100);
hundred[randomPlace]=0;
Write the code to find the missing number WITHOUT using randomPlace. Save it in an int named answer.
Strong hint: The sum of the numbers from 1 to 100 is 5050.
Android Arrays of Objects 3.2 C

Name: _________________________
1. On a gridLayout, views are added row by row, from left to right. Label the order these widgets should be put
into the gridLayout so that they are added correctly.

2. This game is called Scrubby Dubby. Fill in the grid information with the appropriate picture code.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent" The picture information:
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#000000">

<GridLayout The grid information:


android:layout_width="wrap_content"
android:layout_gravity="center"
android:layout_height="match_parent"
android:rowCount="___"
android:columnCount="___"
android:id="@+id/grid">
</GridLayout>

</LinearLayout>

public class Game extends AppCompatActivity {


int soap[][]= {{____, ____, ____, ____, ____},
{____, ____, ____, ____, ____},
{____, ____, ____, ____, ____}};
int row = _____;
int col = _____;
ImageView pics[]=new ImageView[row*col];
public void setpicStart(ImageView i, int pos){
@Override int x = pos/col;
protected void onCreate(Bundle savedInstanceState){ int y = pos%col;
super.onCreate(savedInstanceState); int picnum = soap[x][y];
setContentView(R.layout.activity_game); if(picnum==1)
GridLayout g = (GridLayout)
findViewById(R.id.grid); i.setImageResource(R.drawable._________);
int m=0; else if(picnum==2)
for(int i=0; i<row; i++){
for(int j=0; j<col; j++){ i.setImageResource(R.drawable._________);
pics[m]=new ImageView(this); else if(picnum==3)
setpicStart(pics[m], m);
pics[m].setId(m); i.setImageResource(R.drawable._________);
g.addView(pics[m]); else if(picnum==4)
m++;
} i.setImageResource(R.drawable._________);
} }
}
3. This game is called Diamond Digger. Fill in the grid information with the appropriate picture code.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent" The picture information:
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#000000">

<GridLayout The grid information:


android:layout_width="wrap_content"
android:layout_gravity="center"
android:layout_height="match_parent"
android:rowCount="6"
android:columnCount="6"
android:id="@+id/grid">
</GridLayout>

</LinearLayout>

public class Game extends AppCompatActivity {


int diamond[][]= {{____, ____, ____},
{____, ____, ____},
{____, ____, ____},
{____, ____, ____},
{____, ____, ____}};
int row = _____;
int col = _____;
ImageView pics[]=new ImageView[____*____]; public void setpicStart(ImageView i, int pos){
int x = pos/_______;
@Override int y = pos%_______;
protected void onCreate(Bundle savedInstanceState){ int picnum = ______________[x][y];
super.onCreate(savedInstanceState); if(picnum==1)
setContentView(R.layout.activity_game);
GridLayout g = (GridLayout) i.setImageResource(R.drawable._________);
findViewById(R.id.grid); else if(picnum==2)
int m=0;
for(int i=0; i<____; i++){ i.setImageResource(R.drawable._________);
for(int j=0; j<____; j++){ else if(picnum==3)
pics[m]=new ImageView(this);
setpicStart(pics[m], ______); i.setImageResource(R.drawable._________);
pics[m].setId(m);
g.addView(pics[m]); }
m++;
}
}
}

4. For each picture, you will have a number representation in an integer grid. This tracks the picture value.
(a) Why choose an int type array? (3 reasons, no sentences)
..................................................................
..................................................................
..................................................................
(b) What is a problem with choosing an int type array? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
5. Explain why a setPicMethod is useful. (2 points, sentences)

.......................................................................................................

.......................................................................................................

.......................................................................................................

.......................................................................................................
Grid Array Algorithms 3.3 K
Name: _________________________
1D to 2D Array
0. ImageView arrays are only 1D so their IDs are unique. The 1D array for the grid below is like this:

However, if you look at the screen, it is laid out in a grid, or 2D array. We use a 2D int array to track these
positions.
For the 1D ImageView array: The references are from the 2D int
(a) How many elements? tracking array. Write the
corresponding ID of the 1D
......... ImageView array.
(b) What is the ID of the
first orange oval? (e) [0][0] .........
......... (f) [2][1] .........

For the 2D int tracking array: (g) [3][2] .........


(c) How many rows? (h) [2][3] .........
......... (i) [1][3] .........
oval.gif sq.gif round.gif drop.gif
(d) How many columns? (j) [0][2] .........
0 1 2 3
.........
(k) Using the numbers above, fill in the code for this game of candy crush.
public class MainActivity extends AppCompatActivity {

int candy[][] = {{__, __, __, __}, {__, __, __, __}, {__, __, __, __}};
int row = ___;
int col = ___;
ImageView pics[] = new ImageView[row * col];

@Override public void setpic(ImageView i, int pos) {


protected void onCreate(Bundle savedInstanceState) { int x = pos / col;
super.onCreate(savedInstanceState); int y = pos % col;
setContentView(R.layout.activity_main); int picnum = __________[x][y];
GridLayout g = (GridLayout) findViewById(R.id.grid); if (picnum == ____)
int m = 0; i.setImageResource(R.drawable._____);
for (int i = 0; i < row; i++) { else if (picnum == _____)
for (int j = 0; j < col; j++) { i.setImageResource(R.drawable._____);
pics[m] = new ImageView(this); else if (picnum == _____)
setpic(pics[m], m); i.setImageResource(R.drawable._____);
pics[m].setId(m); else
g.addView(pics[m]); i.setImageResource(R.drawable._____);
m++; }
}
}
} If n is the ID of the ImageView:
int x = n / col;
1. Assume that you have an grid that is 6 (rows) x 5 (cols). int y = n % col;
(a) How many ImageViews do you need? . . . . . . . .
(b) Given the ImageView IDs, determine each button’s (x, y), aka (row, col), position in the int tracking array.
6 10 17 29
Grid Array Algorithms
2. The following pictures are placed in arrays to produce two levels of a game as shown below.
(a) Fill in the lvl1 and sol1 array shown in the array below.
int row = 3;
int col = 6;
int game[][]= new int[row][col];

Level 1 Level 2

int lvl1[][]= {{___, ___, ___, ___, ___, ___}, int lvl2[][]= {{___, ___, ___, ___, ___, ___},
{___, ___, ___, ___, ___, ___}, {___, ___, ___, ___, ___, ___},
{___, ___, ___, ___, ___, ___}}; {___, ___, ___, ___, ___, ___}};
(b) This method redraws the screen with whatever is currently in the game array.
public void redraw() {
int m = 0;
for (int i = 0; i < __________; i++) {
for (int j = 0; j < ____________; j++) {
if (____________[i][j] == ______)
pics[m].setImageResource(R.drawable._____________);
else if (_____________[i][j] == ______)
pics[m].setImageResource(R.drawable._____________);
else if (_____________[i][j] == ______)
pics[m].setImageResource(R.drawable._____________);
else
pics[m].setImageResource(R.drawable._____________);
m++;
}
}
}

(c) Create a method to copy over the array named b into the array named a.
public void copyOver(int a[][], int b[][]) {
for (int i = 0; i < ___________; i++) {
for (int j = 0; j < __________; j++) {
a[i][j] = ______[___][___];
}
}
}

(d) Create an onClick for a button named next that copies the lvl2 array and puts it into the game array. Then it
redraws the array on the screen.
public ____________ _______________(____________ ___________) {
copyOver(__________, __________);
redraw();
}
Searching Algorithms 3.4 C

Name: _________________________
0. Do a linear search on this array for 4.
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] [14] [15]
-10 -5 4 7 9 10 16 22 28 34 37 40 55 345 900 1002
Found in position: _____
1. Do a linear search on this array for ‘m’.
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11]
a n w e q j k d f g h j
Found in position: _____
2. Do a linear search on this array for “Fish”.
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13]
Cat Dog Tree Frog Fish Zoo Ape Kit Ham Two City Red Yam Bob
Found in position: _____
3. Circle the most correct answer.
T F a) Sorting is faster than searching.
T F b) Sorting is finding an element and searching is putting in order.
T F c) Linear Search has one for loop so it is an “n” speed algorithm.
T F d) Linear Search is slower than Selection Sort.
T F e) Linear Search is faster than Quick Sort.
T F f) Linear Search would work on unsorted data.
T F g) Linear Search would work well when looking for the name “ZZtop” in the
phone book.
T F h) Linear Search would work well when looking through a pile of unsorted test
papers to find a particular student’s work.
T F i) The Google Search engine uses linear search because it is really efficient.

4. For each of these algorithms:


 (a) Change the code from an integer search to a String search.
 (b) Fill in the algorithm name & speed.
int find = Integer.parseInt(in.getText().toString()); int find = Integer.parseInt(in.getText().toString());
int high = array.length;
int low = 0; int pos = -1;
boolean foundit = false; for (int i = 0 ; i < array.length ; i++)
int mid = 0; {
if (array [i] == find)
while (high >= low && !foundit) { pos = i;
mid = (high + low) / 2; }
if(pos==-1)
if (array [mid] == find) result.setText("Didn’t find it");
foundit = true; else
result.setText("It is in position "+pos);
else if (find > array [mid])
low = mid + 1;

else //if (find < array [mid])


high = mid - 1;
}

if(foundit)
result.setText("It is in position "+mid);
else
result.setText("It is not in the array");

Algorithm Name: Algorithm Name:

Algorithm Speed: Algorithm Speed:


5. Trace the search of this array to find ‘3’ using binary search. Draw the search on the array too.

Low High Mid


[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] [14]
-2 3 4 6 7 8 10 12 14 34 56 78 79 80 82

6. Trace the search of this array to find ‘12’ using binary search. Draw the search on the array too.

Low High Mid


[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] [14] [15] [16]
-3 0 1 2 3 4 5 6 7 8 9 10 12 13 14 15 17

7. Trace the search of this array to find ‘W’ using binary search. Draw the search on the array too.

Low High Mid


[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13]
A D E G I K L N P Q U V W Z

8. Trace the search of this array to find ‘nap’ using binary search. Draw the search on the array too.

Low High Mid


[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12]
abe at be by do egg fi go hi I moo we zoo

9. Circle the most correct answer.


T F a) Binary Search is generally a great deal faster than Linear Search.
T F b) Binary Search relies on the data being sorted.
T F c) A computer would search a phone book using Binary Search.
T F d) A computer would search a library for a fiction title using Binary Search.
T F e) A hidden cost of the fast speed of Binary Search is that the data must be sorted
and sorting is a slow operation.
T F f) Binary Search is more complex than Linear Search.
T F g) Binary Search’s speed is based on the power of 2 because the data repeatedly
discards half of the data.
T F h) If you have 100 items, in sorted order, Binary search can find an item in at
most 7 tries.

10. Explain the trade-off inherent in binary search.


.................................................................................................
.................................................................................................

.................................................................................................
Selection Sort 3.5 K
Name: _________________________
1. Find the maximum element in each data set.
(a) 2, 5, 67, 8, 7, 98, 1
(b) a, g, r, t, y, u, p, q
(c) zebra, cheetah, elephant, hippo, rhino
(d) red, orange, yellow, green, blue, indigo, violet

2. In each of these three data sets, circle the first two elements that swap.

(a)

(b) (c)

3. Circle the algorithms.


Origami instructions Paper Crane Cake Recipe IKEA instructions

Selection sort Rainbow Unicorn Cake Turnip

Flowchart Computer Mouse Book Map

4. What is the trade-off associated with Selection sort?

...................................................................................................................
...................................................................................................................

5. Put these algorithm speeds in order. 6. What speeds are these array algorithms?
(1 is fastest, 7 is slowest) (a) Linear search _________
___ O(2n) (b) Finding array length _________
___ O(n!) (c) Selection Sort _________
___ O(log n) (d) Finding the average _________
___ Constant time (e) Mergesort _________
___ O(n2) (f) Binary search _________
___ O(n) (g) Redraw _________
___ O(n log n) (h) Copy one array into another _________
7. Trace selection sort for these arrays.
(a) (b) (c)
8 9 4 2 3 1 V T Q U R S Big Ape Add Cap App

8. Look at the following code.


int a[] = {23, 12, 4, -4, 5, 7, 9, 55, 0, -5}; Circle and label the following things.
(a) code to swap two values
for (int left = a.length - 1 ; left > 0 ; left--) { (b) code to find the maximum
(c) the loop that repeatedly finds the
int max = 0;
maximum
for (int i = 1 ; i <= left ; i++) {
if (a [max] < a [i]) Identification questions:
max = i;
}
(a) What type is the array? ___________
(b) How long is the array? ___________
int temp = a [max]; (c) What are the index numbers? ____ to ___
a [max] = a [left]; (d) What is in a[2]? ______
a [left] = temp;
(e) In the outer loop, what is the loop stopping
}
variable? ______
(f) In the inner loop, what is the loop stopping
variable? ______

9. Change the selection sort code to adapt to the following situations.


(a) The array name is num, not a. (7 changes) (b) The array type is double (only 1 change)
for (int left = a.length - 1 ; left > 0 ; left--) { for (int left = a.length - 1 ; left > 0 ; left--){

int max = 0; int max = 0;


for (int i = 1 ; i <= left ; i++) { for (int i = 1 ; i <= left ; i++) {
if (a [max] < a [i]) if (a [max] < a [i])
max = i; max = i;
} }

int temp = a [max]; int temp = a [max];


a [max] = a [left]; a [max] = a [left];
a [left] = temp; a [left] = temp;

} }
(c) The array type is String (2 changes) (d) You want to go from largest to smallest (only 1
code change is needed, then 5 to rename max)
for (int left = a.length - 1 ; left > 0 ; left--) { for (int left = a.length - 1 ; left > 0 ; left--) {

int max = 0; int max = 0;


for (int i = 1 ; i <= left ; i++) { for (int i = 1 ; i <= left ; i++) {
if (a [max] < a [i]) if (a [max] < a [i])
max = i; max = i;
} }

int temp = a [max]; int temp = a [max];


a [max] = a [left]; a [max] = a [left];
a [left] = temp; a [left] = temp;

} }
Bubble Sort 3.6 K

Name: _________________________
0. Which tracing shows bubble sort and which show selection sort?
9 8 7 6 5 9 8 7 6 5
8 9 7 6 5 5 8 7 6 9 q a m b c
8 7 9 6 5 5 6 7 8 9 a q m b c
8 7 6 9 5 a m q b c
8 7 6 5 9 a m b q c
7 8 6 5 9 q a m b c a m b c q
7 6 8 5 9 c a m b q a b m c q
7 6 5 8 9 c a b m q a b c m q
6 7 5 8 9 b a c m q
6 5 7 8 9 a b c m q
5 6 7 8 9

1. Circle the first two elements to swap in:

Selection sort Bubble sort

2. One of these pictures represents bubble sort and one selection. Which is which?

3. Trace the following sorting algorithms.


Bubble Sort Selection Sort Bubble Sort
10 12 2 8 6 I U E A O o i u e a

Selection Sort
8 6 4 1 3
int a [] = {5, 62, 81, 9, 30, 42, 0}; 4. Answer the following questions about
the adjacent code.
public void Bubble(View view) { (a) What is in the array to start?
clear(); 0 1 2 3 4 5 6
unsort();
bubbleSort(a);
redraw();
}
(b) What type is the array? ..........
public void bubbleSort(int a[]) { (c) What is the name of the array? . . . . . . .
int n = a.length;
for (int i = 0; i < n - 1; i++) { (d) What is a.length’s value? .........
for (int j = 0; j < n - 1 - i; j++) {
if (a[j + 1] < a[j]) { (e) What is the onClick of the button?
int temp = a[j];
a[j] = a[j + 1]; ...................
a[j + 1] = temp;
addNewRow();
(f) What is in memory like after the array
} is sorted?
} 0 1 2 3 4 5 6
}
}

(g) What are the three lines to swap the


elements? Box them.

5. What is the trade-off inherent in bubblesort?

..........................................................................................................
..........................................................................................................
..........................................................................................................
..........................................................................................................

6. Which one is selection sort? (circle) 7. In 2013, the Waterloo Beaver Computing Challenge asked the
following question. You are arranging people in order based on the
numbers on their shirts. The order to start is:
7 3 2 9 8 5 1 4 6
You will arrange individuals using the following technique:
• Look at two consecutive people at a time, starting from the
left.
• If the person on the left has a number which is larger than
that of the person on the right, switch the positions of those
two people; otherwise, leave them in the order they are in.
• Move to the right one position, so that you are comparing
one new person with one of the people just compared, and
repeat the above comparison and potential swap.
Once you have compared the right-most two people in the list, we
call this one “pass” over the list.
How many passes over the list are required until the list is in the
order:
1 2 3 4 5 6 7 8 9?

Circle the best answer: (a) 2 (b) 4 (c) 6 (d) 9


Merge Sort 3.7 T

Name: _________________________
Conceptually, a merge sort works as follows:
 Divide the unsorted list into n sublists, each containing 1 element (a list of 1 element is considered sorted).
 Repeatedly merge sublists to produce new sorted sublists until there is only 1 sublist remaining. This will
be the sorted list.

0. Fill in both tracings of mergesort:

1. Fill in the speeds of the algorithms: 2. Given the following array of numbers:
________ Bubble sort, best case. {21, 1, 26, 45, 29, 28, 2, 9, 16, 49, 39, 27, 43, 34, 46, 40}
which answer illustrates the array to be sorted after 3
________ Bubble sort, average case.
recursive calls to mergesort? (circle the correct answer)
________ Delete to a list.
________ Add to a list. (A) {16, 49, 39, 27, 43, 34, 46, 40}
(B) {21,1}
________ Mergesort average case. (C) {21, 1, 26, 45}
________ Selection sort, average case. (D) {21}
________ Print out array.
________ Find the maximum

3. Consider pseudocode of the main method of mergesort.


public int[] mergeSort(int[] array) { (a) Circle and label the base case.
(b) Box the recursive cases.
if (array.length <= 1)
return array; (c) What is the name of the
method? . . . . . . . . . . . . . . . . .
else { (d) Another method is called
int middle = array.length / 2; (outside of mergeSort). What
int firstHalf = mergeSort(array[0..middle - 1]); is it? . . . . . . . . . . . . . . . . .
int secondHalf = mergeSort(array[middle..array.length - 1]);
(e) What is the return type?
return merge(firstHalf, secondHalf);
.................
}
} (f) What is the parameter type?
.................
4. Answer true or false about each statement.
T F a) Mergesort is recursive; that is why it is fast.
T F b) The division and merging process in mergesort requires extra memory.
T F c) The base case of mergesort is an array of one element.
T F d) Once the base case is reached in mergesort, the division process begins.
T F e) Once the base case is reached in mergesort, the merge process ends.
T F f) “Merge” means a smooth, orderly joining of two things.
T F g) Mergesort is an in-place algorithm.
T F h) Selectionsort is an in-place algorithm.
T F i) Mergesort is faster than Quicksort.
T F j) Mergesort is faster than Bubble sort when the array is almost sorted.

5. Which sorting algorithm should be used in each instance?


(a) You have a random array, but no extra memory.
..............................
(b) You have an almost sorted array, but no extra
memory.
..............................
(c) The array is sorted, but one item was added to the
front. You have no extra memory.
..............................
(d) You have 500 million elements in random order.
..............................
(e) You have an array of 500 names in random order.
..............................
(f) You have an array of 500 names that is almost in
correct order.
..............................
6. Explain the trade-off of mergesort.
............................................................................
............................................................................
............................................................................
............................................................................
............................................................................
7. Sort each array using mergesort. (Make boxes and use lines to connect them)
6 3 2 8 4 9 5 0 9 1 6 4 7 3 8 2
Quick Sort 3.8 T

Name: _________________________
void quicksort(int[] array, int startIndex, int endIndex) The steps are:
{
if (startIndex >= endIndex) {  Pick an element, called a pivot, from the array.
return;  Partitioning: reorder the array so that all elements less
than the pivot come before the it, and all elements
} else {
int pivotIndex = partition(array, startIndex, endIndex);
greater come after. After partitioning, the pivot is in its
quicksort(array, startIndex, pivotIndex - 1); final position.
quicksort(array, pivotIndex + 1, endIndex);  Recursively apply the above steps to the sub-array of
} elements before the pivot AND again to those after it.
}

0. Which of the following pictures show quickSort and which show mergeSort?

1. Circle True or False for each question.


T F a) The O in O(n) stands for order.
T F b) The n in O(n) stands for number of operations.
T F c) The n in O(n) stands for number of elements in the array.
T F d) The n in O(n) stands for the number of seconds to run the algorithm.
T F e) Algorithm speed isn’t measured in terms of time, because that is hardware dependant.
T F f) Constant time is another way of saying O(1).
T F g) The fastest time is O(log n).
T F h) Quicksort is a faster O(n log n) than Mergesort, assuming a randomized data set.
2. When one partition of quicksort is done, where is the pivot?

.............................................................................................................
3. Why are quicksort swaps so much better than bubblesort swaps?

.............................................................................................................

.............................................................................................................

.............................................................................................................
4. Trace quicksort on the following arrays.
4 2 9 7 8 3 5 1 6 8 7 6 9 0 1 3 4 5

5. Which algorithm should be used in each case?


a) The sun is shining and the array is in random order.

.............................
b) You have no extra memory and the array randomly ordered.

.............................
c) You have extra memory and the array is in random order.

.............................
d) The array is almost sorted and you are eating an apple.

.............................
e) An integer array, storing ages, is in almost reverse order.

.............................
f) The String array is in almost reverse order, there are 5
million elements and you are sorting on a school computer.

.............................
g) The char array is not well randomized and you have lots of
extra memory.

.............................
The Secret Lives of Modern Algorithms 3.9 C

Name: _________________________

1. From the movie, the Secret Lives of Algorithms, fill in the first column. (The last 5 are from class)
Word Description
A series of steps to complete a task.
Phones use this to quickly find human faces in real time on the screen.
A game strategy algorithm. If your opponent takes n items, you take (4-n).
The oldest recorded algorithm. Finds the greatest common divisor for 2 #s.
A sorting algorithm that repeatedly swaps out of order elements into place.
John Von Neumann’s sorting algorithm. Data is divided then merged.
A billion dollar algorithm that puts searches in an order useful to the user.
A gaming system that taught itself how to understand human movement.
A streaming service that uses algorithms to make future recommendations.
Robots that use algorithms to put together customer orders for mailing.
A compromise. You give up something to get something else.
Looks at each item in the list until the desired item is found.
The worst sorting algorithm. It might never finish.
Repeatedly discarding half of a sorted list until the item is found.
A sorting method where you repeatedly swap the max into place.
The fastest in-place algorithm in the general case.

2. Write the algorithm name that matches each picture.


3. Fill in the crossword using terms from this unit.

Across
7. A searching algorithm that always works, but it is a bit slower.
9. A sorting algorithm invented by John Von Neumann. Data is divided and merged in order.
10. The worst sorting algorithm.
11. The fastest in-place sorting algorithm for regular randomized data.
13. The oldest recorded algorithm. Finds the greatest common divisor for 2 large
numbers.
15. A streaming service that uses algorithms to make recommendations based on your
past movie choices.
16. The fastest searching algorithm.

Down
1. Google’s billion-dollar algorithm; puts web searches in an order useful to the user.
2. A sorting method where you repeatedly find the max and swap it into place.
3. Robots that use algorithms to efficiently put together customer orders for mailing.
4. Phones use this algorithm to quickly find human faces in real time on the screen.
5. A sorting algorithm that repeatedly swaps out of order elements into place. Named
after pop bubbles.
6. A series of steps to complete a task.
8. A game strategy algorithm. If your opponent takes n items, you take (4-n) items.
12. A compromise made when you make a choice. You give up something to get
something else.
14. A gaming system that taught itself how to understand human movement.
PDLC 3.10 C

Name: _________________________
1. What does the acronym PDLC stand for?

2. Match the job with the phase of the PDLC.


Ad Writer, Alpha Tester, Analyst, Beta Tester, Designer, Field Service Technician, Graphic Artist, Help Desk
Lead Programmer, Musician, Programmer, Sales Analyst, Support and Training, Writer
Analysis Design Code Reflection/Maintenance

3. Unscramble these PDLC jobs.

4. Fill in the job that matches the description.


(a) Answers questions that people email or call in about the software.
(b) Helps people use their new software on site.
(c) Decides the overall game or software idea.
(d) Draws the flow charts, screen flow diagrams and structure charts.
(e) Someone in the company, with some programming knowledge who
does testing.
(f) A user who does testing.
5. What are the 4 PDLC phases? 6. Cross out the items that do not belong in the phase.

Analysis Design
1. Create a storyline 1. Character Design
2. Hold Focus Groups 2. Artwork
3. Design levels 3. Level Design
A......... B......... 4. Pitch Concept 4. Watch Netflix
5. Write while loops 5. Make Prototypes
6. Eat Lunch 6. Create storyline
7. Define the Problem 7. Alpha testing
8. Build a snowman 8. Draw Flow charts,
D. . . . . . . . . C.........
9. Analyze sales figures structure charts
10. Write a list of 9. Write and record music
specifications 10. Define the problem

7. True or False: circle the most correct answer.


T F a) Writing loops comes before Flowcharts in your code.
T F b) Testing takes ¾ of the coding phase in a professional game.
T F c) The PDLC is necessary because big programs require a lot of planning to enable large teams to
work together.
T F d) An analyst is generally better paid than a programmer, because the analyst job is harder.
T F e) A tester is generally better paid than a programmer, because it is difficult to find coding errors.

8. Fill in the crossword using job titles from the PDLC.

Across
5. Reviews the sales in the
reflection phase. Thinks about a
sequel.
7. Would pitch the overall idea
and decide the direction of the
software.
8. A type of testing done inside the
company.

Down
1. Draws diagrams and lays out
the details of the program
2. Supports users who are having
difficulties with the software.
3. Designs the screen layout,
colours, character design and
backgrounds.
4. Composes and records music
for the software.
6. creates advertisements for the
software in the reflection phase

You might also like