Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
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 86 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);
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.