Swift Program to Find Median of an Unsorted Array


This tutorial will discuss how to write swift program to find median of an unsorted array.

An array is an ordered collection which is used to store same type of data. For example, if any array is of integer type then it will only store integers, you are strictly not allowed to store elements of other data types like string, float, etc.

Syntax

Following is the syntax for an array −

var array1 = [val1, val2, val3, …]
Or
var array2 = [Type]()

Median represent the middle value of the given sorted set of numbers. We can calculate the middle value by ordering the numbers in ascending order. Once the number are arranged in ascending order, then the middle number is known as median of the given array.

If the array elements are odd number, then the middle element is the median. For example −

2, 5, 8, 90, 34
Median - 8

If the array elements are even number, then the average of two middle elements is the median. For example −

2, 5, 8, 90
Median = (5+8)/2 = 6.5

Below is a demonstration of the same −

Input 1

Suppose our given input is −

MyArray = [10, 23, 45, 67, 98]

Output

The desired output would be −

Median = 45
Input 2

Input 2

Suppose our given input is −

MyArray = [10, 23, 45, 67]

Output

The desired output would be −

Median = 34

Algorithm

Following is the algorithm −

  • Step 1 − Create an array with values.

  • Step 2 − Sort the array using sort() function.

  • Step 3 − Find the size using count property.

  • Step 4 − To find the median, we check the size of the array is odd or even by finding the remainder. If the remainder is 0, then the size of the array is even. Otherwise odd.

if (size % 2 != 0)
  • Step 5 − If the size of the array is odd, then the mid element is the median.

  • Step 6 − If the size of the array is even, then the median is the average of two middle elements.

  • Step 7 − Print the output

Example 1

The following program shows how to calculate the median of an unsorted array.

import Foundation import Glibc // Creating an array of integer type var arrNums = [2, 45, 12, 49, 50] print("Original Array:", arrNums) // Sort the array arrNums.sort() print("Sorted Array:", arrNums) var size = arrNums.count // Finding median // Check for the odd or even case if (size % 2 != 0){ print("Median:", arrNums[size/2]) } else{ print("Median:", (arrNums[(size - 1) / 2] + arrNums[size / 2])/2) }

Output

Original Array: [2, 45, 12, 49, 50]
Sorted Array: [2, 12, 45, 49, 50]
Median: 45

Here, in the above code, we have an array of integer type named arrNums. Now we find the median. So to do that first we sort the array. Then we check for the odd or even case. So if the elements are odd, then the median is the mid value of the array. Otherwise, the median is the average of two middle elements −

if (size % 2 != 0){
   print("Median:", arrNums[size/2])
}
else{
   print("Median:", (arrNums[(size - 1) / 2] + arrNums[size / 2])/2)
}

So the working of the above code −

Sorted array: [2, 12, 45, 49, 50]

Size of the array: 5

if (size % 2 != 0)= (5%2 != 0) // Condition is true means total number of elements are odd{
   // So the mid element is the median
   print("Median:", arrNums[size/2]) = print(arrNums[size/2]) = print(arrNums[5/2]) = print(arrNums[2]) = print(45)
}

Hence the median is 45.

Example 2

The following program shows how to calculate the median of an unsorted array.

import Foundation import Glibc // Function to find the median of the given array func arrayMedian(arr: [Int]){ var arrNums = arr // Sort the array arrNums.sort() print("Sorted Array:", arrNums) let size = arrNums.count // Finding median // Check for the odd or even case if (size % 2 != 0){ print("Median:", arrNums[size/2]) } else{ print("Median:", (arrNums[(size - 1) / 2] + arrNums[size / 2])/2) } } // Creating an array of integer type var arrVals = [40, 20, 60, 80, 30, 70] print("Original Array:", arrVals) // Calling the Function arrayMedian(arr: arrVals)

Output

Original Array: [40, 20, 60, 80, 30, 70]
Sorted Array: [20, 30, 40, 60, 70, 80]
Median: 50

Here, in the above code, we have an array named arrVals of integer type. Now to find the median of arrVals, so we create a function named arrayMedian(). This function will return the median. In this function, we first we sort the array. Then we check for the odd or even case. So if the elements are odd, then the median is the mid value of the array. Otherwise, the median is the average of two middle elements. So the working of the above code is −

Sorted Array: [20, 30, 40, 60, 70, 80]
Size: 6
if (size % 2 != 0)= (6%2 != 0) // Condition is false means total number of elements are even{
   // So the average of two numbers is the median
   print((arrNums[(size - 1) / 2] + arrNums[size / 2])/2) = print((arrNums[(6 - 1) / 2] + arrNums[6 / 2])/2) = print((arrNums[5/ 2] + arrNums[6 / 2])/2) = print((arrNums[2] + arrNums[3])/2) = print((40 + 60)/2) = print(100/2) = print(50)
}

Hence the median is 50.

Updated on: 20-Oct-2022

725 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements