Skip to content

Added bitonic sort #244

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

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 86 additions & 0 deletions Sorts/BitonicSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@

public class BitonicSort {
/**
* The parameter sortingDirection 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 compareAndSwap(int arr[], int i, int j, int sortingDirection) {
if ((arr[i] > arr[j] && sortingDirection == 1) || (arr[i] < arr[j] && sortingDirection == 0)) {
// Swapping elements
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}

/**
* This function is being called to merge two sorted arrays in same order.
*
* @param arr - array to be sorted
* @param low - starting index position of the sequence to be sorted
* @param noOfEleToBeSorted - Number of elements to be sorted
* @param sortingDirection - 0 -> ascending direction, 1 -> descending direction
*/
void bitonicMerge(int arr[], int low, int noOfEleToBeSorted, int sortingDirection) {
if (noOfEleToBeSorted > 1) {
int k = noOfEleToBeSorted / 2;
for (int i = low; i < low + k; i++)
compareAndSwap(arr, i, i + k, sortingDirection);
bitonicMerge(arr, low, k, sortingDirection);
bitonicMerge(arr, low + k, k, sortingDirection);
}
}

/**
* This function 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
*
* @param arr - array to be sorted
* @param low - starting index position of the sequence to be sorted
* @param noOfEleToBeSorted - Number of elements to be sorted
* @param sortingDirection - 0 -> ascending direction, 1 -> descending direction
*/
void bitonicSort(int arr[], int low, int noOfEleToBeSorted, int sortingDirection) {
if (noOfEleToBeSorted > 1) {
int k = noOfEleToBeSorted / 2;

// sort in ascending order since sortingDirection here is 1
bitonicSort(arr, low, k, 1);

// sort in descending order since sortingDirection here is 0
bitonicSort(arr, low + k, k, 0);

// Will merge whole sequence in ascending order
// since sortingDirection=1.
bitonicMerge(arr, low, noOfEleToBeSorted, sortingDirection);
}
}

/**
* Caller of bitonicSort for sorting the entire array of length N in
* ASCENDING order
*/
void sort(int arr[], int N, int up) {
bitonicSort(arr, 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[]) {
int arr[] = { 3, 7, 4, 8, 6, 2, 1, 5 };
int up = 1;
BitonicSort ob = new BitonicSort();
ob.sort(arr, arr.length, up);
System.out.println("\nSorted array");
printArray(arr);
}
}