6.2 KiB
title | TARGET DECK | FILE TAGS | tags | ||
---|---|---|---|---|---|
Insertion Sort | Obsidian::STEM | algorithm::sorting |
|
Overview
Property | Value |
---|---|
Best Case | \Omega(n) |
Worst Case | O(n^2) |
Avg. Case | O(n^2) |
Aux. Memory | O(1) |
Stable | Yes |
Adaptive | Yes |
%%ANKI
Basic
What is insertion sort's best case runtime?
Back: \Omega(n)
Reference: Thomas H. Cormen et al., Introduction to Algorithms, 3rd ed (Cambridge, Mass: MIT Press, 2009).
END%%
%%ANKI Basic What input value does insertion sort perform best on? Back: An already sorted array. Reference: Thomas H. Cormen et al., Introduction to Algorithms, 3rd ed (Cambridge, Mass: MIT Press, 2009).
END%%
%%ANKI
Basic
What is insertion sort's worst case runtime?
Back: O(n^2)
Reference: Thomas H. Cormen et al., Introduction to Algorithms, 3rd ed (Cambridge, Mass: MIT Press, 2009).
END%%
%%ANKI Basic What input value does insertion sort perform worst on? Back: An array in reverse-sorted order. Reference: Thomas H. Cormen et al., Introduction to Algorithms, 3rd ed (Cambridge, Mass: MIT Press, 2009).
END%%
%%ANKI
Basic
What is insertion sort's average case runtime?
Back: O(n^2)
Reference: Thomas H. Cormen et al., Introduction to Algorithms, 3rd ed (Cambridge, Mass: MIT Press, 2009).
END%%
%%ANKI Basic Is insertion sort in place? Back: Yes Reference: Thomas H. Cormen et al., Introduction to Algorithms, 3rd ed (Cambridge, Mass: MIT Press, 2009).
END%%
%%ANKI Basic Is insertion sort stable? Back: Yes Reference: Thomas H. Cormen et al., Introduction to Algorithms, 3rd ed (Cambridge, Mass: MIT Press, 2009).
END%%
%%ANKI Basic Is insertion sort adaptive? Back: Yes Reference: Thomas H. Cormen et al., Introduction to Algorithms, 3rd ed (Cambridge, Mass: MIT Press, 2009).
END%%
void insertion_sort(const int n, int A[static n]) {
for (int i = 1; i < n; ++i) {
int key = A[i];
int j = i - 1;
for (; j >= 0 && A[j] > key; --j) {
A[j + 1] = A[j];
}
A[j + 1] = key;
}
}
%%ANKI Basic What sorting algorithm does the following demonstrate? ! Back: Insertion sort. Reference: Thomas H. Cormen et al., Introduction to Algorithms, 3rd ed (Cambridge, Mass: MIT Press, 2009).
END%%
Loop Invariant
Consider loop-invariant P
given by
A[0..i-1]
consists of the originalA[0..i-1]
elements but in sorted order.
We prove P
maintains the requisite properties:
- Initialization
- When
i = 1
,A[0..0]
contains a single element. This trivially satisfiesP
.
- When
- Maintenance
- Suppose
P
holds for some1 ≤ i < n
. ThenA[0..i-1]
consists of the originalA[0..i-1]
elements but in sorted order. On iterationi + 1
, the nested for loop putsA[0..i]
in sorted order. At the end of the iteration,i
is incremented meaningA[0..i-1]
still satisfiesP
.
- Suppose
- Termination
- The loop ends because
i < n
is no longer true. Theni = n
. SinceP
holds, this meansA[0..n-1]
, the entire array, is in sorted order.
- The loop ends because
%%ANKI
Basic
Given array A[0..n-1]
, what is insertion sort's loop invariant?
Back: A[0..i-1]
consists of the original A[0..i-1]
elements but in sorted order.
END%%
%%ANKI Basic What is initialization of insertion sort's loop invariant? Back: Sorting starts with an singleton array which is trivially sorted. Reference: Thomas H. Cormen et al., Introduction to Algorithms, 3rd ed (Cambridge, Mass: MIT Press, 2009).
END%%
%%ANKI Basic What is maintenance of insertion sort's loop invariant? Back: Each iteration puts the current element into sorted order. Reference: Thomas H. Cormen et al., Introduction to Algorithms, 3rd ed (Cambridge, Mass: MIT Press, 2009).
END%%
%%ANKI Basic insertion sort makes fewer {comparisons} than selection sort in the average case. Reference: Thomas H. Cormen et al., Introduction to Algorithms, 3rd ed (Cambridge, Mass: MIT Press, 2009).
END%%
Analogy
Suppose you have a shuffled deck of playing cards face-down on a table. Start by grabbing a card from the deck with your left hand. For the remainder of the cards, use your right hand to transition the topmost card to the end of your left hand. If the newly placed card isn't in sorted order, move it one position closer to the start. Repeat until it's in sorted order.
If you repeat this process for every card in the deck, your left hand will eventually contain the entire deck in sorted order.
%%ANKI Basic What analogy does Cormen et al. use to explain insertion sort? Back: Sorting a shuffled deck of playing cards. Reference: Thomas H. Cormen et al., Introduction to Algorithms, 3rd ed (Cambridge, Mass: MIT Press, 2009).
END%%
%%ANKI Basic What invariant does the left hand maintain in Cormen et al.'s insertion sort analogy? Back: It contains all drawn cards in sorted order. Reference: Thomas H. Cormen et al., Introduction to Algorithms, 3rd ed (Cambridge, Mass: MIT Press, 2009).
END%%
%%ANKI Basic How does insertion sort partition its input array? Back:
[ sorted | unsorted ]
Reference: Thomas H. Cormen et al., Introduction to Algorithms, 3rd ed (Cambridge, Mass: MIT Press, 2009).
END%%
%%ANKI
Basic
How many comparisons does insertion sort typically perform with x
?
[ sorted | x : unsorted ]
Back: One plus however many elements in sorted
are greater than x
.
Reference: Thomas H. Cormen et al., Introduction to Algorithms, 3rd ed (Cambridge, Mass: MIT Press, 2009).
END%%
%%ANKI
Basic
Which element will insertion sort move to sorted
?
[ sorted | unsorted ]
Back: The first element of unsorted
.
Reference: Thomas H. Cormen et al., Introduction to Algorithms, 3rd ed (Cambridge, Mass: MIT Press, 2009).
END%%
References
- Thomas H. Cormen et al., Introduction to Algorithms, 3rd ed (Cambridge, Mass: MIT Press, 2009).