-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathBitonic.java
125 lines (102 loc) · 2.82 KB
/
Bitonic.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package com.sharmaumang;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Bitonic {
void compAndSwap(int a[], int i, int j, int dir)
{
if ( (a[i] > a[j] && dir == 1) ||
(a[i] < a[j] && dir == 0))
{
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
void bitonicMerge(int a[], int low, int cnt, int dir)
{
if (cnt>1)
{
int k = cnt/2;
for (int i=low; i<low+k; i++)
compAndSwap(a,i, i+k, dir);
bitonicMerge(a,low, k, dir);
bitonicMerge(a,low+k, k, dir);
}
}
void bitonicSort(int a[], int low, int cnt, int dir)
{
if (cnt>1)
{
int k = cnt/2;
bitonicSort(a, low, k, 1);
bitonicSort(a,low+k, k, 0);
bitonicMerge(a, low, cnt, dir);
}
}
void sort(int a[], int N, int up)
{
bitonicSort(a, 0, N, up);
}
/* A utility function to print array of size n */
static void printArray(int arr[])
{
int n = arr.length;
for (int i=0; i<n; ++i)
System.out.print(arr[i] + " ");
System.out.println();
}
// Driver method
public static void main(String args[])
{
FastReader sc = new FastReader();
int t = sc.nextInt();
int[] a = new int[t];
for (int i = 0; i < t; i++) {
a[i]=sc.nextInt();
}
int up = 1;
Bitonic ob = new Bitonic();
ob.sort(a, a.length,up);
System.out.println("\nSorted array");
printArray(a);
}
//Fast Reader Class
static class FastReader {
BufferedReader reader;
StringTokenizer mStringTokenizer;
public FastReader() {
reader = new BufferedReader(new
InputStreamReader(System.in));
}
String next() {
while (mStringTokenizer == null || !mStringTokenizer.hasMoreElements()) {
try {
mStringTokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return mStringTokenizer.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = reader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}