Write a C++ Program that sorts an array through bubble sort by swapping values and then finding
Fantastic news! We've Found the answer you've been seeking!
Question:
Write a C++ Program that sorts an array through bubble sort by swapping values and then finding median of that sorted array. Most importantly write your code in the following parts.
- Make a function Swap( ) that takes address of the two integers variables as arguments (pointers) and swap those two variables.
- Make a function BubbleSort( ) which sorts an integer type array into ascending order. The function BubbleSort( ) takes the array and its size as argument.
It uses the above function swap while sorting to swap two array elements by passing those array elements to swap function.
- Make a function Median() that takes an array and its size as arguments. Then it calls the above function BubbleSort() to sort the array. After sorting it will find the mean of that sorted array through following formual.
- Write main program. Take the values of array in it from user. Call only the function named median( ) from main and pass it the array and its size.
3 | 1 | 4 | 1 | 5 | 9 | 2 | 6 | 5 |
Formula for Median of an array in C++
When size of array is even: Median = arr[size/2];
When size of array is odd: Median = ( arr[ size/2 ]+arr[ size/(2+1)] ) / 2.0;
Remember arrays index start from 0.
Related Book For
Introduction to Algorithms
ISBN: 978-0262033848
3rd edition
Authors: Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest
Posted Date: