
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Calculate Median of an Array in JavaScript
In this problem statement, our task is to calculate the median of an array with the help of Javascript functionalities. There are several ways that can be used to solve this task. One simple method to calculate median is using the built-in function of Javascript.
Understanding the problem statement
The problem statement is to write a function in Javascript that will help to calculate the median of a given array. For example, if we have an array of integers [1, 2, 3, 4, 5] so the median of this array is 3. Because 3 is the middle element of the array.
Logic for the given problem
For the code we will create a function to find the median of the array. And inside the function we will calculate the middle index of the array by dividing the length of the array by 2. This index will be the index of the median in the array if the array has an odd number of elements. If the array has an even number of arrays, the index of the median will be the left of the median.
Then we will sort the array in ascending order using the sort() method, which will take a comparison function as a parameter. If the length of the array is even then we will return the average of the two middle values. We will do this by adding the values at the middle index and index to the left of the middle and then dividing the sum by 2. And if the length is odd in the array so we will simply return the middle value.
Algorithm
Step 1 ? Declare a function called median which is using a parameter of array.
Step 2 ? Calculate the mid of the array by dividing the length of the array by 2. It will show the index of the mid element.
Step 3 ? Sort the given array with the help of the sort method.
Step 4 ? And if the array length is even then return the left side mid value.
Step 5 ? Return the result as the mid element of the array.
Code for the algorithm
//function to find the median of the given array function median(arr) { const mid = Math.floor(arr.length / 2); const sortedArr = arr.sort((a, b) => a - b); if (arr.length % 2 === 0) { return (sortedArr[mid - 1] + sortedArr[mid]) / 2; } else { return sortedArr[mid]; } } const arr = [11, 12, 13, 14, 15, 16, 17, 18, 19]; console.log(median(arr));
Complexity
The time taken by the function is O(n log n ), because we have used a sort method which performs quick sort and quicksort takes O(n log n) time to sort the elements. Where n is the size of the given array. And the space used by the code is O(1) as it is storing the result as only the middle element of the array.
Conclusion
The implemented code is providing a simple and efficient solution to find out the median of the given array. The time taken by the created function is O(n log n). There can be a case when the given array is already sorted so in that case the time complexity will be O(n) because it is taking linear time.