# Find Mean and median of an unsorted array

Here given code implementation process.

``````// C Program
// Find Mean and median of an unsorted array
#include <stdio.h>

//Display elements of given array
void printArray(int arr[], int size)
{
for (int i = 0; i < size; ++i)
{
printf("  %d", arr[i]);
}
}
//Swap the array element
void swap(int arr[], int first, int second)
{
//first and second are index of array
int temp = arr[first];
arr[first] = arr[second];
arr[second] = temp;
}
// Returns the pivot element
int pivotElement(int arr[], int low, int high)
{
//Set the high index element to its proper sorted position
int pv = arr[high];
int i = low - 1;
for (int j = low; j < high; ++j)
{
if (arr[j] < pv)
{
i++;
swap(arr, i, j);
}
}
//set the high index value to its sorted position
swap(arr, i + 1, high);
//returns the next sorting  element location
return i + 1;
}
//
void quickSort(int arr[], int low, int high)
{
if (low < high)
{
//Get the pivot element
int pv = pivotElement(arr, low, high);
quickSort(arr, low, pv - 1);
quickSort(arr, pv + 1, high);
}
}
// Calculate mean and median of given array
void meanAndMedian(int arr[], int size)
{
if (size <= 0)
{
return;
}
double result = 0.0;
// Loop controlling variable
int i = 0;
// Execute loop through by size
// And calculate sum of array elements
for (i = 0; i < size; ++i)
{
result += arr[i];
}
// Calculate mean of given array
// (Sum of element / size of array)
result = result / size;
// Display calculated mean of array
printf("\n Mean : %lf", result);
// Sort element
quickSort(arr, 0, size - 1);
if (size % 2 == 0)
{
// When have size of array are in Even length
i = size / 2;
// There is two middle element position
// Sum of two middle elements
result = arr[i] + arr[i - 1];
// Get Average
result /= 2;
printf("\n Median : %lf\n", result);
}
else
{
i = size / 2;
// When length of array are Odd size
// Only one middle element possible
printf("\n Median : %d\n", arr[i]);
}
}
int main(int argc, char const *argv[])
{
int arr1[] = {
7 ,10, 3 , 1 , 8 , 4 , 2 , 5
};
int arr2[] = {
1 , 4 , 2 , 3 , 7
};
// Get the size
int size = sizeof(arr1) / sizeof(arr1[0]);
printf(" Array elements \n");
printArray(arr1, size);
meanAndMedian(arr1, size);
// Get the size
size = sizeof(arr2) / sizeof(arr2[0]);
printf("\n Array elements \n");
printArray(arr2, size);
meanAndMedian(arr2, size);
return 0;
}``````

#### Output

`````` Array elements
7  10  3  1  8  4  2  5
Mean : 5.000000
Median : 4.500000

Array elements
1  4  2  3  7
Mean : 3.400000
Median : 3``````
``````/*
Java Program
Find Mean and median of an unsorted array
*/
public class MeanMedian
{
//Display elements of given array
public void printArray(int[] arr, int size)
{
for (int i = 0; i < size; ++i)
{
System.out.print("  " + arr[i] );
}
}
//Swap the array element
public void swap(int[] arr, int first, int second)
{
//first and second are index of array
int temp = arr[first];
arr[first] = arr[second];
arr[second] = temp;
}
// Returns the pivot element
public int pivotElement(int[] arr, int low, int high)
{
//Set the high index element to its proper sorted position
int pv = arr[high];
int i = low - 1;
for (int j = low; j < high; ++j)
{
if (arr[j] < pv)
{
i++;
swap(arr, i, j);
}
}
//set the high index value to its sorted position
swap(arr, i + 1, high);
//returns the next sorting  element location
return i + 1;
}
//
public void quickSort(int[] arr, int low, int high)
{
if (low < high)
{
//Get the pivot element
int pv = pivotElement(arr, low, high);
quickSort(arr, low, pv - 1);
quickSort(arr, pv + 1, high);
}
}
// Calculate mean and median of given array
public void meanAndMedian(int[] arr, int size)
{
if (size <= 0)
{
return;
}
double result = 0.0;
// Loop controlling variable
int i = 0;
// Execute loop through by size
// And calculate sum of array elements
for (i = 0; i < size; ++i)
{
result += arr[i];
}
// Calculate mean of given array
// (Sum of element / size of array)
result = result / size;
// Display calculated mean of array
System.out.print("\n Mean : " + result );
// Sort element
quickSort(arr, 0, size - 1);
if (size % 2 == 0)
{
// When have size of array are in Even length
i = size / 2;
// There is two middle element position
// Sum of two middle elements
result = arr[i] + arr[i - 1];
// Get Average
result /= 2;
System.out.print("\n Median : " + result + "\n");
}
else
{
i = size / 2;
// When length of array are Odd size
// Only one middle element possible
System.out.print("\n Median : " + arr[i] + "\n");
}
}
public static void main(String[] args)
{
int[] arr1 =
{
7 ,10, 3 , 1 , 8 , 4 , 2 , 5
};
int[] arr2 =
{
1 , 4 , 2 , 3 , 7
};
// Get the size
int size = arr1.length;
System.out.print(" Array elements \n");
// Get the size
size = arr2.length;
System.out.print("\n Array elements \n");
}
}``````

#### Output

`````` Array elements
7  10  3  1  8  4  2  5
Mean : 5.0
Median : 4.5

Array elements
1  4  2  3  7
Mean : 3.4
Median : 3``````
``````// Include header file
#include <iostream>
using namespace std;

/*
C++ Program
Find Mean and median of an unsorted array
*/

class MeanMedian
{
public:
//Display elements of given array
void printArray(int arr[], int size)
{
for (int i = 0; i < size; ++i)
{
cout << "  " << arr[i];
}
}
//Swap the array element
void swap(int arr[], int first, int second)
{
//first and second are index of array
int temp = arr[first];
arr[first] = arr[second];
arr[second] = temp;
}
// Returns the pivot element
int pivotElement(int arr[], int low, int high)
{
//Set the high index element to its proper sorted position
int pv = arr[high];
int i = low - 1;
for (int j = low; j < high; ++j)
{
if (arr[j] < pv)
{
i++;
this->swap(arr, i, j);
}
}
//set the high index value to its sorted position
this->swap(arr, i + 1, high);
//returns the next sorting  element location
return i + 1;
}
//
void quickSort(int arr[], int low, int high)
{
if (low < high)
{
//Get the pivot element
int pv = this->pivotElement(arr, low, high);
this->quickSort(arr, low, pv - 1);
this->quickSort(arr, pv + 1, high);
}
}
// Calculate mean and median of given array
void meanAndMedian(int arr[], int size)
{
if (size <= 0)
{
return;
}
double result = 0.0;
// Loop controlling variable
int i = 0;
// Execute loop through by size
// And calculate sum of array elements
for (i = 0; i < size; ++i)
{
result += arr[i];
}
// Calculate mean of given array
// (Sum of element / size of array)
result = result / size;
// Display calculated mean of array
cout << "\n Mean : " << result;
// Sort element
this->quickSort(arr, 0, size - 1);
if (size % 2 == 0)
{
// When have size of array are in Even length
i = size / 2;
// There is two middle element position
// Sum of two middle elements
result = arr[i] + arr[i - 1];
// Get Average
result /= 2;
cout << "\n Median : " << result << "\n";
}
else
{
i = size / 2;
// When length of array are Odd size
// Only one middle element possible
cout << "\n Median : " << arr[i] << "\n";
}
}
};
int main()
{
int arr1[] = {
7 , 10 , 3 , 1 , 8 , 4 , 2 , 5
};
int arr2[] = {
1 , 4 , 2 , 3 , 7
};
// Get the size
int size = sizeof(arr1) / sizeof(arr1[0]);
cout << " Array elements \n";
// Get the size
size = sizeof(arr2) / sizeof(arr2[0]);
cout << "\n Array elements \n";
return 0;
}``````

#### Output

`````` Array elements
7  10  3  1  8  4  2  5
Mean : 5
Median : 4.5

Array elements
1  4  2  3  7
Mean : 3.4
Median : 3``````
``````// Include namespace system
using System;
/*
C# Program
Find Mean and median of an unsorted array
*/
public class MeanMedian
{
//Display elements of given array
public void printArray(int[] arr, int size)
{
for (int i = 0; i < size; ++i)
{
Console.Write("  " + arr[i]);
}
}
//Swap the array element
public void swap(int[] arr, int first, int second)
{
//first and second are index of array
int temp = arr[first];
arr[first] = arr[second];
arr[second] = temp;
}
// Returns the pivot element
public int pivotElement(int[] arr, int low, int high)
{
//Set the high index element to its proper sorted position
int pv = arr[high];
int i = low - 1;
for (int j = low; j < high; ++j)
{
if (arr[j] < pv)
{
i++;
swap(arr, i, j);
}
}
//set the high index value to its sorted position
swap(arr, i + 1, high);
//returns the next sorting  element location
return i + 1;
}
//
public void quickSort(int[] arr, int low, int high)
{
if (low < high)
{
//Get the pivot element
int pv = pivotElement(arr, low, high);
quickSort(arr, low, pv - 1);
quickSort(arr, pv + 1, high);
}
}
// Calculate mean and median of given array
public void meanAndMedian(int[] arr, int size)
{
if (size <= 0)
{
return;
}
double result = 0.0;
// Loop controlling variable
int i = 0;
// Execute loop through by size
// And calculate sum of array elements
for (i = 0; i < size; ++i)
{
result += arr[i];
}
// Calculate mean of given array
// (Sum of element / size of array)
result = result / size;
// Display calculated mean of array
Console.Write("\n Mean : " + result);
// Sort element
quickSort(arr, 0, size - 1);
if (size % 2 == 0)
{
// When have size of array are in Even length
i = size / 2;
// There is two middle element position
// Sum of two middle elements
result = arr[i] + arr[i - 1];
// Get Average
result /= 2;
Console.Write("\n Median : " + result + "\n");
}
else
{
i = size / 2;
// When length of array are Odd size
// Only one middle element possible
Console.Write("\n Median : " + arr[i] + "\n");
}
}
public static void Main(String[] args)
{
int[] arr1 = {
7 , 10 , 3 , 1 , 8 , 4 , 2 , 5
};
int[] arr2 = {
1 , 4 , 2 , 3 , 7
};
// Get the size
int size = arr1.Length;
Console.Write(" Array elements \n");
// Get the size
size = arr2.Length;
Console.Write("\n Array elements \n");
}
}``````

#### Output

`````` Array elements
7  10  3  1  8  4  2  5
Mean : 5
Median : 4.5

Array elements
1  4  2  3  7
Mean : 3.4
Median : 3``````
``````<?php
/*
Php Program
Find Mean and median of an unsorted array
*/
class MeanMedian
{
//Display elements of given array
public	function printArray( & \$arr, \$size)
{
for (\$i = 0; \$i < \$size; ++\$i)
{
echo "  ". \$arr[\$i];
}
}
//Swap the array element
public	function swap( & \$arr, \$first, \$second)
{
//first and second are index of array
\$temp = \$arr[\$first];
\$arr[\$first] = \$arr[\$second];
\$arr[\$second] = \$temp;
}
// Returns the pivot element
public	function pivotElement( & \$arr, \$low, \$high)
{
//Set the high index element to its proper sorted position
\$pv = \$arr[\$high];
\$i = \$low - 1;
for (\$j = \$low; \$j < \$high; ++\$j)
{
if (\$arr[\$j] < \$pv)
{
\$i++;
\$this->swap(\$arr, \$i, \$j);
}
}
//set the high index value to its sorted position
\$this->swap(\$arr, \$i + 1, \$high);
//returns the next sorting  element location
return \$i + 1;
}
//
public	function quickSort( & \$arr, \$low, \$high)
{
if (\$low < \$high)
{
//Get the pivot element
\$pv = \$this->pivotElement(\$arr, \$low, \$high);
\$this->quickSort(\$arr, \$low, \$pv - 1);
\$this->quickSort(\$arr, \$pv + 1, \$high);
}
}
// Calculate mean and median of given array
public	function meanAndMedian( & \$arr, \$size)
{
if (\$size <= 0)
{
return;
}
\$result = 0.0;
// Loop controlling variable
\$i = 0;
// Execute loop through by size
// And calculate sum of array elements
for (\$i = 0; \$i < \$size; ++\$i)
{
\$result += \$arr[\$i];
}
// Calculate mean of given array
// (Sum of element / size of array)
\$result = (\$result / \$size);
// Display calculated mean of array
echo "\n Mean : ". \$result;
// Sort element
\$this->quickSort(\$arr, 0, \$size - 1);
if (\$size % 2 == 0)
{
// When have size of array are in Even length
\$i = intval(\$size / 2);
// There is two middle element position
// Sum of two middle elements
\$result = \$arr[\$i] + \$arr[\$i - 1];
// Get Average
\$result = (\$result / 2);
echo "\n Median : ". \$result ."\n";
}
else
{
\$i = intval(\$size / 2);
// When length of array are Odd size
// Only one middle element possible
echo "\n Median : ". \$arr[\$i] ."\n";
}
}
}

function main()
{
\$arr1 = array(7, 10, 3, 1, 8, 4, 2, 5);
\$arr2 = array(1, 4, 2, 3, 7);
// Get the size
\$size = count(\$arr1);
echo " Array elements \n";
// Get the size
\$size = count(\$arr2);
echo "\n Array elements \n";
}
main();``````

#### Output

`````` Array elements
7  10  3  1  8  4  2  5
Mean : 5
Median : 4.5

Array elements
1  4  2  3  7
Mean : 3.4
Median : 3``````
``````/*
Node Js Program
Find Mean and median of an unsorted array
*/
class MeanMedian
{
//Display elements of given array
printArray(arr, size)
{
for (var i = 0; i < size; ++i)
{
process.stdout.write("  " + arr[i]);
}
}
//Swap the array element
swap(arr, first, second)
{
//first and second are index of array
var temp = arr[first];
arr[first] = arr[second];
arr[second] = temp;
}
// Returns the pivot element
pivotElement(arr, low, high)
{
//Set the high index element to its proper sorted position
var pv = arr[high];
var i = low - 1;
for (var j = low; j < high; ++j)
{
if (arr[j] < pv)
{
i++;
this.swap(arr, i, j);
}
}
//set the high index value to its sorted position
this.swap(arr, i + 1, high);
//returns the next sorting  element location
return i + 1;
}
//
quickSort(arr, low, high)
{
if (low < high)
{
//Get the pivot element
var pv = this.pivotElement(arr, low, high);
this.quickSort(arr, low, pv - 1);
this.quickSort(arr, pv + 1, high);
}
}
// Calculate mean and median of given array
meanAndMedian(arr, size)
{
if (size <= 0)
{
return;
}
var result = 0.0;
// Loop controlling variable
var i = 0;
// Execute loop through by size
// And calculate sum of array elements
for (i = 0; i < size; ++i)
{
result += arr[i];
}
// Calculate mean of given array
// (Sum of element / size of array)
result = (result / size);
// Display calculated mean of array
process.stdout.write("\n Mean : " + result);
// Sort element
this.quickSort(arr, 0, size - 1);
if (size % 2 == 0)
{
// When have size of array are in Even length
i = parseInt(size / 2);
// There is two middle element position
// Sum of two middle elements
result = arr[i] + arr[i - 1];
// Get Average
result = (result / 2);
process.stdout.write("\n Median : " + result + "\n");
}
else
{
i = parseInt(size / 2);
// When length of array are Odd size
// Only one middle element possible
process.stdout.write("\n Median : " + arr[i] + "\n");
}
}
}

function main()
{
var arr1 = [7, 10, 3, 1, 8, 4, 2, 5];
var arr2 = [1, 4, 2, 3, 7];
// Get the size
var size = arr1.length;
process.stdout.write(" Array elements \n");
// Get the size
size = arr2.length;
process.stdout.write("\n Array elements \n");
}
main();``````

#### Output

`````` Array elements
7  10  3  1  8  4  2  5
Mean : 5
Median : 4.5

Array elements
1  4  2  3  7
Mean : 3.4
Median : 3``````
``````#  Python 3 Program
#  Find Mean and median of an unsorted array

class MeanMedian :
# Display elements of given array
def printArray(self, arr, size) :
i = 0
while (i < size) :
print("  ", arr[i], end = "")
i += 1

# Swap the array element
def swap(self, arr, first, second) :
# first and second are index of array
temp = arr[first]
arr[first] = arr[second]
arr[second] = temp

#  Returns the pivot element
def pivotElement(self, arr, low, high) :
# Set the high index element to its proper sorted position
pv = arr[high]
i = low - 1
j = low
while (j < high) :
if (arr[j] < pv) :
i += 1
self.swap(arr, i, j)

j += 1

# set the high index value to its sorted position
self.swap(arr, i + 1, high)
# returns the next sorting  element location
return i + 1

#
def quickSort(self, arr, low, high) :
if (low < high) :
# Get the pivot element
pv = self.pivotElement(arr, low, high)
self.quickSort(arr, low, pv - 1)
self.quickSort(arr, pv + 1, high)

#  Calculate mean and median of given array
def meanAndMedian(self, arr, size) :
if (size <= 0) :
return

result = 0.0
#  Loop controlling variable
i = 0
#  Execute loop through by size
#  And calculate sum of array elements
while (i < size) :
result += arr[i]
i += 1

#  Calculate mean of given array
#  (Sum of element / size of array)
result = (result / size)
#  Display calculated mean of array
print("\n Mean : ", result, end = "")
#  Sort element
self.quickSort(arr, 0, size - 1)
if (size % 2 == 0) :
#  When have size of array are in Even length
i = int(size / 2)
#  There is two middle element position
#  Sum of two middle elements
result = arr[i] + arr[i - 1]
#  Get Average
result = (result / 2)
print("\n Median : ", result )
else :
i = int(size / 2)
#  When length of array are Odd size
#  Only one middle element possible
print("\n Median : ", arr[i] )

def main() :
arr1 = [7, 10, 3, 1, 8, 4, 2, 5]
arr2 = [1, 4, 2, 3, 7]
#  Get the size
size = len(arr1)
print(" Array elements ")
#  Get the size
size = len(arr2)
print("\n Array elements ")

if __name__ == "__main__": main()``````

#### Output

`````` Array elements
7   10   3   1   8   4   2   5
Mean :  5.0
Median :  4.5

Array elements
1   4   2   3   7
Mean :  3.4
Median :  3``````
``````#  Ruby Program
#  Find Mean and median of an unsorted array

class MeanMedian
# Display elements of given array
def printArray(arr, size)
i = 0
while (i < size)
print("  ", arr[i])
i += 1
end

end

# Swap the array element
def swap(arr, first, second)
# first and second are index of array
temp = arr[first]
arr[first] = arr[second]
arr[second] = temp
end

#  Returns the pivot element
def pivotElement(arr, low, high)
# Set the high index element to its proper sorted position
pv = arr[high]
i = low - 1
j = low
while (j < high)
if (arr[j] < pv)
i += 1
self.swap(arr, i, j)
end

j += 1
end

# set the high index value to its sorted position
self.swap(arr, i + 1, high)
# returns the next sorting  element location
return i + 1
end

#
def quickSort(arr, low, high)
if (low < high)
# Get the pivot element
pv = self.pivotElement(arr, low, high)
self.quickSort(arr, low, pv - 1)
self.quickSort(arr, pv + 1, high)
end

end

#  Calculate mean and median of given array
def meanAndMedian(arr, size)
if (size <= 0)
return
end

result = 0.0
#  Loop controlling variable
i = 0
#  Execute loop through by size
#  And calculate sum of array elements
while (i < size)
result += arr[i]
i += 1
end

#  Calculate mean of given array
#  (Sum of element / size of array)
result = result / size
#  Display calculated mean of array
print("\n Mean : ", result)
#  Sort element
self.quickSort(arr, 0, size - 1)
if (size % 2 == 0)
#  When have size of array are in Even length
i = size / 2
#  There is two middle element position
#  Sum of two middle elements
result = arr[i] + arr[i - 1]
#  Get Average
result = result / 2.0
print("\n Median : ", result ,"\n")
else
i = size / 2
#  When length of array are Odd size
#  Only one middle element possible
print("\n Median : ", arr[i] ,"\n")
end

end

end

def main()
arr1 = [7, 10, 3, 1, 8, 4, 2, 5]
arr2 = [1, 4, 2, 3, 7]
#  Get the size
size = arr1.length
print(" Array elements \n")
#  Get the size
size = arr2.length
print("\n Array elements \n")
end

main()``````

#### Output

`````` Array elements
7  10  3  1  8  4  2  5
Mean : 5.0
Median : 4.5

Array elements
1  4  2  3  7
Mean : 3.4
Median : 3
``````
``````/*
Scala Program
Find Mean and median of an unsorted array
*/
class MeanMedian
{
//Display elements of given array
def printArray(arr: Array[Int], size: Int): Unit = {
var i: Int = 0;
while (i < size)
{
print("  " + arr(i));
i += 1;
}
}
//Swap the array element
def swap(arr: Array[Int], first: Int, second: Int): Unit = {
//first and second are index of array
var temp: Int = arr(first);
arr(first) = arr(second);
arr(second) = temp;
}
// Returns the pivot element
def pivotElement(arr: Array[Int], low: Int, high: Int): Int = {
//Set the high index element to its proper sorted position
var pv: Int = arr(high);
var i: Int = low - 1;
var j: Int = low;
while (j < high)
{
if (arr(j) < pv)
{
i += 1;
this.swap(arr, i, j);
}
j += 1;
}
//set the high index value to its sorted position
this.swap(arr, i + 1, high);
//returns the next sorting  element location
return i + 1;
}
//
def quickSort(arr: Array[Int], low: Int, high: Int): Unit = {
if (low < high)
{
//Get the pivot element
var pv: Int = this.pivotElement(arr, low, high);
this.quickSort(arr, low, pv - 1);
this.quickSort(arr, pv + 1, high);
}
}
// Calculate mean and median of given array
def meanAndMedian(arr: Array[Int], size: Int): Unit = {
if (size <= 0)
{
return;
}
var result: Double = 0.0;
// Loop controlling variable
var i: Int = 0;
// Execute loop through by size
// And calculate sum of array elements
while (i < size)
{
result += arr(i);
i += 1;
}
// Calculate mean of given array
// (Sum of element / size of array)
result = (result / size);
// Display calculated mean of array
print("\n Mean : " + result);
// Sort element
this.quickSort(arr, 0, size - 1);
if (size % 2 == 0)
{
// When have size of array are in Even length
i = (size / 2).toInt;
// There is two middle element position
// Sum of two middle elements
result = arr(i) + arr(i - 1);
// Get Average
result = (result / 2.0);
print("\n Median : " + result + "\n");
}
else
{
i = (size / 2).toInt;
// When length of array are Odd size
// Only one middle element possible
print("\n Median : " + arr(i) + "\n");
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: MeanMedian = new MeanMedian();
var arr1: Array[Int] = Array(7, 10, 3, 1, 8, 4, 2, 5);
var arr2: Array[Int] = Array(1, 4, 2, 3, 7);
// Get the size
var size: Int = arr1.length;
print(" Array elements \n");
// Get the size
size = arr2.length;
print("\n Array elements \n");
}
}``````

#### Output

`````` Array elements
7  10  3  1  8  4  2  5
Mean : 5.0
Median : 4.5

Array elements
1  4  2  3  7
Mean : 3.4
Median : 3``````
``````/*
Swift 4 Program
Find Mean and median of an unsorted array
*/
class MeanMedian
{
//Display elements of given array
func printArray(_ arr: [Int], _ size: Int)
{
var i: Int = 0;
while (i < size)
{
print("  ", arr[i], terminator: "");
i += 1;
}
}
//Swap the array element
func swap(_ arr: inout[Int], _ first: Int, _ second: Int)
{
//first and second are index of array
let temp: Int = arr[first];
arr[first] = arr[second];
arr[second] = temp;
}
// Returns the pivot element
func pivotElement(_ arr: inout[Int], _ low: Int, _ high: Int)->Int
{
//Set the high index element to its proper sorted position
let pv: Int = arr[high];
var i: Int = low - 1;
var j: Int = low;
while (j < high)
{
if (arr[j] < pv)
{
i += 1;
self.swap(&arr, i, j);
}
j += 1;
}
//set the high index value to its sorted position
self.swap(&arr, i + 1, high);
//returns the next sorting  element location
return i + 1;
}
//
func quickSort(_ arr: inout[Int], _ low: Int, _ high: Int)
{
if (low < high)
{
//Get the pivot element
let pv: Int = self.pivotElement(&arr, low, high);
self.quickSort(&arr, low, pv - 1);
self.quickSort(&arr, pv + 1, high);
}
}
// Calculate mean and median of given array
func meanAndMedian(_ arr: inout[Int], _ size: Int)
{
if (size <= 0)
{
return;
}
var result: Double = 0.0;
// Loop controlling variable
var i: Int = 0;
// Execute loop through by size
// And calculate sum of array elements
while (i < size)
{
result += Double(arr[i]);
i += 1;
}
// Calculate mean of given array
// (Sum of element / size of array)
result = result / Double(size);
// Display calculated mean of array
print("\n Mean : ", result, terminator: "");
// Sort element
self.quickSort(&arr, 0, size - 1);
if (size % 2 == 0)
{
// When have size of array are in Even length
i = size / 2;
// There is two middle element position
// Sum of two middle elements
result = Double(arr[i] + arr[i - 1]);
// Get Average
result = result / 2.0;
print("\n Median : ", result );
}
else
{
i = size / 2;
// When length of array are Odd size
// Only one middle element possible
print("\n Median : ", arr[i] );
}
}
}
func main()
{
var arr1: [Int] = [7, 10, 3, 1, 8, 4, 2, 5];
var arr2: [Int] = [1, 4, 2, 3, 7];
// Get the size
var size: Int = arr1.count;
print(" Array elements ");
// Get the size
size = arr2.count;
print("\n Array elements ");
}
main();``````

#### Output

`````` Array elements
7   10   3   1   8   4   2   5
Mean :  5.0
Median :  4.5

Array elements
1   4   2   3   7
Mean :  3.4
Median :  3``````
``````/*
Kotlin Program
Find Mean and median of an unsorted array
*/
class MeanMedian
{
//Display elements of given array
fun printArray(arr: Array <Int> , size: Int): Unit
{
var i: Int = 0;
while (i < size)
{
print("  " + arr[i]);
i += 1;
}
}
//Swap the array element
fun swap(arr: Array <Int> , first: Int, second: Int): Unit
{
//first and second are index of array
var temp: Int = arr[first];
arr[first] = arr[second];
arr[second] = temp;
}
// Returns the pivot element
fun pivotElement(arr: Array <Int> , low: Int, high: Int): Int
{
//Set the high index element to its proper sorted position
var pv: Int = arr[high];
var i: Int = low - 1;
var j: Int = low;
while (j < high)
{
if (arr[j] < pv)
{
i += 1;
this.swap(arr, i, j);
}
j += 1;
}
//set the high index value to its sorted position
this.swap(arr, i + 1, high);

//returns the next sorting  element location
return i + 1;
}
//
fun quickSort(arr: Array <Int> , low: Int, high: Int): Unit
{
if (low < high)
{
//Get the pivot element
var pv: Int = this.pivotElement(arr, low, high);
this.quickSort(arr, low, pv - 1);
this.quickSort(arr, pv + 1, high);
}
}
// Calculate mean and median of given array
fun meanAndMedian(arr: Array <Int> , size: Int): Unit
{
if (size <= 0)
{
return;
}
var result: Double = 0.0;
// Loop controlling variable
var i: Int = 0;
// Execute loop through by size
// And calculate sum of array elements
while (i < size)
{
result += arr[i];
i += 1;
}
// Calculate mean of given array
// (Sum of element / size of array)
result = result / size;
// Display calculated mean of array
print("\n Mean : " + result);
// Sort element
this.quickSort(arr, 0, size - 1);
if (size % 2 == 0)
{
// When have size of array are in Even length
i = size / 2;
// There is two middle element position
// Sum of two middle elements
result = (arr[i] + arr[i - 1]).toDouble();
// Get Average
result = result / 2.0;
print("\n Median : " + result + "\n");
}
else
{
i = size / 2;
// When length of array are Odd size
// Only one middle element possible
print("\n Median : " + arr[i] + "\n");
}
}
}

fun main(args: Array < String > ): Unit
{
var arr1: Array <Int> = arrayOf(7, 10, 3, 1, 8, 4, 2, 5);
var arr2: Array <Int> = arrayOf(1, 4, 2, 3, 7);
// Get the size
var size: Int = arr1.count();
print(" Array elements \n");
// Get the size
size = arr2.count();
print("\n Array elements \n");
}``````

#### Output

`````` Array elements
7  10  3  1  8  4  2  5
Mean : 5.0
Median : 4.5

Array elements
1  4  2  3  7
Mean : 3.4
Median : 3``````

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.