New Question!!
Write a program in Java to perform web sort on a inputted array.
Restrictions:-
1. Not to use nested loop. You can use maximum of 3 loops. (1 for taking input, 1 for web sort, 1 for displaying the result)
2. You can use only 1 if statement.
Don't copy answers from any website.
Moderators and stars can answer in my question.
Answers
Answer:
Given a sorted array of positive integers, rearrange the array alternately i.e first element should be maximum value, second minimum value, third second max, fourth second min and so on.
Examples:
Input: arr[] = {1, 2, 3, 4, 5, 6, 7}
Output: arr[] = {7, 1, 6, 2, 5, 3, 4}
Input: arr[] = {1, 2, 3, 4, 5, 6}
Output: arr[] = {6, 1, 5, 2, 4, 3}
Expected time complexity: O(n).
Recommended: Please solve it on “PRACTICE ” first, before moving on to the solution.
The idea is to use an auxiliary array. We maintain two pointers one to leftmost or smallest element and other to rightmost or largest element. We more both pointers toward each other and alternatively copy elements at these pointers to an auxiliary array. Finally, we copy auxiliary array back to the original array.
Below image is a dry run of the above approach:
plz tell me now.. where is the rude word