Skip to content

Commit 8917e41

Browse files
committed
Added bitonic sort
fixes TheAlgorithms#96
1 parent 598783d commit 8917e41

File tree

1 file changed

+86
-0
lines changed

1 file changed

+86
-0
lines changed

Sorts/BitonicSort.java

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
2+
public class BitonicSort {
3+
/**
4+
* The parameter sortingDirection indicates the sorting direction, ASCENDING or
5+
* DESCENDING; if (a[i] > a[j]) agrees with the direction, then a[i] and
6+
* a[j] are interchanged.
7+
*/
8+
void compareAndSwap(int arr[], int i, int j, int sortingDirection) {
9+
if ((arr[i] > arr[j] && sortingDirection == 1) || (arr[i] < arr[j] && sortingDirection == 0)) {
10+
// Swapping elements
11+
int temp = arr[i];
12+
arr[i] = arr[j];
13+
arr[j] = temp;
14+
}
15+
}
16+
17+
/**
18+
* This function is being called to merge two sorted arrays in same order.
19+
*
20+
* @param arr - array to be sorted
21+
* @param low - starting index position of the sequence to be sorted
22+
* @param noOfEleToBeSorted - Number of elements to be sorted
23+
* @param sortingDirection - 0 -> ascending direction, 1 -> descending direction
24+
*/
25+
void bitonicMerge(int arr[], int low, int noOfEleToBeSorted, int sortingDirection) {
26+
if (noOfEleToBeSorted > 1) {
27+
int k = noOfEleToBeSorted / 2;
28+
for (int i = low; i < low + k; i++)
29+
compareAndSwap(arr, i, i + k, sortingDirection);
30+
bitonicMerge(arr, low, k, sortingDirection);
31+
bitonicMerge(arr, low + k, k, sortingDirection);
32+
}
33+
}
34+
35+
/**
36+
* This function first produces a bitonic sequence by recursively sorting its
37+
* two halves in opposite sorting orders, and then calls bitonicMerge to
38+
* make them in the same order
39+
*
40+
* @param arr - array to be sorted
41+
* @param low - starting index position of the sequence to be sorted
42+
* @param noOfEleToBeSorted - Number of elements to be sorted
43+
* @param sortingDirection - 0 -> ascending direction, 1 -> descending direction
44+
*/
45+
void bitonicSort(int arr[], int low, int noOfEleToBeSorted, int sortingDirection) {
46+
if (noOfEleToBeSorted > 1) {
47+
int k = noOfEleToBeSorted / 2;
48+
49+
// sort in ascending order since sortingDirection here is 1
50+
bitonicSort(arr, low, k, 1);
51+
52+
// sort in descending order since sortingDirection here is 0
53+
bitonicSort(arr, low + k, k, 0);
54+
55+
// Will merge whole sequence in ascending order
56+
// since sortingDirection=1.
57+
bitonicMerge(arr, low, noOfEleToBeSorted, sortingDirection);
58+
}
59+
}
60+
61+
/**
62+
* Caller of bitonicSort for sorting the entire array of length N in
63+
* ASCENDING order
64+
*/
65+
void sort(int arr[], int N, int up) {
66+
bitonicSort(arr, 0, N, up);
67+
}
68+
69+
/* A utility function to print array of size n */
70+
static void printArray(int arr[]) {
71+
int n = arr.length;
72+
for (int i = 0; i < n; ++i)
73+
System.out.print(arr[i] + " ");
74+
System.out.println();
75+
}
76+
77+
// Driver method
78+
public static void main(String args[]) {
79+
int arr[] = { 3, 7, 4, 8, 6, 2, 1, 5 };
80+
int up = 1;
81+
BitonicSort ob = new BitonicSort();
82+
ob.sort(arr, arr.length, up);
83+
System.out.println("\nSorted array");
84+
printArray(arr);
85+
}
86+
}

0 commit comments

Comments
 (0)