본문 바로가기

대학교 1-2/컴프

LABHW3_5

LABHW3_5

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <stdio.h>
#define SIZE 10
 
void selectionSort(int list [], int size)
{
    for (int i = 0; i < SIZE - 1; i++)
    {
        int temp, minIndex, min=999;
        for (int j = i; j < SIZE; j++)
            if (list[j] < min)
            {
                min = list[j];
                minIndex = j;
            }
 
        temp = list[i];
        list[i] = list[minIndex];
        list[minIndex] = temp;
    }
}
 
int main(void)
{
    int a[SIZE] = { 1771795899382278441 };
    int i, j, temp, minIndex, min;
 
    selectionSort(a, SIZE);
 
    for (i = 0; i < SIZE; i++)
        printf("%d ", a[i]);
    printf("\n");
    return 0;
}
cs

이거 선택정렬 예제랑 비슷하다

그냥 교수님이 설명해주신 예제를 함수화 시킨건데

그 설명해주신 예제는 아래와 같다

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <stdio.h>
#define SIZE 6
int main(void)
{
    int a[SIZE] = { 538127 };
    int i, j, temp, minIndex;
    for (i = 0; i < SIZE - 1; i++) {
        int min = 999;
        for (int j = i; j < SIZE; j++)
            if (a[j] < min) // minIndex를 찾는다
            {
                min = a[j];
                minIndex = j;
            }
        temp = a[i];// i번째 원소와 minIndex 위치의 원소를 교환
        a[i] = a[minIndex];
        a[minIndex] = temp;
    }
    for (i = 0; i < SIZE; i++)
        printf("%d ", a[i]);
    printf("\n");
    return 0;
}
cs

'대학교 1-2 > 컴프' 카테고리의 다른 글

LAB16_3  (0) 2023.09.21
LAB16_1  (0) 2023.09.21
LABHW3_4_2  (0) 2023.09.18
LABHW3_4_1  (0) 2023.09.18
LABHW3_3_1  (0) 2023.09.12