ADVERTISEMENT
ADVERTISEMENT

C program to implement Bitonic sort algorithm to sort an array in ascending order

Here, we are going to learn how to implement Bitonic sort algorithm to sort an array in ascending order using C program?
Submitted by Nidhi, on August 16, 2021

Problem Solution:

Here, we will create an array of integers then we will read array elements from the user and implement a Bitonic sort algorithm to arrange array elements in ascending order.

Program:

The source code to implement the Bitonic sort algorithm is given below. The given program is compiled and executed using GCC compile on UBUNTU 18.04 OS successfully.

// C program to implement Bitonic sort algorithm

#include <stdio.h>

#define MAX 8

int arr[MAX];
int up = 1;
int down = 0;

void Exchange(int* num1, int* num2)
{
    int temp;

    temp = *num1;
    *num1 = *num2;
    *num2 = temp;
}

void compare(int i, int j, int dir)
{
    int t;

    if (dir == (arr[i] > arr[j])) {
        Exchange(&arr[i], &arr[j]);
    }
}

void bitonicmerge(int low, int c, int dir)
{
    int k = 0;
    int i = 0;

    if (c > 1) {
        k = c / 2;
        i = low;
        while (i < low + k) {
            compare(i, i + k, dir);
            i++;
        }

        bitonicmerge(low, k, dir);
        bitonicmerge(low + k, k, dir);
    }
}

void recbitonic(int low, int v, int dir)
{
    int k = 0;

    if (v > 1) {
        k = v / 2;
        recbitonic(low, k, up);
        recbitonic(low + k, k, down);
        bitonicmerge(low, v, dir);
    }
}

int main()
{
    int i = 0;

    printf("Enter array elements:\n");
    while (i < MAX) {
        printf("Element[%d]: ", i);
        scanf("%d", &arr[i]);
        i++;
    }

    recbitonic(0, MAX, up);

    printf("Sorted Array: \n");
    i = 0;
    while (i < MAX) {
        printf("%d ", arr[i]);
        i++;
    }
    printf("\n");

    return 0;
}

Output:

RUN 1:
Enter array elements:
Element[0]: 23
Element[1]: 45 
Element[2]: 65
Element[3]: 23
Element[4]: 87
Element[5]: 56
Element[6]: 98
Element[7]: 34
Sorted Array: 
23 23 34 45 56 65 87 98

RUN 2:
Enter array elements:
Element[0]: 10
Element[1]: 12
Element[2]: 34
Element[3]: 5
Element[4]: 15
Element[5]: 10
Element[6]: 25
Element[7]: 67
Sorted Array: 
5 10 10 12 15 25 34 67 

Explanation:

Here, we created five functions Exchange(), compare(), bitonicmerge(), recbitonic(), and main().The Exchange() function is used to swap values of two variable. The compare() function is used to compare array element and perform swapping. The bitonicmerge(), recbitonic() functions are used to sort bitonic sequence in ascending order.

In the main() function, we read array elements from the user. Then we sorted the elements of the array using the recbitonic() function and printed the sorted array on the console screen.

ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.