# CS21: Quiz 6 Study Guide

### In addition to the concepts below, you should also know the concepts that were tested on all of the previous quizzes: Quiz 1, Quiz 2, Quiz 3, Quiz 4, Quiz 5.

#### You should be able to define and explain the following terms:

• Sorting: selection, bubble, insertion
• Analysis of algorithms (number of steps)
• Big-O notation (O(N) vs O(logN), etc)
• Recursion and iteration
• Merge sort (uses recursion)

#### You should understand and be able to use the following Python concepts:

• complexity (big O, number of steps) of selection/bubble/insertion/merge sort algorithms
• recursive function base case
• how variables are stored and function arguments are passed on the stack for recursive functions

#### Practice problems:

1. If you are running selectionSort() on the following list, L:
```L = [9, 35, 18, 200, 0, 6, 43, 21]
```
Which of the following represents L after 3 iterations of the outer loop in selectionSort()?
```A. [0, 6, 9, 18, 21, 35, 43, 200]
B. [0, 6, 9, 200, 18, 35, 43, 21]
C. [0, 9, 35, 18, 200, 6, 43, 21]
D. [0, 6, 9, 35, 200, 18, 43, 21]
```
2. Describe the sorting algorithms we studied in class and show the above list, L, as it is being sorted, using each algorithm.
3. Write two versions of each of the following functions...one version using iteration and one using recursion:
• sum all values in a given list of integers
• return True if a list is sorted/in order, False if not
• count/return how many times a given value (v) is found in a list (L)
• find the largest number in a list
4. Draw a stack diagram for the following program. Show what the stack would look like at it's largest point.
```def main():
n = 5
ans = factorial(n)
print ans

def factorial(x):
if x == 0:
return 1
else:
return x * factorial(x-1)

main()
```
[Answer (but red arrows aren't needed -- they just show where things get returned)]
{"found_math": false, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "url": "https://web.cs.swarthmore.edu/~knerr/teaching/s15/Quizzes/quiz6.php", "fetch_time": 1720987234000000000, "content_mime_type": "text/html", "warc_filename": "crawl-data/CC-MAIN-2024-30/segments/1720763514638.53/warc/CC-MAIN-20240714185510-20240714215510-00339.warc.gz", "warc_record_offset": 553952115, "warc_record_length": 1984, "token_count": 540, "char_count": 1879, "score": 3.65625, "int_score": 4, "crawl": "CC-MAIN-2024-30", "snapshot_type": "latest", "language": "en", "language_score": 0.8522970676422119, "file_path": "/sailhome/yjruan/project/latent-lm/data/processed/finemath-4plus/finemath-4plus/train-00043-of-00064.parquet"}