-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArrayRearrangement.java
80 lines (67 loc) · 2.08 KB
/
ArrayRearrangement.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import java.util.Arrays;
import java.util.stream.Collectors;
// given array rearrange it in
// smallest, largest, 2nd smallest, 2nd largest, 3rd smallest, 3rd largest,...
//
// input 6, 3, 2, 88, 90, 66, 89, 23, 1
// output 1, 90, 2, 89, 3, 88, 6, 66, 23
public class ArrayRearrangement {
public static void main(String args[]) {
int arr[] = {6, 3, 2, 88, 90, 66, 89, 23, 1, 13};
System.out.println(Arrays.stream(arr).mapToObj(String::valueOf).collect(Collectors.joining(" ")));
rearrange(arr);
System.out.println(Arrays.stream(arr).mapToObj(String::valueOf).collect(Collectors.joining(" ")));
}
private static void rearrange(int arr[]) {
sort(arr, 0, arr.length - 1);
int temp[] = new int[arr.length];
int i = 0;
while (i < arr.length / 2) {
temp[2 * i] = arr[i];
temp[2 * i + 1] = arr[arr.length - i - 1];
i++;
}
if (arr.length % 2 != 0) temp[2 * i] = arr[i];
for (int j = 0; j < arr.length; j++)
arr[j] = temp[j];
}
private static void sort(int arr[], int left, int right) {
if (left > right) return;
int pivot = partition(arr, left, right);
sort(arr, left, pivot - 1);
sort(arr, pivot + 1, right);
}
private static int partition(int arr[], int left, int right) {
int pivotIndex = findMedian(arr, left, right);
swap(arr, left, pivotIndex);
int pivot = arr[left];
int start = left + 1, end = right;
while (start <= end) {
while (start <= end && arr[start] < pivot) start++;
while (start <= end && arr[end] > pivot) end--;
if (start <= end) {
swap(arr, start, end);
start++; end--;
}
}
swap(arr, left, end);
return end;
}
private static int findMedian(int arr[], int left, int right) {
int mid = (left + right) / 2;
if (arr[mid] < arr[left] && arr[mid] < arr[right]) {
return mid;
} else if (arr[left] < arr[mid] && arr[mid] < arr[right]) {
return left;
} else if (arr[right] < arr[mid] && arr[mid] < arr[left]) {
return right;
} else {
return mid;
}
}
private static void swap(int arr[], int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}