The quartiles of a ranked set of data values are three points which divide the data into exactly four equal parts, each part comprising of 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=13
Input : 1, 3, 4, 5, 5, 6, 7, 11
Output : 3
C++
#include <bits/stdc++.h>
using namespace std;
int median( int * a, int l, int r)
{
int n = r - l + 1;
n = (n + 1) / 2 - 1;
return n + l;
}
int IQR( int * a, int n)
{
sort(a, a + n);
int mid_index = median(a, 0, n);
int Q1 = a[median(a, 0, mid_index)];
int Q3 = a[mid_index + median(a, mid_index + 1, n)];
return (Q3 - Q1);
}
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);
return 0;
}
|
Java
import java.io.*;
import java .util.*;
class GFG
{
static int median( int a[],
int l, int r)
{
int n = r - l + 1 ;
n = (n + 1 ) / 2 - 1 ;
return n + l;
}
static int IQR( int [] a, int n)
{
Arrays.sort(a);
int mid_index = median(a, 0 , n);
int Q1 = a[median(a, 0 ,
mid_index)];
int Q3 = a[mid_index + median(a,
mid_index + 1 , n)];
return (Q3 - Q1);
}
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
def median(a, l, r):
n = r - l + 1
n = (n + 1 ) / / 2 - 1
return n + l
def IQR(a, n):
a.sort()
mid_index = median(a, 0 , n)
Q1 = a[median(a, 0 , mid_index)]
Q3 = a[mid_index + median(a, mid_index + 1 , n)]
return (Q3 - Q1)
if __name__ = = '__main__' :
a = [ 1 , 19 , 7 , 6 , 5 , 9 , 12 , 27 , 18 , 2 , 15 ]
n = len (a)
print (IQR(a, n))
|
C#
using System;
class GFG
{
static int median( int []a,
int l, int r)
{
int n = r - l + 1;
n = (n + 1) / 2 - 1;
return n + l;
}
static int IQR( int [] a, int n)
{
Array.Sort(a);
int mid_index = median(a, 0, n);
int Q1 = a[median(a, 0,
mid_index)];
int Q3 = a[mid_index + median(a,
mid_index + 1, n)];
return (Q3 - Q1);
}
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));
}
}
|
PHP
<?php
function median( $a , $l , $r )
{
$n = $r - $l + 1;
$n = (int)(( $n + 1) / 2) - 1;
return $n + $l ;
}
function IQR( $a , $n )
{
sort( $a );
$mid_index = median( $a , 0, $n );
$Q1 = $a [median( $a , 0, $mid_index )];
$Q3 = $a [ $mid_index + median( $a , $mid_index + 1, $n )];
return ( $Q3 - $Q1 );
}
$a = array ( 1, 19, 7, 6, 5, 9,
12, 27, 18, 2, 15 );
$n = count ( $a );
echo IQR( $a , $n );
?>
|
Javascript
<script>
function median(a, l , r)
{
var n = r - l + 1;
n = parseInt((n + 1) / 2) - 1;
return parseInt(n + l);
}
function IQR(a , n)
{
a.sort((a,b)=>a-b);
var mid_index = median(a, 0, n);
var Q1 = a[median(a, 0,
mid_index)];
var Q3 = a[mid_index + median(a,
mid_index + 1, n)];
return (Q3 - Q1);
}
var a = [1, 19, 7, 6, 5, 9,
12, 27, 18, 2, 15];
var n = a.length;
document.write(IQR(a, n));
</script>
|
Output:
13
Time Complexity: O(1)
Auxiliary Space: O(1)
Reference
https://en.wikipedia.org/wiki/Interquartile_range
If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
27 Aug, 2022
Like Article
Save Article