-
Notifications
You must be signed in to change notification settings - Fork 20k
Improving BitonicSort #5244
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Improving BitonicSort #5244
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b9430f2
Improving BitonicSort
2388655
Moving max method to SortingUtils
50ccde3
Adding Javadoc to merge method
93d7a4d
Fix for test and code improvements
27a3a56
Improving code readability
521d823
Renaming method parameters
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,79 +1,109 @@ | ||
package com.thealgorithms.sorts; | ||
|
||
/* Java program for Bitonic Sort. Note that this program | ||
works only when size of input is a power of 2. */ | ||
public class BitonicSort { | ||
|
||
/* The parameter dir indicates the sorting direction, | ||
ASCENDING or DESCENDING; if (a[i] > a[j]) agrees | ||
with the direction, then a[i] and a[j] are | ||
interchanged. */ | ||
void compAndSwap(int[] a, int i, int j, int dir) { | ||
if ((a[i] > a[j] && dir == 1) || (a[i] < a[j] && dir == 0)) { | ||
// Swapping elements | ||
int temp = a[i]; | ||
a[i] = a[j]; | ||
a[j] = temp; | ||
import java.util.Arrays; | ||
|
||
/** | ||
* BitonicSort class implements the SortAlgorithm interface using the bitonic sort technique. | ||
*/ | ||
public class BitonicSort implements SortAlgorithm { | ||
|
||
/** | ||
* Sorts the given array using the Bitonic Sort algorithm. | ||
* | ||
* @param <T> the type of elements in the array, which must implement the Comparable interface | ||
* @param arr the array to be sorted | ||
* @return the sorted array | ||
*/ | ||
@Override | ||
public <T extends Comparable<T>> T[] sort(T[] arr) { | ||
if (arr == null || arr.length == 0) { | ||
return arr; | ||
} | ||
|
||
int n = arr.length; | ||
int paddedSize = nextPowerOfTwo(n); | ||
T[] paddedArray = Arrays.copyOf(arr, paddedSize); | ||
|
||
// Fill the padded part with a maximum value | ||
T maxValue = findMax(arr); | ||
Arrays.fill(paddedArray, n, paddedSize, maxValue); | ||
|
||
bitonicSort(paddedArray, 0, paddedSize, true); | ||
|
||
return Arrays.copyOf(paddedArray, n); | ||
alxkm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
/* It recursively sorts a bitonic sequence in ascending | ||
order, if dir = 1, and in descending order otherwise | ||
(means dir=0). The sequence to be sorted starts at | ||
index position low, the parameter cnt is the number | ||
of elements to be sorted.*/ | ||
void bitonicMerge(int[] a, int low, int cnt, int dir) { | ||
private <T extends Comparable<T>> void bitonicSort(T[] arr, int low, int cnt, boolean dir) { | ||
alxkm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (cnt > 1) { | ||
int k = cnt / 2; | ||
alxkm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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); | ||
|
||
// Sort first half in ascending order | ||
bitonicSort(arr, low, k, true); | ||
|
||
// Sort second half in descending order | ||
bitonicSort(arr, low + k, cnt - k, false); | ||
|
||
// Merge the whole sequence in ascending order | ||
bitonicMerge(arr, low, cnt, dir); | ||
} | ||
} | ||
|
||
/* This funcion first produces a bitonic sequence by | ||
recursively sorting its two halves in opposite sorting | ||
orders, and then calls bitonicMerge to make them in | ||
the same order */ | ||
void bitonicSort(int[] a, int low, int cnt, int dir) { | ||
private <T extends Comparable<T>> void bitonicMerge(T[] arr, int low, int cnt, boolean dir) { | ||
if (cnt > 1) { | ||
int k = cnt / 2; | ||
|
||
// sort in ascending order since dir here is 1 | ||
bitonicSort(a, low, k, 1); | ||
|
||
// sort in descending order since dir here is 0 | ||
bitonicSort(a, low + k, k, 0); | ||
for (int i = low; i < low + k; i++) { | ||
if (dir == (arr[i].compareTo(arr[i + k]) > 0)) { | ||
SortUtils.swap(arr, i, i + k); | ||
} | ||
} | ||
|
||
// Will merge whole sequence in ascending order | ||
// since dir=1. | ||
bitonicMerge(a, low, cnt, dir); | ||
bitonicMerge(arr, low, k, dir); | ||
bitonicMerge(arr, low + k, cnt - k, dir); | ||
} | ||
} | ||
|
||
/*Caller of bitonicSort for sorting the entire array | ||
of length N in ASCENDING order */ | ||
void sort(int[] a, int n, int up) { | ||
bitonicSort(a, 0, n, up); | ||
} | ||
/** | ||
* Finds the maximum element in the given array. | ||
* | ||
* @param <T> the type of elements in the array, which must implement the Comparable interface | ||
* @param array the array to be searched | ||
* @return the maximum element in the array | ||
* @throws IllegalArgumentException if the array is null or empty | ||
*/ | ||
public static <T extends Comparable<T>> T findMax(T[] array) { | ||
if (array == null || array.length == 0) { | ||
throw new IllegalArgumentException("Array must not be null or empty"); | ||
} | ||
|
||
/* 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] + " "); | ||
T max = array[0]; | ||
for (T element : array) { | ||
if (element.compareTo(max) > 0) { | ||
max = element; | ||
} | ||
} | ||
System.out.println(); | ||
return max; | ||
} | ||
|
||
public static void main(String[] args) { | ||
int[] a = {3, 7, 4, 8, 6, 2, 1, 5}; | ||
int up = 1; | ||
BitonicSort ob = new BitonicSort(); | ||
ob.sort(a, a.length, up); | ||
System.out.println("\nSorted array"); | ||
printArray(a); | ||
/** | ||
* Finds the next power of two greater than or equal to the given number. | ||
* | ||
* @param n the number | ||
* @return the next power of two | ||
*/ | ||
private static int nextPowerOfTwo(int n) { | ||
int count = 0; | ||
|
||
// First n in the below condition is for the case where n is 0 | ||
if ((n & (n - 1)) == 0) { | ||
return n; | ||
} | ||
|
||
while (n != 0) { | ||
n >>= 1; | ||
count += 1; | ||
} | ||
|
||
return 1 << count; | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/test/java/com/thealgorithms/sorts/BitonicSortTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.thealgorithms.sorts; | ||
|
||
public class BitonicSortTest extends SortingAlgorithmTest { | ||
private final BitonicSort bitonicSort = new BitonicSort(); | ||
|
||
@Override | ||
SortAlgorithm getSortAlgorithm() { | ||
return bitonicSort; | ||
alxkm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.