algoalgo-world
algoalgo-world/sort/bitonic-sort
sort/bitonic-sort

바이토닉 정렬

최선
O(n log^2 n)
평균
O(n log^2 n)
최악
O(n log^2 n)
공간
O(1)
안정성
불안정
방식
정렬 네트워크

언어를 고르면 코드가 열린다

def bitonic_merge(a, lo, length, ascending):
    if length <= 1:
        return
    half = length // 2
    for i in range(lo, lo + half):
        if (a[i] > a[i + half]) == ascending:
            a[i], a[i + half] = a[i + half], a[i]
    bitonic_merge(a, lo, half, ascending)
    bitonic_merge(a, lo + half, half, ascending)


def bitonic_sort(a, lo, length, ascending=True):
    if length <= 1:
        return
    half = length // 2
    bitonic_sort(a, lo, half, True)
    bitonic_sort(a, lo + half, half, False)
    bitonic_merge(a, lo, length, ascending)
function bitonicMerge(a, lo, length, ascending) {
  if (length <= 1) return;
  const half = length >> 1;
  for (let i = lo; i < lo + half; i++) {
    if (a[i] > a[i + half] === ascending) {
      [a[i], a[i + half]] = [a[i + half], a[i]];
    }
  }
  bitonicMerge(a, lo, half, ascending);
  bitonicMerge(a, lo + half, half, ascending);
}

function bitonicSort(a, lo, length, ascending = true) {
  if (length <= 1) return;
  const half = length >> 1;
  bitonicSort(a, lo, half, true);
  bitonicSort(a, lo + half, half, false);
  bitonicMerge(a, lo, length, ascending);
}
static void swap(int a[], int i, int j) {
    int t = a[i];
    a[i] = a[j];
    a[j] = t;
}

static void bitonic_merge(int a[], int lo, int length, int ascending) {
    if (length <= 1) return;
    int half = length / 2;
    for (int i = lo; i < lo + half; i++) {
        if ((a[i] > a[i + half]) == ascending) swap(a, i, i + half);
    }
    bitonic_merge(a, lo, half, ascending);
    bitonic_merge(a, lo + half, half, ascending);
}

void bitonic_sort(int a[], int lo, int length, int ascending) {
    if (length <= 1) return;
    int half = length / 2;
    bitonic_sort(a, lo, half, 1);
    bitonic_sort(a, lo + half, half, 0);
    bitonic_merge(a, lo, length, ascending);
}
static void bitonic_merge(std::vector<int>& a, int lo, int length,
                          bool ascending) {
    if (length <= 1) return;
    int half = length / 2;
    for (int i = lo; i < lo + half; i++) {
        if ((a[i] > a[i + half]) == ascending) {
            std::swap(a[i], a[i + half]);
        }
    }
    bitonic_merge(a, lo, half, ascending);
    bitonic_merge(a, lo + half, half, ascending);
}

void bitonic_sort(std::vector<int>& a, int lo, int length, bool ascending) {
    if (length <= 1) return;
    int half = length / 2;
    bitonic_sort(a, lo, half, true);
    bitonic_sort(a, lo + half, half, false);
    bitonic_merge(a, lo, length, ascending);
}
static void BitonicMerge(int[] a, int lo, int length, bool ascending) {
    if (length <= 1) return;
    int half = length / 2;
    for (int i = lo; i < lo + half; i++) {
        if (a[i] > a[i + half] == ascending) {
            (a[i], a[i + half]) = (a[i + half], a[i]);
        }
    }
    BitonicMerge(a, lo, half, ascending);
    BitonicMerge(a, lo + half, half, ascending);
}

static void BitonicSort(int[] a, int lo, int length, bool ascending) {
    if (length <= 1) return;
    int half = length / 2;
    BitonicSort(a, lo, half, true);
    BitonicSort(a, lo + half, half, false);
    BitonicMerge(a, lo, length, ascending);
}
static void swap(int[] a, int i, int j) {
    int t = a[i];
    a[i] = a[j];
    a[j] = t;
}

static void bitonicMerge(int[] a, int lo, int length, boolean ascending) {
    if (length <= 1) return;
    int half = length / 2;
    for (int i = lo; i < lo + half; i++) {
        if (a[i] > a[i + half] == ascending) swap(a, i, i + half);
    }
    bitonicMerge(a, lo, half, ascending);
    bitonicMerge(a, lo + half, half, ascending);
}

static void bitonicSort(int[] a, int lo, int length, boolean ascending) {
    if (length <= 1) return;
    int half = length / 2;
    bitonicSort(a, lo, half, true);
    bitonicSort(a, lo + half, half, false);
    bitonicMerge(a, lo, length, ascending);
}
돌려 보고 코드도 본다