https://github.com/mission-peace/interview/blob/master/src/com/interview/array/ConvertAnArrayIntoDecreaseIncreaseFashion.java
* Convert an unsorted array into an array such that
* a < b > c < d > e < f and so on
*/
public void convert(int arr[]){
int k = 0;
if(arr.length % 2 ==0){
k = arr.length/2 ;
}else{
k = arr.length/2+1;
}
KthElementInArray kthElement = new KthElementInArray();
kthElement.kthElement(arr, k);
int high = k;
int low = 1;
while(low < high && high < arr.length){
swap(arr,low,high);
high++;
low += 2;
}
}
/**
* Sort the array first.
* Then swap every adjacent element to get final result
* @param arr
*/
public void convert1(int arr[]){
Arrays.sort(arr);
for(int i=1; i < arr.length; i+=2){
if(i+1 < arr.length){
swap(arr, i, i+1);
}
}
}
* Convert an unsorted array into an array such that
* a < b > c < d > e < f and so on
*/
public void convert(int arr[]){
int k = 0;
if(arr.length % 2 ==0){
k = arr.length/2 ;
}else{
k = arr.length/2+1;
}
KthElementInArray kthElement = new KthElementInArray();
kthElement.kthElement(arr, k);
int high = k;
int low = 1;
while(low < high && high < arr.length){
swap(arr,low,high);
high++;
low += 2;
}
}
/**
* Sort the array first.
* Then swap every adjacent element to get final result
* @param arr
*/
public void convert1(int arr[]){
Arrays.sort(arr);
for(int i=1; i < arr.length; i+=2){
if(i+1 < arr.length){
swap(arr, i, i+1);
}
}
}