Alternative Sorting
Alternative Sorting
Alternative Sorting
Given an array of integers, print the array in such a way that the first element is first
maximum and second element is first minimum and so on.
Examples :
Input : arr[] = {7, 1, 2, 3, 4, 5, 6}
Output : 7 1 6 2 5 3 4
A simple solution is to first print maximum element, then minimum, then second
maximum, and so on. Time complexity of this approach is O(n2).
An efficient solution involves following steps.
1) Sort input array using a O(n Log n) algorithm.
2) We maintain two pointers, one from beginning and one from end in sorted array.
We alternatively print elements pointed by two pointers and move them toward
each other.
// Java program to print an array in alternate
// sorted manner
import java.io.*;
import java.util.Arrays;
class AlternativeString
{
// Function to print alternate sorted values
static void alternateSort(int arr[], int n)
{
Arrays.sort(arr);
// Printing the last element of array
// first and then first element and then
// second last element and then second
// element and so on.
int i = 0, j = n-1;
while (i < j) {
System.out.print(arr[j--] + " ");
System.out.print(arr[i++] + " ");
}
// If the total element in array is odd
// then print the last middle element.
if (n % 2 != 0)
System.out.print(arr[i]);
}
/* Driver program to test above functions */
public static void main (String[] args)
{
int arr[] = {1, 12, 4, 6, 7, 10};
int n = arr.length;
alternateSort(arr, n);
}
}
Output :
12 1 10 4 7 6
Time Complexity: O(n Log n)
Auxiliary Space : O(1)
The idea is to use a self balancing binary search tree. We traverse input array and for
every element, we find its difference with x and store the difference as key and
element as value in self balancing binary search tree. Finally we traverse the tree and
print its inorder traversal which is required output.
Algorithm:
Let first array be mPlusN[] and other array be N[]
1) Move m elements of mPlusN[] to end.
2) Start from nth element of mPlusN[] and 0th
element of N[] and merge them into mPlusN[].
Below is the implementation of the above algorithm :
class MergeArrays
{
/* Function to move m elements at the end of array mPlusN[] */
void moveToEnd(int mPlusN[], int size)
{
int i, j = size - 1;
for (i = size - 1; i >= 0; i--)
{
if (mPlusN[i] != -1)
{
mPlusN[j] = mPlusN[i];
j--;
}
}
}
/* Merges array N[] of size n into array mPlusN[]
of size m+n*/
void merge(int mPlusN[], int N[], int m, int n)
{
int i = n;
/* Current index of i/p part of mPlusN[]*/
int j = 0;
/* Current index of N[]*/
int k = 0;
/* Current index of output mPlusN[]*/
while (k < (m + n))
{
/* Take an element from mPlusN[] if
a) value of the picked element is smaller and we have
not reached end of it
b) We have reached end of N[] */
if ((i < (m + n) && mPlusN[i] <= N[j]) || (j == n))
{
mPlusN[k] = mPlusN[i];
k++;
i++;
}
else // Otherwise take element from N[]
{
mPlusN[k] = N[j];
k++;
j++;
}
}
}
/* Utility that prints out an array on a line */
void printArray(int arr[], int size)
{
int i;
for (i = 0; i < size; i++)
System.out.print(arr[i] + " ");
System.out.println("");
}
public static void main(String[] args)
{
MergeArrays mergearray = new MergeArrays();
/* Initialize arrays */
int mPlusN[] = {2, 8, -1, -1, -1, 13, -1, 15, 20};
int N[] = {5, 7, 9, 25};
int n = N.length;
int m = mPlusN.length - n;
/*Move the m elements at the end of mPlusN*/
mergearray.moveToEnd(mPlusN, m + n);
/*Merge N[] into mPlusN[] */
mergearray.merge(mPlusN, N, m, n);
/* Print the resultant mPlusN */
mergearray.printArray(mPlusN, m + n);
}
}
Output:
2 5 7 8 9 13 15 20 25
Time Complexity: O(m+n)