# Pivot partitioning by scanning

Algorithmic problem: Pivot partioning

Prerequisites: None

Type of algorithm: loop

Auxiliary data: Five index pointers, $m_1,m_2,i_1,i_2,i_3 \in \mathbb N.$

## Abstract View

Invariant:

1. The values of $m_1$ and $m_2$ only depend on the input sequence $S$ and are, hence, constant throughout the loop. More specifically, in $S$ there are
1. $m_1-1$ elements less than $p$,
2. $m_2-m_1$ elements equal to $p$, and
3. $n - m_2+1$ elements greater than $p$.
2. Before and after each iteration, it is:
1. $1 \le i_1 \le m_1 \le i_2 \le m_2 \le i_3 \le n+1$;
2. $S[j] \lt p$ for $j \in \{1,...,i_1-1\}$; if $i_1 \lt m_1$, it is $S[i_1] \nless p$;
3. $S[j] =p$ for $j \in\{m_1,...,i_2-1\}$; if $i_2 \lt m_2$, it is $S[i_2] \ne p$;
4. $S[j] \gt p$ for $j \in \{m_2,...,i_3-1\}$; if $i_3 \le n$, it is $S[i_3] \ngtr p$.

Variant: At least one of $i_1,i_2$, and $i_3$ is increased by at least $1$, none of them is decreased.

Break condition: It is $i_1=m_1, i_2=m_2$ and $i_3 = n+1$.

## Induction basis

Abstract view:

1. Set $m_1$ and $m_2$ according to the loop invariant.
2. Initialize $i_1,i_2$ and $i_3$.

Implementation:

1. Set $m_1:=1$ .
2. For $j \in {1,...,n}$: If $S[j]\lt p$ increase $m_1$ by $1$.
3. Set $m_2:=m_1$.
4. For $j \in {1,...,n}$: If $S[j]=p$ increase $m_2$ by $1$.
5. $i_1:=1$.
6. $i_2:=m_1$.
7. $i_3:=m_2$.
8. While $i_1\lt m_1$ and $S[i_1]\lt p$, set $i_1:=i_1+1$.
9. While $i_2\lt m_2$ and $S[i_2]=p$ , set $i_2:=i_2+1$.
10. While $i_3\lt n+1$ and $S[i_3]\gt p$, set $i_3:=i_3+1$ .

Proof: Obvious.

## Induction step

Abstract view:

1. If the algorithm is not done yet, identify two out of the three index pointers $i_1,i_2$, and $i_3$such that at least one of these two can be moved one step forward after a swap of the elements at these two positions.
2. Then move each of $i_1,i_2$, and $i_3$ forward as long as the invariant is not violated.

Implementation:

1. If $i_1=m_1$ and $i_2=m_2$ and $i_3=n+1$, terminate the algorithm and return $m_1,m_2$.
2. If $i_1\lt m_1$ :
1. If $S[i_1]=p$:
1. Swap $S[i_1]$ and $S[i_2]$.
2. Set $i_2:=i_2+1$ until $i_2=m_2$ or else $S[i_2] \ne p$.
2. Otherwise (that is, $S[i_1]\gt p$):
1. Swap $S[i_1]$ and $S[i_3]$.
2. Set $i_3:=i_3+1$ until $i_3=n+1$ or else $S[i_3] \ngtr p$.
3. While $i_1\lt m_1$ and $S[i_1]\lt p$: set $i_1:=i_1+1$.
3. Otherwise (that is, $i_1=m_1$):
1. Swap $S[i_2]$ and $S[i_3]$.
2. Set $i_2:=i_2+1$ until $i_2=m_2$ or else $S[i_2] \ne p$ .
3. Set $i_3:=i_3+1$ until $i_3=n+1$ or else $S[i_3] \ngtr p$.

Correctness: Due to the various "otherwise" clauses, all possible cases are covered. Therefore, it remains to show that the invariants are maintained in each of the considered cases. As the values of $m_1$ and $m_2$ are never changed after initialization, all three parts of the first invariant are trivially maintained. Since $i_1$ (resp., $i_2$, $i_3$) is only increased in case $i_1\lt m_1$ (resp., $i_2\lt m_2$, $i_3\lt n+1$), Invariant 2.1 is maintained as well. So, we will focus on Invariants 2.2-2.4. If Step 2 applies, maintenance of Invariants 2.2-2.4 might be obvious. So consider Step 3. If Step 3 applies ($i_1=m_1$), the break condition is not yet met, so we know $i_2\lt m_2$ or $i_3\lt n+1$, or both. Without loss of generality, assume $i_2\lt m_2$ (the other case is mirror-symmetric). Invariant 2.3 implies $S[i_2]\neq p$, which amounts to $S[i_2]\gt p$ in Step 3. So, not all elements $\gt p$ are on the correct positions, which implies $i_3\lt n+1$. In summary, it is safe to apply Step 3.

## Java Implementation

    //snipped from https://foo.algo.informatik.tu-darmstadt.de
//preperated for Spec
//AUTHOR: Thomas Lüdecke
//30.05.2015 05:22 | git 4072e171

private List<Integer> list executePivotPartitioning(int pivot, int m1, int m2,  List<Integer> list){
//init and save
int i1 = 0;
int i2 = m1;
int i3 = m2;

//int i = 0; //Iteration Counter
//Inductionbase
while(i1 < m1 && list.get(i1) < pivot){
i1++;
}

while(i2 < m2 && list.get(i2) == pivot){
i2++;
}

while(i3 <list.size() && list.get(i3) > pivot){
i3++;
}

//each Iteration is one induction step
while(i1 < m1 || i2 < m2 || i3 < list.size()) {

if(i1 < m1) {
if(list.get(i1) == pivot) {
swap(i1,i2, list);
while(i2 < m2 && list.get(i2) == pivot){
i2++;
}
}else{
swap(i1,i3, list);
while(i3 <list.size() && list.get(i3) > pivot){
i3++;
}
}
while(i1 < m1 && list.get(i1) < pivot){
i1++;
}
//i++; //iteration end
}else{
swap(i2,i3, list);
while(i2 < m2 && list.get(i2) == pivot){
i2++;
}
while(i3 <list.size() && list.get(i3) > pivot){
i3++;
}
//i++; //iteration end
}
}
return list;
}

//AUTHOR: Alexander Jandousek
private void swap(int i1, int i2, List<Integer> list){
int temp = list.get(i1);
list.set(i1, list.get(i2));
list.set(i2, temp);
}

//These methods are used to init m1 and m2 before calling executePivotPartitioning(...)
//AUTHOR: Alexander Jandousek
private int calcM1(int pivot,  List<Integer> list){
int m1 = 0;
for(int n : list){
m1 = (n < pivot)? m1+1: m1;
}
return m1;
}

//AUTHOR: Alexander Jandousek, Thomas Lüdecke
private int calcM2(int pivot,  List<Integer> list){
int m2 = calcM1(pivot,list); //not optimized
for(int n : list){
m2 = (n == pivot)? m2+1: m2;
}
return m2;
}