In an ordered dataset, the median is the average of the two central elements.
If the dataset is X₁, X₂, X₃, ..., X₂ᵣ with 2r (n=2r is an even number) elements, then the central elements are Xᵣ and Xᵣ₊₁.
The median is ½(Xᵣ + Xᵣ₊₁). If the median is used to calculate the mean deviation then the deviation of X₁ from the median is ½(Xᵣ + Xᵣ₊₁)-X₁, and for all the elements up to Xᵣ we can sum the deviations:
∑(½(Xᵣ + Xᵣ₊₁)-X↓i where the down-arrow means that i is a subscript and the sum is between 1 and r.
This comes to ½r(Xᵣ + Xᵣ₊₁)-∑X↓i for 1≤i≤r. Let A=∑X↓i for 1≤i≤r.
For all elements from Xᵣ₊₁ to X₂ᵣ, the sum of the deviations is ∑X↓i-½r(Xᵣ + Xᵣ₊₁) for r<i≤2r. Let B=∑X↓i for r<i≤2r.
Therefore the total sum of deviations is B-A and the mean deviation is (B-A)/2r=(B-A)/n.
If the mean µ is used to calculate the mean deviation, we don’t know where the mean lies in relation to the dataset, because the distribution could be skew, which shifts the mean to the left or right of the median. This would redefine A and B, where A is the sum of the data lower than the mean and B the sum of the data higher than the mean. The mean itself is µ=∑X↓i)/2r for 1≤i≤2r. Then we need to know k where 1≤i≤k for A and k<i≤2r for B. The mean deviation from the mean would be:
(µk-A+B-µ(2r-k))/2r=(B-A-2µ(r-k))/2r or (B-A-2µ(½n-k))/n.
This implies that, provided the mean lies between the same two data values as the median, the formula (B-A)/n applies, because k=r=n/2.