CSC 231 recursion, hashing, sorting

¡Supera tus tareas y exámenes ahora con Quizwiz!

Why does selection sort improve on bubble sort?

By making only one exchange for every pass through the list

What is the folding method

-Constructs hash functions by dividing the item into equal-size pieces (the last piece may not be of equal size). -Pieces are then added together to give the resulting hash value -ex) item=436-555-4601 divide digits into groups of 2, add them to together to get 210. 210% # of slots

For a binary search, what is a must?

-The items must be sorted -Random access to items in the list -use sequential search

What are pythons 2 built in sorting methods for lists?

-list.sort() -sorted(list)

The minimum number of fields or instance variables with each node of a DLL is: a) 1 b) 2 c) 3 d) 4

C) 3

Explain how binary search works

-sorted list -starts by examining middle term -If item is middle, stop -If the item we are searching for is less than the middle item, we can simply perform a binary search of the left half.

Explain mid-square method for hashing

-square the item, extract some portion of result -ex) item=77 77^2=5929 (take middle #'s) 92%11 (11=# of slots) = slot 4 ex) item=31 31^2=961 slot=6 ex) item=17 17^2=289 slot=8

Explain hash function using remainders ex) item= 54

-take an item and divides it by the table size, returning the remainder as it's half value -Item%table size

The efficiency of a sorting algorithm depends on what 2 things?

Comparisons and movements

A recursive function has 3 properties/laws

1) A base case in which the return value of the function is specified for one or more values of its parameter 2) Recursive step in which the current return value of the function is defined in a terms previously defined function values and/or parameter values. This recursive step must make progress towards the base case. 3) Functon that calls itself

How does quick sort work?

1) Pick a pivot(number) 2) Divide into less than and greater than pivot (groups that are less than pivot or number and groups greater than pivot/number) 3)) sort each side recursively

In a DLL, the number of pointers affected for an insertion operation at the beginning or end of a DLL will be: 4,0,1,2

2

What is the output of the following code and explain it: def some_func(n): if n <= 1: return n else: return(some_func(n-1) + some_func(n-2)) print(some_func(4)) answer options: 3, 5, 2 or 8

3

How many recursive calls are made when computing the sum of the list [2,4,6,8,10]?

4

In a DLL, the number of pointers affected for an insertion operation in the middle of the doubly linked list will be: 4, 0 , 1, 6

4

Suppose you have the following sorted list [3, 5, 6, 8, 11, 12, 14, 15, 17, 18] and are using the recursive binary search algorithm. Which group of numbers correctly shows the sequence of comparisons used to search for the key 16? a) 11,14,17 b) 18,17,15 c),14,17,15 d)12,17,15

D

Suppose you are doing a sequential search of the ordered list [3, 5, 6, 8, 11, 12, 14, 15, 17, 18]. How many comparisons would you need to do in order to find the key 13?

7 comparisons since 14 is greater than the key value 13. You do not need to search the entire list, since it is ordered you can stop searching when you have compared with a larger value than the key.

What is Linear Search or Sequential Search

A method for finding a particular value in a list that checks each element in sequence until the desired element is found or the list is exhausted

Is exchanging or shifting operation better?

A shifting operation would be better because it requires approximately a third of the processing work

Is bubble sort stable?

A stable sort

Why use Insertion Sort?

Algorithm of choice for small lists because the act of creating the sorting sublists helps minimize the numbers of swaps

Suppose you have the following sorted list [3, 5, 6, 8, 11, 12, 14, 15, 17, 18] and are using the recursive binary search algorithm. Which group of numbers correctly shows the sequence of comparisons used to find the key 8. a) 11,5,6,8 b)12,6,11,8 c)3,5,6,8 d) 18,12,6,8

B

Best to worse operations for running times

Best-O(1) log n. n log n O(n^2) O(2^n) O(n!)

How are merge sort and quick sort similar?

Both break down numbers into groups

Consider an implementation of an unsorted singly linked list. Suppose it has its representation with a head and a tail pointer (i.e. pointers to the first and last nodes of the linked list). (Reminder: single linked list does not have a built in previous pointer). Given the representation, which of the following operation can not be implemented in O(1) time ? a) Insertion at the front of the LL b) insertion at the end of the linked list c) deletion of the front node of the LL d) deletion of the last node of the linked list

D) Deleting of the last node of the linked list

What does divide and conquer mean?

Divides the problem into smaller pieces, solve the smaller pieces in some way, and then reassemble the whole problem to get the result.

what does it mean if a algorithm happens in place?

Doesn't use a lot of buffer speed.

What is the best case scenario for a sequential list?

First item

Best case in Binary Search when target is present

First mid

What's another method used in hashing?

Folding

Difference between LL and DLL?

LL: Requires less memory, cannot be iterated in reverse -good for insertion/deletion and not searching elements DLL: Can be iterated in forward and reverse direction -requires mores memory for storage. -good when need to search for elements

What will binary search not work on?

Linked lists

What is sorting

The process of rearranging elements in a list into a specific order, usually their "natural ordering"

What is a stable sort

items with the same value maintain their relative order in the list -whether identical items keep their original position - if you have duplicates, do they stay in their same order

Difference betweenn O(1) and O(n) in LL and DLL?

O(1) keeps up with length O(n) going through and counting. Start from beginning and loop through entire list and count how many nodes are in there

Space complexity for LL and DLL?

O(n)

What is the BIG O case if the target number is not in a list?

O(n)

What big O is selection sort?

O(n^2

What big O is bubble sort?

O(n^2)

Is merge sort in-place or an out-place sort and why?

Out-of-place sort because it requires extra space to hold the two halves as they are extracted with the slicing operations

MergeSort does what?

Recursively divides the list in half until it has a bunch of lists of size <=1 then merges the lists and puts things in order

What does sorted(list) do

Returns new copy of list with the elements sorted

Difference between Sequential search and smart sequential search?

SSS is sorted SS is not sorted

Is selection sort a stable sort?

Selection sort is not a stable sort

Worst case in binary search when target is present?

last comparsison -log n

What sorts are stable and what sorts are unstable

Stable: Bubble, insertion, merge Unstable: Quick sort and selection sort

When target is absent, what is the best case in binary search?

log n

Describe a hash function

Takes the state of an object(the object's value)) applies math operations or another scheme to create a hash(often a number) to identify the object

When target is absent, what is the worst case in binary search?

log n

The best case for an insertion sort is O(n). Explain why.

The best case is when only one comparison needs to be done on each pass. The best case would apply for an already sorted list.

What does selection sort do

Looks for the largest element, moves to the rear -minimizes the number of swaps

What is bubble sort

Makes multiple passes throughout the list -Compares adjacent items and exchanges those that are out of order -each pass places the next largers value in its proper place

Why would we care about an algorithm being in place or not?

Memory -not a lot of space, want to utilize a method

What operator can we use in hashing?

Modulo

The searching operation for hash is O(?)

O(1)

A problem solved with recursion can also be solved using iteration? True or False?

True

What does the hash function do?

Will take any items in the collection and return an integer in the range of slot names between 0 and m-1.

Can we make a unstable sort stable?

Yes by changing the value comparison operation so that the comparison of the two values considers position as as factor for objects with two equal values

Is merge sort a stable sort and why?

Yes. The statement left half[I]<=righthalf[j] ensures that the sorting method is stable

Suppose you have the following list of numbers to sort: <br> [19, 1, 9, 7, 3, 10, 13, 15, 8, 12] which list represents the partially sorted list after three complete passes of bubble sort?

[1,3,7,9,10,8,12,13,15,19]

Given the following list of numbers: <br> [21, 1, 26, 45, 29, 28, 2, 9, 16, 49, 39, 27, 43, 34, 46, 40] <br> which answer illustrates the list to be sorted after 3 recursive calls to mergesort?

[21,1] -a merge sort will continue to recursively move toward the beginning of the list until it hits a base case

Given the following list of numbers: <br> [21, 1, 26, 45, 29, 28, 2, 9, 16, 49, 39, 27, 43, 34, 46, 40] <br> which answer illustrates the first two lists to be merged?

[21] and [1]

A hash table is...

a collection of items which are stored in such a way as to make It easy to find them layer.

When two or more items end up in the same spot using the hash function, this is called...

a collision

What is quick sort commonly implemented as?

a recursive, divide and conquer algorithm

A perfect hash function provides..

a unique hash for all possible values

Linked Lists Big-O for add, append, pop, pop(pos), remove, search, is empty, size

add(item) and is_empty():O(1) append, pop, remove, search, size: O(n)

What is a way to always have a perfect hash function?

increase the size of the hash table so that each possible value in the item range will have a unique slot -not practical for large lists such as social security numbers; would require a billion slots

In a sequential search of an unordered list, when the item is not present the best case and worst case are...

base case= n worst case=n

Best, worst and average case for merge sort?

best case: O(n * log n) worst case: O(n* log n) average case:O( n * log n)

Best, worst and average case for quick sort?

best case: O(n * log n) worst case: O(n^2) average case:O(n * log n)

Best, worst and average case for smart bubble sort?

best case: O(n) worst case: O(n^2) average case:O(n^2)

Best, worst and average case for selection sort?

best case: O(n^2) worst case: O(n^2) average case: O(n^2)

Best, worst and average case for bubble sort?

best case: O(n^2) worst case:O(n^2) average case:O(n^2)

Best, worst and average case for insertion sort?

best case:O(n) worst case: O(n^2) average case: O(n^2)

In a sequential search of an ordered list, when the item is present the best case and worst case are...

best case=1 worst case=n

In a sequential search of an unordered list, when the item is present the best case is and worst case are...

best case=1 worst case=n

what is clustering?

if many collisions occur at the same hash value, a number of surrounding slots will be filled by the linear probing resolution

What are the 2 main sorting algorithms?

bubble sort and selection sort

A recursive function is a function that...

calls itself

What is the disadvantage to linear probing?

clustering/items become clustered in the table

How we would take the string "cat" and make it a hash value?

in code: ord ('c') output=99 ord('a') output=97 ord('t') output=116 99+97+116=312 312 % 11(# of slots) =4

What is a base case?

condition that allows the algorithm to stop recursing (stop iteration/loop)

Merge sort is what kind of algorithm?

divide-and-conquer algorithm

The mapping between an item and the slot where that item belongs in the hash table is called the...

hash function

Why use merge sort?

it maintains its big O notation regardless of the order of items in the list you are sorting

Recursion is:

method of problem solving that involves breaking a problem down into smaller and smaller subproblems until you get to a small enough problem that it can be solved trivially. -When a function calls itself -to solve smaller-and-smaller problems until it reaches the base case -then all the functions return and results are joined together

In the worst case, the number of comparisons needed to search a singly linked list of length n for a given element is: long, n/2, n^2 or n

n

In a sequential search of an ordered list, when the item is not present the best case and worst case are...

n and n

In a bubble sort, if there are n items in a list, how many pairs of items need to be compared on the first pass?

n-1

Suppose you are going to write a recursive function to calculate the factorial of a number. fact(n) returns n*n-1* n-2*... Where the factorial of zero is defined to be 1. What would the most appropriate base case?

n<=1 is most efficient, keeps program from crashing if you try to compute the factorial of a negative number

What are the 2 orders of sorting

numerical order or alphabetic order

A hash function that maps each item into a unique slot Is referred to as a..

perfect hash function

Pros and cons of quick sort

pro: can be must faster than merge sort and uses less memory con: can be weak when data is ordered

Each position of the hash table, often called a ____, can hold an item and is named by an integer value starting at 0.

slot

What built-in sorts use a version of Merge sort?

sorted() and list.sort()

What is the difference between list.sort() and sorted(list)

sorted(list) returns a new copy of the list with the elements sorted -list.sort() doesn't return a new copy but changes the current list

What does list.sort() do

sorts that list in-place -does not return anything but changes the ordering of the elements in list

Open Addressing with linear probing

when a collision occurs the calculated hash index is incremented by one. -open addressing is a collision resolution process that tries to find the next open slot to address in the hash table. -By systematically visiting each slot one at a time, we are performing an open addressing technique called linear probing

Stack Overflow

when the computer is out of memory to allocate variables and arguments -Ifinite loop leads to stack overflow


Conjuntos de estudio relacionados

Day 12/13/14/15/16/17/19 skipped 18 Topology

View Set

31. Their Eyes Were Watching God: Themes

View Set

Extension of the Vertebral Column: Synergist & Antagonist Muscles

View Set

Students With Disabilities CST NYSTCE (060)

View Set

Photosynthesis and celluar respiration

View Set