Datastructuren Flashcards
INFODS van universiteit Utrecht (450 cards)
Satellite data
The data associated with the keys in an input array for the sorting problem.
Record
A key and satellite date together.
How does insertion sort work with a stack of cards?
Start with an empty left hand and a pile of cards on the table.
Pick up the first card and put it in your left hand.
At every time step, remove one card from the pile on the table and compare it to the right-most card in your left hand. Go from right to left until you find a card that is lower or equal than the card you picked. Insert the card you picked on the right of this card.
What does the insertion sort algorithm take as input?
An array A containing the data and an integer n representing the number of keys.
INSERTION-SORT(A, n)
for i=2 to i=n
…key = A[i]
…j = i-1
…while j>0 and A[j] > key
… … A[j+1] = A[j]
… … j = j-1
…A[j+1] = key
What does the input array A look like at each iteration of the for-loop in insertion sort?
The subarray A[1:i-1] is the sorted ‘left hand’ and the subarray A[i+1:n] is the unsorted ‘pile on the table’.
What three things do you need to prove when using a loop invariant?
- Initialization
- Maintenance
- Termination
How do you prove initialization for a loop invariant?
Prove that the loop invariant is true prior to the first loop.
How do you prove maintenance for a loop invariant?
Prove that, if the loop invariant is true before an iteration, then it will be true before the next iteration.
How do you prove termination for a loop invariant?
Prove that the loop terminates, and that the loop invariant gives a useful property when the loop terminates.
In a loop of ‘for i=2 to i=n’, what is the value of ‘i’ when the loop terminates?
i = n+1
Wat is het verschil tussen databases en datastructuren?
Bij databases bemoei je je niet met details van opslag, maar stop je vragn ‘aan de data’ in de SQL-query. Bij datastructuren probeer je beste organisatie van je data te vinden terwijl je rekening houdt met vragen die je gaat stellen.
Wat zijn de kosten van operaties in een ongesorteerde rij?
Toevoegen is makkelijk, O(1).
Zoeken is makkelijk maar tijdrovend, want je moet het query-element vergelijken met elk element, O(n).
Enumeratie
Een bepaalde handeling met elk element doen.
Wat zijn de kosten van toevoegen en zoeken in een gesorteerde rij?
Toevoegen is duur, want alles wat groter is dan x moet 1 plek opschuiven en dan moet je x daaronder invoegen, O(n). Zoeken kan snel, je kunt stoppen als je een el >x ziet.
Wat zijn de 4 componenten van een loop?
- Initialisatie
- Conditie
- Body
- Terminatie
Wat is de rekentijd van Binair zoeken?
O(lg n), logaritmisch.
Hoe bewijs je een statement ‘A=B’ met inductie?
Herschrijf één kant stapsgewijs. Zet per stap de motivatie tussen {accolades}.
Hoe bewijs je een eigenschap ‘voor alle natuurlijke getallen’ met inductie?
- Neem n=0 en bewijs dat LHS en RHS tot hetzelfde herleid worden.
- Neem aan dat P(n) is waar en bewijs dat als P(n), dan P(n+1).
Wat zijn de onderdelen van de inductiestap ‘Als P(n), dan P(n+1) bewijzen?
- Isoleer de LHS van de inductiehypothese in de LHS van het ‘te bewijzen’.
- Vervang binnen de expressie de LHS door de RHS van de inductiehypothese.
- Werk naar de RHS van het ‘te bewijzen’ toe.
What is the worst-case running time of Quicksort?
THETA(n^2)
What is the expected running time of Quicksort if all elements are distinct?
O(n lg n)
What are the three steps in the divide-and-conquer method?
- Divide an array A[p:r] in A[p : q-1] and A[q+1 : r]. The pivot is q.
- Call the algorithm recursively to each subarray.
- Combine the subarrays into A.
QUICKSORT(A, p, r)
if (p<r)
{ q = PARTITION(A,p,r);
QUICKSORT(A,p,q-1);
QUICKSORT(A, q+1, r);
}