Memory and Pointers
- 66Unfamiliar
- 0Learning
- 0Familiar
- 0Mastered
- spaceFlip card
- KI knew it
- DI didn't know it
- ←Previous card
- →Next card
What does RAM stand for, and how can computer memory be viewed for reasoning about arrays?
RAM stands for Random Access Memory. It can be viewed as a sequence or grid of bytes, each with an address.
Or swipe the card
66 cards
What does RAM stand for, and how can computer memory be viewed for reasoning about arrays?
RAM stands for Random Access Memory. It can be viewed as a sequence or grid of bytes, each with an address.
How does a computer access the contents of an array when searching?
It examines array elements step by step; it does not instantly take in the entire array as a person might visually scan a list.
What is linear search?
A search that examines elements in sequence until it finds the target or reaches the end.
What must linear search do in the worst case to find a target in an unsorted array of n elements?
It may have to examine all n elements.
What is binary search, and what condition does it require?
Binary search repeatedly compares the target with a middle element and discards the half where the target cannot be. The data must be sorted.
What does binary search do when the target is smaller or larger than the middle element?
If the target is smaller, it searches the left half; if larger, it searches the right half.
When does binary search return false because the target is absent?
When the search range becomes empty.
What are the worst-case running times of linear search and binary search?
Linear search takes O(n) time; binary search takes O(logn) time on sorted data.
What are the best-case running times of linear search and binary search?
Both can find the target in one comparison, so each has best-case running time Ω(1).
Why might sorting data before binary search not always make searching more efficient overall?
Sorting has a cost. Sorting first is worthwhile mainly when the data will be searched enough times for the faster searches to offset that cost.
What does Big O notation describe?
An asymptotic upper bound on an algorithm’s running time, commonly used to discuss worst-case performance.
What does Big Omega notation describe?
An asymptotic lower bound on an algorithm’s running time, often used to describe best-case performance.
What does Big Theta notation describe?
A tight asymptotic bound when an algorithm’s upper and lower bounds have the same growth rate.
How should O(n/2) be simplified in Big O notation, and why?
It is written as O(n) because constant factors do not change the asymptotic growth rate.
Why can the base of a logarithm usually be omitted in asymptotic notation?
Logarithms with different constant bases differ only by a constant factor, which Big O notation ignores.
Why does Big O notation focus on the highest-order term of a running-time expression?
As input size grows, the fastest-growing term dominates the expression; lower-order terms and constant factors matter less.
What does an algorithm’s running time measure?
The work it takes to run, measured in units such as steps or elapsed time.
Why can counting every element in a list take Θ(n) time even in the best case?
An accurate count requires examining every element, regardless of the input’s arrangement.
Why do computer scientists often emphasize an algorithm’s upper bound over its best-case lower bound?
Best-case inputs may be unusual; an upper bound gives information about how the algorithm can perform on difficult inputs.
What is the distinction between an algorithm’s correctness and its efficiency?
Correctness asks whether it produces the right result; efficiency asks how much work or time it takes.
How can a C array be initialized concisely when its values are already known?
Use a brace-enclosed initializer, such as
int numbers[] = {4, 8, 15};.Why can C compare integers with
==but not use==to compare string contents?A C string is a sequence of characters, and
==does not compare all those characters for content equality.What does the C function
strcmpreturn when two strings are equal?It returns
0.What do positive and negative return values from
strcmpindicate?A positive value indicates the first string comes later alphabetically; a negative value indicates it comes earlier.
Which header is needed to use
strcmpin C?string.h.What are the conventional return values from
mainfor success and failure in the examples?0indicates success; a nonzero value, such as1, indicates failure.Why is a phone number usually better represented as a string than as an integer?
Phone numbers may contain punctuation or a leading zero, and they are identifiers rather than quantities used in arithmetic.
What problem can arise from storing names and phone numbers in separate arrays?
Their matching entries must remain at the same indices; if the arrays get out of sync, a name can be associated with the wrong number.
What is a C
struct?A container that groups multiple data fields, potentially of different types, into one composite value.
What does
typedef structallow a programmer to do?Define a structure type and give it a convenient type name, such as
person.How are fields accessed in an ordinary C structure value?
With dot notation, such as
person.nameorperson.number.Why can grouping a person’s name and phone number in one structure improve a phone book’s design?
The related values stay together, reducing the risk that sorting or editing separate arrays will break their associations.
What is bubble sort’s basic operation?
It repeatedly compares adjacent elements and swaps them when they are out of order.
Why is bubble sort called bubble sort?
Large elements move through repeated adjacent swaps toward their correct position at the end of the list, as if bubbling upward.
What is the worst-case running time of the basic bubble sort described?
O(n2), because it makes about n passes, each involving about n comparisons.
What is the best-case running time of bubble sort if it stops after a pass with no swaps?
Ω(n), because it still must compare each adjacent pair once.
How can bubble sort avoid unnecessary passes on an already sorted list?
Track whether any swaps occurred during a pass and stop if none did.
What is selection sort’s basic strategy?
Repeatedly find the smallest element in the unsorted portion and swap it into the next position in the sorted portion.
Why does selection sort scan the unsorted portion even when the input is already sorted?
It must inspect the remaining elements to know which is smallest; the basic algorithm does not stop early just because elements appear ordered.
What is the worst-case running time of selection sort?
O(n2).
What is the best-case running time of the described selection sort?
Ω(n2), because it still searches the remaining unsorted elements on every iteration.
How many comparisons does selection sort perform in terms of the sizes of its successive unsorted portions?
It performs approximately n+(n−1)+(n−2)+⋯+1 comparisons.
What is the sum n+(n−1)+⋯+1, and what does it imply for selection sort?
The sum is 2n(n+1), which grows as n2; therefore selection sort is O(n2).
How do bubble sort and selection sort differ in how they move elements?
Bubble sort swaps neighboring elements repeatedly; selection sort finds the next smallest element and moves it directly into its final position.
Why can moving an element directly into place in selection sort be preferable to shifting many elements?
A direct swap can avoid moving every intervening element, though the algorithm still has to search for the smallest remaining element.
What is recursion?
A programming technique in which a function calls itself, typically on a smaller version of the problem.
What is a base case in a recursive function?
A condition that returns or stops without making another recursive call, preventing recursion from continuing indefinitely.
How can a pyramid of height h be defined recursively?
Draw a pyramid of height h−1, then draw one additional row; the height-zero case draws nothing.
Why must a recursive call generally work on a smaller problem?
Repeatedly shrinking the problem lets the computation eventually reach its base case.
How does a recursive pyramid-drawing function produce rows after reaching its base case?
Calls proceed down to the base case, then return in reverse order; each returning call prints its row.
What is iteration in programming?
Repeating instructions with a loop.
How does recursion differ from iteration as a way to repeat work?
Iteration uses loops; recursion repeats work through a function calling itself on a smaller problem.
What is divide and conquer?
A strategy that solves a problem by breaking it into smaller subproblems, solving those, and combining their results when needed.
What are the three main steps of merge sort?
Sort the left half, sort the right half, then merge the two sorted halves.
What is the base case for merge sort?
A list with one item is already sorted, so it can be returned without further work.
How does the merge step combine two sorted lists?
Repeatedly take the smaller of the first unprocessed elements from the two lists and append it to the merged list.
Why does merge sort require extra space in the described approach?
It uses additional memory to hold the merged result while combining the sorted halves.
How many levels of halving are needed to reduce n elements to single-element lists?
Approximately log2n levels.
Why does merge sort take O(nlogn) time?
There are about logn levels of splitting, and each level processes all n elements during merging.
What are the best- and worst-case running times of the merge sort described?
Both are Θ(nlogn) because it follows the same split-and-merge process regardless of the initial order.
How does merge sort compare asymptotically with bubble sort and selection sort?
Merge sort takes O(nlogn) time, which grows more slowly than the O(n2) time of bubble sort and selection sort for large inputs.
What is a plurality election?
An election in which the candidate with the most votes wins.
How does a runoff election use voter preferences?
Voters rank candidates, and the rankings are used to determine the winner through a runoff process.
Why can the algorithm used to count votes affect an election’s outcome?
Different vote-counting procedures can interpret the same ballots differently and produce different winners.
What broader lesson does comparing sorting algorithms illustrate?
Correctness is not the only goal: algorithm design and running time matter, especially as input sizes grow.
Why is the fastest search algorithm not necessarily the best overall choice?
Its prerequisites and setup costs matter; for example, binary search is fast but requires sorted data, which may be costly to prepare.

Ask anything
We've read this whole study set, so questions about it get the best answers.