Input n integers in an array of size n.
Rearrange these integers in the following manner:
- Put the maximum value in the centre of the array
- Next largest value to its right
- Next largest value to its left, and so on.
Example:
Input- 1, 3, 5, 9, 4, 7; Output- 1, 3, 5, 9, 7, 4, 2
I wrote this code:
class sort
{
public static void main(int n)throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int arr[] = new int[n];
for(int i=0; i<n; i++)
{
arr[i] = Integer.parseInt(br.readLine());
}
int g = arr[0];
int it = 0;
int loc = 1;
sort: for(int i=0; i<n; i++)
{
if(arr[i] > g && i != loc) g = arr[i];
}
if(n%2 != 0)
{
it++;
int c = (n+1) / 2;
int arr[c] = g;
if(it%2 != 0) n += 2;
if(it%2 == 0) n -= 3;
loc = i;
}
else if(n%2 == 0)
{
System.out.println("Array length is even");
System.exit(1);
}
if(it <= arr.length) continue sort;
for(int i=0; i<arr.length; i++) System.out.println(a[i] + " ");
}
}
When I compiled it, it said: ']' expected at line number 22. Please temme how to resolve this error. And would this code sort the array in the desired manner?
This post has been edited by adhish94: 14 July 2010 - 04:02 AM

New Topic/Question
Reply




MultiQuote





|