#include <stdio.h>
#include <stdlib.h>

typedef struct aa
{
    int a1; // 값
    int a2; // 원래 인덱스
} A;

// 구조체 스왑
void Swap(A arr[], int a, int b)
{
    A temp = arr[a];
    arr[a] = arr[b];
    arr[b] = temp;
}

// 분할 함수
int Partition(A arr[], int left, int right)
{
    int pivot = arr[left].a1;

    int low = left + 1;
    int high = right;

    while (low <= high)
    {
        while (low <= right && pivot >= arr[low].a1)
        {
            low++;
        }

        while (high >= left + 1 && pivot <= arr[high].a1)
        {
            high--;
        }

        if (low <= high)
        {
            Swap(arr, low, high);
        }
    }

    Swap(arr, left, high);

    return high;
}

// 퀵 정렬
void QuickSort(A arr[], int left, int right)
{
    if (left < right)
    {
        int pivot = Partition(arr, left, right);

        QuickSort(arr, left, pivot - 1);
        QuickSort(arr, pivot + 1, right);
    }
}

int main()
{
    int n;
    scanf("%d", &n);

    A aalist[n];
    int arr[n];

    // 입력
    for (int i = 0; i < n; i++)
    {
        scanf("%d", &aalist[i].a1);
        aalist[i].a2 = i;
    }

    // 정렬
    QuickSort(aalist, 0, n - 1);

    // 원래 인덱스 기준으로 정렬된 위치 저장
    for (int i = 0; i < n; i++)
    {
        arr[aalist[i].a2] = i;
    }

    // 출력
    for (int i = 0; i < n; i++)
    {
        printf("%d ", arr[i]);
    }

    return 0;
}

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: