정성훈
    (추가) 실습 프로그램 예) // 구조체 버블 정렬
struct_bubble_sort.png [50 KB]   struct_bubble_soft_dstruct.txt [3 KB]  




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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <stdio.h>
#include <string.h>
 
#define N 4
 
// 구조체 정의
struct student_info {
    char name[10];
    int age;
    float weight;
};
 
struct student_info students[N] = {
    {"홍길동", 504, 67.5},
    {"이순신", 703, 80.3},
    {"강감찬", 645, 77.0},
    {"유관순", 205, 56.5}
};
 
// 구조체 배열 출력 함수
void print_students(struct student_info arr[], int n) {
    for (int i = 0; i < n; i++) {
        printf("%-6s  Age: %3d  Weight: %5.1f\n",
            arr[i].name, arr[i].age, arr[i].weight);
    }
    printf("\n");
}
 
// bubble sort: 이름 기준
void bubble_sort_name(struct student_info arr[], int n) {
    struct student_info tmp;
    for (int j = 0; j < n - 1; j++) {
        for (int i = 0; i < n - 1 - j; i++) {
            if (strcmp(arr[i].name, arr[i + 1].name) > 0) {
                tmp = arr[i];
                arr[i] = arr[i + 1];
                arr[i + 1] = tmp;
            }
        }
    }
}
 
// bubble sort: 나이 기준
void bubble_sort_age(struct student_info arr[], int n) {
    struct student_info tmp;
    for (int j = 0; j < n - 1; j++) {
        for (int i = 0; i < n - 1 - j; i++) {
            if (arr[i].age > arr[i + 1].age) {
                tmp = arr[i];
                arr[i] = arr[i + 1];
                arr[i + 1] = tmp;
            }
        }
    }
}
 
// bubble sort: 몸무게 기준
void bubble_sort_weight(struct student_info arr[], int n) {
    struct student_info tmp;
    for (int j = 0; j < n - 1; j++) {
        for (int i = 0; i < n - 1 - j; i++) {
            if (arr[i].weight > arr[i + 1].weight) {
                tmp = arr[i];
                arr[i] = arr[i + 1];
                arr[i + 1] = tmp;
            }
        }
    }
}
 
int main() {
 
    struct student_info students2[N], students3[N];
    memcpy(students2, students, sizeof(students));
    memcpy(students3, students, sizeof(students));
 
    // 이름으로 정렬
    printf("이름순 정렬:\n");
    bubble_sort_name(students, N);
    print_students(students, N);
 
 
    // 나이순 정렬
    printf("나이순 정렬:\n");
    bubble_sort_age(students2, N);
    print_students(students2, N);
 
    // 몸무게순 정렬
    printf("몸무게순 정렬:\n");
    bubble_sort_weight(students3, N);
    print_students(students3, N);
 
    return 0;
}
cs

  등록일 : 2025-09-07 [16:47] 조회 : 305 다운 : 250   
 
△ 이전글(추가) 실습 프로그램 예) // 3) 구조체 전체 copy
▽ 다음글(실습 2) 순환 // 4주차 오프라인 실습
자료구조 실습게시판
번호 ⓒ 제 목
실습 운영 방안
[참고] 교재에 있는 소스코드
실습 목차
MS Visual Studio 2026에서 한글 깨지는 문제 해결 방법 (gemini)
31 └❶ l실습 프로그램 예)
30    └❷ l실습 프로그램 예)
29       └❸ 💯 구조체 데이터의 swap (추가)ⓘ
28          └❹ (추가) 실습 프로그램 예) // 1) 구조체 복사©
27             └❺ (추가) 실습 프로그램 예) // 2) 구조체 배열의 첫번째와 두번째를 swap
26                └❻ (추가) 실습 프로그램 예) // 3) 구조체 전체 copy
25                   └❼ (추가) 실습 프로그램 예) // 구조체 버블 정렬
24 l(실습 2) 순환 // 4주차 오프라인 실습ⓘ
23 └❶ l실습 프로그램 예)
22    └❷ l실습 프로그램 예)
21       └❸ l실습 프로그램 예)
20          └❹ 💯 power 다른 순환 구현 및 구구단 출력 (추가)ⓘ
19             └❺ (추가) 실습 프로그램 예) // power 구현
18                └❻ (추가) 실습 프로그램 예) // 구구단 구현
17 l(실습 1) 선택정렬 // 3주차 오프라인 실습
16 └❶ l실습 프로그램 예)
15    └❷ 💯 문자열 정렬 (추가)
14       └❸ (추가) 실습 프로그램 예) // 선택정렬
13          └❹ (추가) 실습 프로그램 예) // 버블정렬
12 └❶ 버블 정렬

[1][2][3][4][5]