CS Principles: Chapter 6 Study Guide

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

What are some problems (either ones we addressed in class or others) that might require sorting, shuffling, and/or searching data?

- Pokedex project - Sort students in PS - Excel/Sheets - UNO game - Simulation - Online Shopping App - Old Scheduler App

When merge sorting a list of 1,000,000 elements, the merge sort algorithm divides the list 20 times before the list has been reduced to one element. How many divisions would be needed for a list of 2,000,000 elements?

21

What is a divide-and-conquer algorithm?

A divide-and-conquer algorithm solves a problem by dividing the problem into smaller subproblems, and then solving those smaller (and easier) problems. The problem is typically subdivided until it reaches a size that's trivially easy to solve.

Which is faster: adding an item to the start of a list or adding an item to the end of a list?

Adding an item to the end of the list is faster. This is because if you add an item to the start of the list, the computer has to shift all the items already in the list over by one position.

What does it mean for a shuffling algorithm to be biased? In general, is this an advantage or a disadvantage?

Bias means that some shuffles are more likely than others. For example if an algorithm shuffles the list [1, 2, 3] and the outcome [3, 2, 1] is more likely than [3, 1, 2] that would indicate a bias. Generally bias is considered undesirable because shuffling should make the order of the list completely and unpredictably random

Do you think that Python's built-in library to search a list (with the in keyword) uses linear search or binary search? Why?

It uses linear search (even though binary search is faster), because there's no guarantee that the list you give it to search will be sorted. Remember that binary search requires the list to be sorted.

Which algorithms that we studied are divide-and-conquer algorithms?

Merge sort and binary sort

Which is faster: bubble sort or merge sort?

Merge sort, generally faster (except in very special cases; 99 vs <1% of cases)

Which sorting algorithm do you think is used by the sort method of lists? Why?

Probably merge sort or similar. NOT bubble sort (inefficient)

Which is faster: searching a sorted list or searching an unsorted list?

Searching a sorted list is faster (assuming you binary search)

How can you search a list?

if x in ls: print('This list contains x') else: print('This list does not contain x') Use an accumulator to know how many of x are in the list.

How can you sort a list using Python's built-in sorting?

ls.sort()

How can you shuffle a list?

random.shuffle(ls)


Kaugnay na mga set ng pag-aaral

История Казахстана 2023

View Set

Microeconomics Practice Questions: Demand

View Set

2. Legal Concepts, Risk Management , and Ethical Issues

View Set