5. Assignment 13
WAP to find the median of two unsorted arrays.
Input: Read two integer array of size ‘M’ and ‘N’, and read
elements for array1 of size ‘M’ and array2 of size ‘N’
6. Assignment 13
WAP to find the median of two unsorted arrays.
Input: Read two integer array of size ‘M’ and ‘N’, and read
elements for array1 of size ‘M’ and array2 of size ‘N’
Output: Print the median of array1 and array2 , and print
the average of both median
13. Assignment 13
What is median?
What is the median, if the array is unsorted?
4 7 3 2 9
Ex: Unsorted array
14. Assignment 13
What is median?
What is the median, if the array is unsorted?
Sort the array elements in ascending order first and then find
the median.
4 7 3 2 9
Ex: Unsorted array
15. Assignment 13
What is median?
What is the median, if the array is unsorted?
Sort the array elements in ascending order first and then find
the median.
4 7 3 2 9
Ex:
2 3 4 7 9 Sorted array
Unsorted array
16. Assignment 13
What is median?
What is the median, if the array is unsorted?
Sort the array elements in ascending order first and then find
the median.
What is the median(middle element)?
4 7 3 2 9
Ex:
2 3 4 7 9 Sorted array
Unsorted array
17. Assignment 13
What is median?
What is the median, if the array is unsorted?
Sort the array elements in ascending order first and then find
the median.
What is the median(middle element)?
Median = 4.
4 7 3 2 9
Ex:
2 3 4 7 9 Sorted array
Unsorted array
18. Assignment 13
What is median?
It is a middle element of the sorted list.
1 2 3 4 5 6
Ex:
19. Assignment 13
What is median?
It is a middle element of the sorted list.
There are two middle elements (i.e) -> 3 and 4
1 2 3 4 5 6
Ex:
20. Assignment 13
What is median?
It is a middle element of the sorted list.
There are two middle elements (i.e) -> 3 and 4
Which element will be the median?
1 2 3 4 5 6
Ex:
21. Assignment 13
What is median?
It is a middle element of the sorted list.
There are two middle elements (i.e) -> 3 and 4
Which element will be the median?
Median = average of 3 & 4
1 2 3 4 5 6
Ex:
22. Assignment 13
What is median?
It is a middle element of the sorted list.
There are two middle elements (i.e) -> 3 and 4
Which element will be the median?
Median = average of 3 & 4
= (3 + 4) / 2
= 3.5
1 2 3 4 5 6
Ex:
44. Assignment 13
Example :-
Step 4 : Sort the array1
After sorting , array1 elements are
Step 5 : Sort the array2
1 2 4 5 6 8 9
45. Assignment 13
Example :-
Step 4 : Sort the array1
After sorting , array1 elements are
Step 5 : Sort the array2
After sorting , array2 elements are
1 2 4 5 6 8 9
46. Assignment 13
Example :-
Step 4 : Sort the array1
After sorting , array1 elements are
Step 5 : Sort the array2
After sorting , array2 elements are
1 2 3 4 5 6 7
1 2 4 5 6 8 9
50. Assignment 13
Example :-
Step 6 : Print the median of array1
Median of array1(Median1) = 5
Step 7 : Print the median of array2
1 2 4 5 6 8 9
51. Assignment 13
Example :-
Step 6 : Print the median of array1
Median of array1(Median1) = 5
Step 7 : Print the median of array2
1 2 4 5 6 8 9
1 2 3 4 5 6 7
52. Assignment 13
Example :-
Step 6 : Print the median of array1
Median of array1(Median1) = 5
Step 7 : Print the median of array2
Median of array2(Median1) = 4
1 2 4 5 6 8 9
1 2 3 4 5 6 7
54. Assignment 13
Example :-
Step 8 : Print the average of Median1 and Median2 (i.e., median
for both the arrays)
Final median = (5 + 4) / 2 = 4.5
55. Assignment 13
Example :-
Step 8 : Print the average of Median1 and Median2 (i.e., median
for both the arrays)
Final median = (5 + 4) / 2 = 4.5
What is the output, if you print
median using %f ?
What is the output, if you print
median using %g ?
56. Assignment 13
Example :-
Step 8 : Print the average of Median1 and Median2 (i.e., median
for both the arrays)
Final median = (5 + 4) / 2 = 4.5
What is the output, if you print
median using %f ?
Output: 4.500000
What is the output, if you print
median using %g ?
Output: 4.5
%g is used to print the decimal floating-point values, and it uses the
fixed precision.
66. Assignment 13
Example :-
Step 4 : Sort the array1
After sorting , array1 elements are
Step 5 : Sort the array2
1 2 5 6
67. Assignment 13
Example :-
Step 4 : Sort the array1
After sorting , array1 elements are
Step 5 : Sort the array2
After sorting , array2 elements are
1 2 5 6
68. Assignment 13
Example :-
Step 4 : Sort the array1
After sorting , array1 elements are
Step 5 : Sort the array2
After sorting , array2 elements are
1 2 5 6
1 4 5 6 7
72. Assignment 13
Example :-
Step 6 : Print the median of array1
Median of array1(Median1) = (2 + 5) / 2 = 3.5
Step 7 : Print the median of array2
1 2 5 6
73. Assignment 13
Example :-
Step 6 : Print the median of array1
Median of array1(Median1) = (2 + 5) / 2 = 3.5
Step 7 : Print the median of array2
1 2 5 6
1 4 5 6 7
74. Assignment 13
Example :-
Step 6 : Print the median of array1
Median of array1(Median1) = (2 + 5) / 2 = 3.5
Step 7 : Print the median of array2
Median of array2(Median1) = 5
1 2 5 6
1 4 5 6 7