Array list: remove: Difference between revisions
No edit summary |
|||
(2 intermediate revisions by one other user not shown) | |||
Line 1: | Line 1: | ||
[[Category:Videos]] | |||
{{#ev:youtube|https://www.youtube.com/watch?v=uYTck0kDvl4|500|right||frame}} | |||
'''Algorithmic problem:''' [[Ordered sequence#Remove|Ordered sequence: remove]] | '''Algorithmic problem:''' [[Ordered sequence#Remove|Ordered sequence: remove]] | ||
Line 53: | Line 55: | ||
# Otherwise (that is, <math>p</math>.next<math>\ne</math>void and <math>K\notin\{p</math>.next.A<math>[1],...,</math>.next.A[p.next.n]<math>\}</math>), set <math>p:=p</math>.next. | # Otherwise (that is, <math>p</math>.next<math>\ne</math>void and <math>K\notin\{p</math>.next.A<math>[1],...,</math>.next.A[p.next.n]<math>\}</math>), set <math>p:=p</math>.next. | ||
'''Correctness:''' If <math>p</math>.next<math>=</math>void or <math>K\notin\{p</math>.next.A<math>[1],...,p</math>.next.A[first.n]<math>\}</math>, correctness is obvious. So consider the case that <math>p</math>.next<math>=</math>void and <math>K\in\{p</math>.next.A<math>[1],...,p</math>.next.A[<math>p</math>next.n]<math>\}</math>. | '''Correctness:''' If <math>p</math>.next<math>=</math>void or <math>K\notin\{p</math>.next.A<math>[1],...,p</math>.next.A[first.n]<math>\}</math>, correctness is obvious. So consider the case that <math>p</math>.next<math>=</math>void and <math>K\in\{p</math>.next.A<math>[1],...,p</math>.next.A[<math>p</math>.next.n]<math>\}</math>. | ||
If <math>p</math>.next.n<math>=1</math>, the array would be empty after removing <math>K</math>, so Step 2.1 is right to decouple this list item from the list. Otherwise, | If <math>p</math>.next.n<math>=1</math>, the array would be empty after removing <math>K</math>, so Step 2.1 is right to decouple this list item from the list. Otherwise, <math>K</math> is overwritten and the array is rearranged accordingly in Step 2.2. | ||
== Complexity == | == Complexity == |
Latest revision as of 23:11, 19 June 2015
Algorithmic problem: Ordered sequence: remove
Prerequisites:
Type of algorithm: loop
Auxiliary data: A pointer
Abstract view
Invariant: After
- The pointer
points to the array list item at position (or is void if no such item exists). - None of the arrays at positions
contains .
Variant: The pointer
Break condition: Either it is
Induction basis
Abstract view: If the list is empty, the algorithm terminates. Otherwise, if the key to be removed is in the first array, it is removed and the algorithm terminates again (if this was the only element of the array, that array list item is removed as well).
Implementation:
- If first
void, terminate the algorithm and return false. - Otherwise, if
first.A first.A[first.n] :- If first.n
, set first first.next. - Otherwise:
- Let
first.n such that first.A . - For
first.n (in this order): Set first.A first.A . - Set first.A
first.n void. - Set first.n:=first.n
.
- Let
- Terminate the algorithm and return true.
- If first.n
- Otherwise (that is, first
void and first.A first.A[first.n] ), set first.
Proof: If
Induction step
Abstract view: If
Implementation:
- If
.next void, terminate the algorithm and return false. - Otherwise, if
.next.A[ ],..., .A.next.A[ .next.n] :- If
.next.n , set .next .next.next. - Otherwise:
- Let
.next.n such that .next.A. . - For
.next.n (in this order): Set .next.A .next.A . - Set
.next.A .next.n void. - Set
.next.n .next.n .
- Let
- Terminate the algorithm and return true.
- If
- Otherwise (that is,
.next void and .next.A .next.A[p.next.n] ), set .next.
Correctness: If
Complexity
Statement: Linear in the length of the sequence in the worst case.
Proof: Obvious.