In this tutorial you will learn about the Bitonic Sort Algorithm and its application with practical example.
Bitonic Sort Program In C
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 |
#include<stdio.h> void swap(int arr[], int i, int j, int d) { int temp; if (d==(arr[i]>arr[j])) { temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } void merge(int arr[], int l, int c, int d) { int k,i; if (c>1) { k = c/2; for (i=l; i<l+k; i++) swap(arr, i, i+k, d); merge(arr, l, k, d); merge(arr, l+k, k, d); } } void bitonic_sort(int arr[],int l, int c, int d) { int k; if (c>1) { k = c/2; bitonic_sort(arr, l, k, 1); bitonic_sort(arr, l+k, k, 0); merge(arr,l, c, d); } } void sort(int arr[], int n, int order) { bitonic_sort(arr,0, n, order); } int main() { int i, n, a[10]; printf("Enter number of elements: "); scanf("%d",&n); printf("Enter %d integer numbers\n", n); for(i = 0; i < n; i++) { scanf("%d",&a[i]); } int order = 1; sort(a, n, order); printf("Elements after sorting: "); for (i=0; i<n; i++) printf("%d ", a[i]); } |
Output:-