algoalgo-world
algoalgo-world/sort/cycle-sort
sort/cycle-sort

사이클 정렬

최선
O(n^2)
평균
O(n^2)
최악
O(n^2)
공간
O(1)
안정성
불안정
방식
비교 기반

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

def cycle_sort(a):
    n = len(a)
    for start in range(n - 1):
        value = a[start]
        pos = start + sum(1 for i in range(start + 1, n) if a[i] < value)
        if pos == start:
            continue
        while value == a[pos]:
            pos += 1
        a[pos], value = value, a[pos]
        while pos != start:
            pos = start + sum(1 for i in range(start + 1, n) if a[i] < value)
            while value == a[pos]:
                pos += 1
            a[pos], value = value, a[pos]
function cycleSort(a) {
  for (let start = 0; start < a.length - 1; start++) {
    let value = a[start];
    let pos = placeOf(a, start, value);
    if (pos === start) continue;
    while (value === a[pos]) pos++;
    [a[pos], value] = [value, a[pos]];
    while (pos !== start) {
      pos = placeOf(a, start, value);
      while (value === a[pos]) pos++;
      [a[pos], value] = [value, a[pos]];
    }
  }
}

function placeOf(a, start, value) {
  let pos = start;
  for (let i = start + 1; i < a.length; i++) {
    if (a[i] < value) pos++;
  }
  return pos;
}
static int place_of(int a[], int n, int start, int value) {
    int pos = start;
    for (int i = start + 1; i < n; i++) {
        if (a[i] < value) pos++;
    }
    return pos;
}

void cycle_sort(int a[], int n) {
    for (int start = 0; start < n - 1; start++) {
        int value = a[start];
        int pos = place_of(a, n, start, value);
        if (pos == start) continue;
        while (value == a[pos]) pos++;
        int t = a[pos]; a[pos] = value; value = t;
        while (pos != start) {
            pos = place_of(a, n, start, value);
            while (value == a[pos]) pos++;
            t = a[pos]; a[pos] = value; value = t;
        }
    }
}
static size_t place_of(const std::vector<int>& a, size_t start, int value) {
    size_t pos = start;
    for (size_t i = start + 1; i < a.size(); i++) {
        if (a[i] < value) pos++;
    }
    return pos;
}

void cycle_sort(std::vector<int>& a) {
    for (size_t start = 0; start + 1 < a.size(); start++) {
        int value = a[start];
        size_t pos = place_of(a, start, value);
        if (pos == start) continue;
        while (value == a[pos]) pos++;
        std::swap(a[pos], value);
        while (pos != start) {
            pos = place_of(a, start, value);
            while (value == a[pos]) pos++;
            std::swap(a[pos], value);
        }
    }
}
static int PlaceOf(int[] a, int start, int value) {
    int pos = start;
    for (int i = start + 1; i < a.Length; i++) {
        if (a[i] < value) pos++;
    }
    return pos;
}

static void CycleSort(int[] a) {
    for (int start = 0; start < a.Length - 1; start++) {
        int value = a[start];
        int pos = PlaceOf(a, start, value);
        if (pos == start) continue;
        while (value == a[pos]) pos++;
        (a[pos], value) = (value, a[pos]);
        while (pos != start) {
            pos = PlaceOf(a, start, value);
            while (value == a[pos]) pos++;
            (a[pos], value) = (value, a[pos]);
        }
    }
}
static int placeOf(int[] a, int start, int value) {
    int pos = start;
    for (int i = start + 1; i < a.length; i++) {
        if (a[i] < value) pos++;
    }
    return pos;
}

static void cycleSort(int[] a) {
    for (int start = 0; start < a.length - 1; start++) {
        int value = a[start];
        int pos = placeOf(a, start, value);
        if (pos == start) continue;
        while (value == a[pos]) pos++;
        int t = a[pos]; a[pos] = value; value = t;
        while (pos != start) {
            pos = placeOf(a, start, value);
            while (value == a[pos]) pos++;
            t = a[pos]; a[pos] = value; value = t;
        }
    }
}
돌려 보고 코드도 본다