Interquartile Range (IQR)
The quartiles of a ranked set of data values are three points that divide the data into exactly four equal parts, each part comprising quarter data.
- Q1 is defined as the middle number between the smallest number and the median of the data set.
- Q2 is the median of the data.
- Q3 is the middle value between the median and the highest value of the data set.
The interquartile range IQR tells us the range
where the bulk of the values lie. The interquartile
range is calculated by subtracting the first quartile
from the third quartile.
IQR = Q3 - Q1
Uses
1. Unlike range, IQR tells where the majority of data lies and is thus preferred over range.
2. IQR can be used to identify outliers in a data set.
3. Gives the central tendency of the data.
Examples:
Input : 1, 19, 7, 6, 5, 9, 12, 27, 18, 2, 15
Output : 13
The data set after being sorted is
1, 2, 5, 6, 7, 9, 12, 15, 18, 19, 27
As mentioned above Q2 is the median of the data.
Hence Q2 = 9
Q1 is the median of lower half, taking Q2 as pivot.
So Q1 = 5
Q3 is the median of upper half talking Q2 as pivot.
So Q3 = 18
Therefore IQR for given data=Q3-Q1=18-5=13Input : 1, 3, 4, 5, 5, 6, 7, 11
Output : 3
Below is the implementation of the above approach:
#include <iostream>
#include <algorithm>
using namespace std;
// Function to give
// index of the median
int median(int l, int r){
int n = r - l + 1;
n = (n + 1) / 2 - 1;
return n + l;
}
// Function to
// calculate IQR
int IQR(int a[], int n){
sort(a, a + n);
// Index of median
// of entire data
int mid_index = median(0, n - 1);
// Median of first half
int Q1;
if (n % 2 == 0)
Q1 = a[median(0, mid_index)];
else
Q1 = a[median(0, mid_index - 1)];
// Median of second half
int Q3;
if (n % 2 == 0)
Q3 = a[median(mid_index + 1, n - 1)];
else
Q3 = a[median(mid_index + 1, n)];
// IQR calculation
return (Q3 - Q1);
}
// Driver Code
int main() {
int a[] = {1, 19, 7, 6, 5, 9, 12, 27, 18, 2, 15};
int n = sizeof(a) / sizeof(a[0]);
cout << IQR(a, n) << endl;
return 0;
}
// Java program to find
// IQR of a data set
import java.io.*;
import java.util.*;
class GFG{
// Function to give
// index of the median
static int median(int l, int r){
int n = r - l + 1;
n = (n + 1) / 2 - 1;
return n + l;
}
// Function to
// calculate IQR
static int IQR(int [] a, int n){
Arrays.sort(a);
// Index of median
// of entire data
int mid_index = median(0, n - 1);
// Median of first half
int Q1;
if (n % 2 == 0)
Q1 = a[median(0, mid_index)];
else
Q1 = a[median(0, mid_index - 1)];
// Median of second half
int Q3;
if (n % 2 == 0)
Q3 = a[median(mid_index + 1, n - 1)];
else
Q3 = a[median(mid_index + 1, n)];
// IQR calculation
return (Q3 - Q1);
}
// Driver Code
public static void main (String[] args) {
int []a = {1, 19, 7, 6, 5, 9,
12, 27, 18, 2, 15};
int n = a.length;
System.out.println(IQR(a, n));
}
}
# Python3 program to find IQR of
# a data set
# Function to give index of the median
def median(l, r):
n = r - l + 1
n = (n + 1) // 2 - 1
return n + l
# Function to calculate IQR
def IQR(a, n):
a.sort()
# Index of median of entire data
mid_index = median(0, n - 1)
# Median of first half
Q1 = a[median(0, mid_index)]
# Median of second half
if n % 2 == 0:
Q3 = a[median(mid_index + 1, n - 1)]
else:
Q3 = a[median(mid_index + 1, n - 1)]
# IQR calculation
return (Q3 - Q1)
# Driver Function
if __name__=='__main__':
a = [1, 19, 7, 6, 5, 9, 12, 27, 18, 2, 15]
n = len(a)
print(IQR(a, n))
// C# program to find
// IQR of a data set
using System;
class GFG{
// Function to give
// index of the median
static int median(int l, int r){
int n = r - l + 1;
n = (n + 1) / 2 - 1;
return n + l;
}
// Function to
// calculate IQR
static int IQR(int [] a, int n){
Array.Sort(a);
// Index of median
// of entire data
int mid_index = median(0, n - 1);
// Median of first half
int Q1 = a[median(0, mid_index)];
// Median of second half
int Q3;
if (n % 2 == 0)
Q3 = a[median(mid_index + 1, n - 1)];
else
Q3 = a[median(mid_index + 1, n)];
// IQR calculation
return (Q3 - Q1);
}
// Driver Code
public static void Main () {
int []a = {1, 19, 7, 6, 5, 9,
12, 27, 18, 2, 15};
int n = a.Length;
Console.WriteLine(IQR(a, n));
}
}
// This code is contributed
// by anuj_67.
// JavaScript program to find
// IQR of a data set
// Function to give
// index of the median
function median(l, r) {
let n = r - l + 1;
n = Math.floor((n + 1) / 2) - 1;
return n + l;
}
// Function to
// calculate IQR
function IQR(a, n) {
a.sort((x, y) => x - y);
// Index of median
// of entire data
let mid_index = median(0, n - 1);
// Median of first half
let Q1 = a[median(0, mid_index)];
// Median of second half
let Q3;
if (n % 2 === 0)
Q3 = a[median(mid_index + 1, n - 1)];
else
Q3 = a[median(mid_index + 1, n)];
// IQR calculation
return Q3 - Q1;
}
// Driver Code
let a = [1, 19, 7, 6, 5, 9, 12, 27, 18, 2, 15];
let n = a.length;
console.log(IQR(a, n));
Output
13
Time Complexity: O(n * log(n)), due to sorting of array.
Auxiliary Space: O(1)