bitonic sort

Reference: https://www.geeksforgeeks.org/dsa/bitonic-sort/

We start from this sequence: 3, 7, 4, 8, 6, 2, 1, 5.

We define these vars:
entire_length = 8
stride
length_pair=2

step1(construct bitonic seq)

length_pair from 2 to entire_length, length_pair = length_pair*2, i.e. [2,4,8]
stride from [length_pair/2 to 1, stride /= 2, i.e. [1]

iter0

length_pair=2, stride=1
ascending order [3,7]. descending order [4,8] -> [8,4]. ascending order[6,2] -> [2,6]. descending order[1,5] -> [5,1].
we have [3,7,8,4,2,6,5,1] after iter0.

iter1

length_pair=4, stride=2
when stride = 2.
ascending order [3,7,8,4], compare 3 with 8 since stride = 2. compare 7 with 4 as well.
descending order [2,6,5,1]. compare 2 with 5 since stride = 2. compare 6 with 1 as well.
we have [3,4,8,7,5,6,2,1] after this iter.

length_pair=4, stride=1
when stride = 1.
ascending within [3,4], and [8,7], we have [3,4,7,8]
descending within [5,6] and [2,1], we have[6,5,2,1].
so, we have [3,4,7,8,6,5,2,1] after this iter1.

end of step1

length_pair=8
We reache state ep, since length_pair == entire_length_pair, i.e. 8
The result of step1 is [3,4,7,8,6,5,2,1]

step2(merge)

Now, there are two sub parts in this seq.
[3,4,7,8] and [6,5,2,1]
Since we have reached state ep(length_pair == entire_length_pair), we start compare each element in first part with counterpart in second part to ensure ascending order.
stride from length_pair/2 to 1, stride/=2. i.e. [4,2,1]
length_pair from entire_length_pair to 1, length_pair/=2, i.e. [8,4,2,1]. 1 refers to end.

iter0

length_pair=8
stride=4
compare 3 with 6, 4 with 5, 7 with 2, 8 with 1.
we get [3,4,2,1,6,5,7,8]

iter1

length_pair=8
stride=2
comapre 3 with 2, 4 with 1, 6 with 7, 5 with 8.
we get [2,1,3,4,6,5,7,8]

iter2

length_pair=8
stride=1
compare 2 with 1, 3 with 4, 6 wirth 5, 7 with 8.
we get [1,2,3,4,5,6,7,8]

/* C++ Program for Bitonic Sort. Note that this program
   works only when size of input is a power of 2. */
#include<bits/stdc++.h>
using namespace std;

/*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 (dir==(a[i]>a[j]))
        swap(a[i],a[j]);
}

/*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)
{
    if (cnt>1)
    {
        int k = cnt/2;
        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);
    }
}

/* 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 */
void bitonicSort(int a[],int low, int cnt, int 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);

        // Will merge whole sequence in ascending order
        // since dir=1.
        bitonicMerge(a,low, cnt, 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);
}

// Driver code
int main()
{
    int a[]= {3, 7, 4, 8, 6, 2, 1, 5};
    int N = sizeof(a)/sizeof(a[0]);

    int up = 1;   // means sort in ascending order
    sort(a, N, up);

    printf("Sorted array: \n");
    for (int i=0; i<N; i++)
        printf("%d ", a[i]);
    return 0;
}
posted @ 2025-12-04 11:48  ijpq  阅读(7)  评论(0)    收藏  举报