-
Notifications
You must be signed in to change notification settings - Fork 29
/
BitonicSort.java
77 lines (70 loc) · 1.85 KB
/
BitonicSort.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
// Bitonic Sort
//this program works when size of input is power of 2.
public class BitonicSort
{
static void exchange(int arr[], int i, int j, boolean d)
{
int temp;
if (d==(arr[i]>arr[j]))
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
/* The sequence to be sorted starts at
index position low (l), the parameter c is the number
of elements to be sorted.*/
static void bitonicMerge(int arr[], int l, int c, boolean d)
{
int k,i;
if (c>1)
{
k = c/2;
for (i=l; i<l+k; i++)
exchange(arr, i, i+k, d);
bitonicMerge(arr, l, k, d);
bitonicMerge(arr, l+k, k, d);
}
}
static void bitonicSort(int arr[],int l, int c, boolean d)
{
int k;
if (c>1)
{
k = c/2;
// sort in ascending order since dir here is 1
bitonicSort(arr, l, k, true);
// sort in descending order since dir here is 0
bitonicSort(arr, l+k, k, false);
// Will merge whole sequence in ascending order since dir=1.
bitonicMerge(arr,l, c, d);
}
}
/*Caller of bitonicSort for sorting the entire array
of length N in ASCENDING order */
static void sort(int arr[], int n, boolean order)
{
bitonicSort(arr,0, n, order);
}
/* 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 Code
public static void main(String[] args)
{
int arr[]= {1, 10, 2, 3, 1, 23, 45, 21};
int n = arr.length;
int i;
boolean order = true;
sort(arr, n, order);
System.out.println("Sorted array: \n");
for (i=0; i<n; i++)
System.out.println(arr[i]);
}
}