#include <stdio.h>

int main() {
    int arr[4] = {1, 2, 3, 4};
    //배열의 인덱스 0부터 끝까지 비교하는 것 = 1바퀴
    //한 바퀴 안에서 3번의 비교활동 발생
    //두 바퀴 안에서 2번의 비교활동 발생
    //세 바퀴 안에서 1번의 비교활동 발생
    //arr 배열 길이 = 4
    //바퀴 수 = 4-1 = 3
    //비교활동 4-1 = 3 -- 1

    //배열의 길이 구하기 
    int len = sizeof(arr)/sizeof(int);

    //중첩 반복
    for(int i=0; i<len-1; i++) //바퀴 반복
    {
        for(int j=0; j<len-1-i; j++) //비교활동 반복
        {
            if(arr[j] < arr[j+1])
            {
                int t; // 임시 저장
                t=arr[j];
                arr[j] = arr[j+1];
                arr[j+1] = t;
            }
        }
    }

    for(int i=0; i<len; 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: