Selection sort: Difference between revisions
No edit summary |
|||
Line 46: | Line 46: | ||
'''Proof:''' The asymptotic complexity of the <math>i</math>-th iteration is in <math>\Theta(T\cdot(n - i))</math>. Therefore, the total complexity is in <math>\Theta\left(T\cdot\sum_{i=1}^{n-1} (n-i) \right) = \Theta\left(T\cdot\sum_{i=1}^{n-1} i \right) = \Theta\left(T\cdot\frac{n(n-1)}{2} \right) = \Theta\left(T\cdot n^2\right)</math>. | '''Proof:''' The asymptotic complexity of the <math>i</math>-th iteration is in <math>\Theta(T\cdot(n - i))</math>. Therefore, the total complexity is in <math>\Theta\left(T\cdot\sum_{i=1}^{n-1} (n-i) \right) = \Theta\left(T\cdot\sum_{i=1}^{n-1} i \right) = \Theta\left(T\cdot\frac{n(n-1)}{2} \right) = \Theta\left(T\cdot n^2\right)</math>. | ||
== Java Code == | |||
<syntaxhighlight lang="java"> | |||
/** | |||
int[] selectionSort(int [] list) { | |||
int n = list.length - 1; | |||
int left = 0; | |||
int min; | |||
int tmp; | |||
while(left < n) { | |||
min = left; | |||
for(int i = left + 1; i<=n; i++) { | |||
if (list[i] < list[min]) { | |||
min = i; | |||
} | |||
} | |||
tmp = list[min]; | |||
list[min] = list[left]; | |||
list[left] = tmp; | |||
left = left + 1; | |||
} | |||
} | |||
</syntaxhighlight> |
Revision as of 15:46, 19 June 2015
General information
Algorithmic problem: Sorting based on pairwise comparison
Type of algorithm: loop
Abstract view
Invariant: After [math]\displaystyle{ i \geq 0 }[/math] iterations: The elements at positions [math]\displaystyle{ |S|-i+1,\dots,|S| }[/math] are correctly placed in sorting order.
Variant: [math]\displaystyle{ i }[/math] increases by [math]\displaystyle{ 1 }[/math].
Break condition: [math]\displaystyle{ i = |S| - 1 }[/math].
Induction Basis
Abstract view: Nothing to do.
Implementation: Nothing to do.
Proof: Nothing to show.
Induction step
Abstract view: Identify the maximum out of [math]\displaystyle{ S[1],\dots,S[|S|-i+1] }[/math] and then move it, by a swap, to position [math]\displaystyle{ |S|-i+1 }[/math].
Implementation:
- Set [math]\displaystyle{ m := 1 }[/math]
- For all [math]\displaystyle{ j=2,\dots,|S|-i+1 }[/math]: If [math]\displaystyle{ S[j] \gt S[m] }[/math] set [math]\displaystyle{ m := j }[/math].
- Swap [math]\displaystyle{ S[m] }[/math] and [math]\displaystyle{ S[|S|-i+1] }[/math].
Correctness: Obviously: [math]\displaystyle{ S[m] }[/math] is the maximum element out of [math]\displaystyle{ S[1],\dots,S[j] }[/math].
Complexity
Statement: The asymptotic complexity is in [math]\displaystyle{ \Theta(T\cdot n^2) }[/math] in the best and worst case, where [math]\displaystyle{ T }[/math] is the complexity of the comparison.
Proof: The asymptotic complexity of the [math]\displaystyle{ i }[/math]-th iteration is in [math]\displaystyle{ \Theta(T\cdot(n - i)) }[/math]. Therefore, the total complexity is in [math]\displaystyle{ \Theta\left(T\cdot\sum_{i=1}^{n-1} (n-i) \right) = \Theta\left(T\cdot\sum_{i=1}^{n-1} i \right) = \Theta\left(T\cdot\frac{n(n-1)}{2} \right) = \Theta\left(T\cdot n^2\right) }[/math].
Java Code
/**
int[] selectionSort(int [] list) {
int n = list.length - 1;
int left = 0;
int min;
int tmp;
while(left < n) {
min = left;
for(int i = left + 1; i<=n; i++) {
if (list[i] < list[min]) {
min = i;
}
}
tmp = list[min];
list[min] = list[left];
list[left] = tmp;
left = left + 1;
}
}