0
Read in an integer n (which can be at most 50). Then read in n integers one by one
and store them in an array data from index 0 to n-1. Now we want to rearrange the
integers in data in the following way :
Find the maximum value in data and put in the centre of the array (that is at (n/2);
find the next largest value and put it to its right; then the next largest and place it to
its left and so on alternating right and left until all integers in data are done. For
example, if the array is initially:
7, 3, 1, 6, 4, 2, 5 then after rearranging it becomes 1, 3, 5, 7, 6, 4, 2
However, since we have very little memory, you are not allowed to use any other
array from data.
Sample data:
Input:
Give the number of integers: 5
Give integer 1 : 7
Give integer 2 : 9
Give integer 3 : 2
Give integer 4 : 5
Give integer 5 : 6
Output:
Original array
7 9 2 5 6
rearranged array
2 6 9 7 5
Input:
Give the number of integers: 6
Give integer 1 : 5
Give integer 2 : 1
Give integer 3 : 9
Give integer 4 : 8
Give integer 5 : 2
Give integer 6 : 4
]
Output:
Original array
5 1 9 8 2 4
rearranged array
2 5 9 8 4 1
and store them in an array data from index 0 to n-1. Now we want to rearrange the
integers in data in the following way :
Find the maximum value in data and put in the centre of the array (that is at (n/2);
find the next largest value and put it to its right; then the next largest and place it to
its left and so on alternating right and left until all integers in data are done. For
example, if the array is initially:
7, 3, 1, 6, 4, 2, 5 then after rearranging it becomes 1, 3, 5, 7, 6, 4, 2
However, since we have very little memory, you are not allowed to use any other
array from data.
Sample data:
Input:
Give the number of integers: 5
Give integer 1 : 7
Give integer 2 : 9
Give integer 3 : 2
Give integer 4 : 5
Give integer 5 : 6
Output:
Original array
7 9 2 5 6
rearranged array
2 6 9 7 5
Input:
Give the number of integers: 6
Give integer 1 : 5
Give integer 2 : 1
Give integer 3 : 9
Give integer 4 : 8
Give integer 5 : 2
Give integer 6 : 4
]
Output:
Original array
5 1 9 8 2 4
rearranged array
2 5 9 8 4 1
zaid
Feb 15, '14
Log in to your account to answer this questions.