C 언어 배열 sort 함수 - C eon-eo baeyeol sort hamsu

프로그램언어/C언어

2019. 10. 21. 12:04

qsort()함수를 통해 숫자와 문자열을 오름차순 정렬과 내림차순 정렬을 실행해보고 구조체에서 숫자필드를 기준으로 오름차순 정렬과 내림차순 정렬을 실행해보고 구조체의 문자열필드를 기준으로 오름차순 정렬과 내림차순정렬을 실행해 보자.

c언어 qsort()함수는 정렬하는 함수로 string.h 라이브러리에서 제공하고 있습니다. qsort()함수를 사용하기 위해서는 헤더파일에 #inlcude <stdlib.h>를 추가해야 합니다.

qsort() 함수 :  quick sort 알고리즘 함수

함수 원형

    void qsort(void *base, size_t num, size_t width, int (__cdecl *compare )(const void *, const void *)); 

base : 정렬할 배열의 첫번째 포인터 
num : 배열의 개수
width : 배열 하나의 크기 
(*compar) : 비교함수
반환값 : void형으로 반환값이 없음

sqort() 문자열 오름차순 정렬 예제

소스코드 실행 결과

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

#define COUNT 10 

int compare(const void *a, const void *b); 

int main(void) 
{ 
int i; 
    char arr[COUNT][20] = { "C-Language", "Python", "Java", "Php", "jQuery","API", "MFC", "Unity", "C#", "Jsp" }; 

    qsort(arr, COUNT, sizeof(arr[0]), compare); 

    for (i = 0; i < COUNT; i++) 
    { 
         printf("%2d번 : %s\n", i + 1, arr[i]); 
    } 
    printf("\n"); 

    return 0; 
} 

int compare(const void *a, const void *b) 
{ 
    return strcmp((char*)a, (char*)b)[

}

 1번 : API
 2번 : C#
 3번 : C-Language
 4번 : Java
 5번 : Jsp
 6번 : MFC
 7번 : Php
 8번 : Python
 9번 : Unity
10번 : jQuery

qsort() 숫자 오름차순 정렬 예제

소스코드 실행 결과

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

#define COUNT 10 

int compare(const void *a , const void *b); 

int main(void) 
{ 
 int i; 
 int arr[COUNT] = {5,9,2,7,10,3,6,4,8,1}; 

    qsort(arr , COUNT , sizeof(arr[0]) , compare); 

    for(i = 0 ; i < COUNT ; i++) 
    { 
        printf("%2d번 : %d\n" , i+1, arr[i]); 
    } 
    printf("\n"); 

    return 0; 
} 

int compare(const void *a , const void *b) 
{ 
     if( *(int*)a > *(int*)b )

        return 1;

    else if( *(int*)a < *(int*)b )

        return -1;

    else

        return 0;
} 

 1번 : 1 
 2번 : 2 
 3번 : 3 
 4번 : 4 
 5번 : 5 
 6번 : 6 
 7번 : 7 
 8번 : 8 
 9번 : 9 
10번 : 10 

qsort()함수를 이용하여 구조체를 내림차순과 오름차순으로 정렬하기

qsort()함수로 구조체에서 문자열을 기준을 오름차순과 내림차순으로 정렬하기

소스코드 실행 결과
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct score {
     char name[10];
     int kor;
     int com;
     int mat;
};

int compare(const void *a, const void *b)  //오름차순정렬
{
    score *pa = (score*)a;
    score *pb = (score*)b;

    return strcmp(pa->name, pb->name);
}

int compare1(const void *a, const void *b)  //내림차순정렬
{
    score *pa = (score*)a;
    score *pb = (score*)b;

    return strcmp(pb->name, pa->name);
}

int main() {
    struct score x[3]= {{"홍길동", 70, 80, 90},
                            {"일지매", 80, 70, 80},
                            {"임꺽정", 100, 60, 60}};

    qsort(x, 3, sizeof(score), compare);

    printf("오름차순정렬(이름)\n");
    printf("%6s %6s %6s %6s\n", "이름", "국어", "컴퓨터", "수학");
    for(int i=0; i<3; i++) {
        printf("%6s %6d %6d %6d\n", x[i].name, x[i].kor, x[i].com, x[i].mat);
    }

    qsort(x, 3, sizeof(score), compare1);

    printf("\n내림차순정렬(이름)\n");
    printf("%6s %6s %6s %6s\n", "이름", "국어", "컴퓨터", "수학");
    for(int i=0; i<3; i++) {
        printf("%6s %6d %6d %6d\n", x[i].name, x[i].kor, x[i].com, x[i].mat);
    }

    return 0;
}

오름차순정렬(이름)
  이름   국어 컴퓨터   수학
일지매     80     70     80
임꺽정    100     60     60
홍길동     70     80     90

내림차순정렬(이름)
  이름   국어 컴퓨터   수학
홍길동     70     80     90
임꺽정    100     60     60
일지매     80     70     80

qsort()함수로 구조체에서 숫자를 기준을 내림차순과 오름차순으로 정렬하기

소스코드 실행 결과
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct score { 
     char name[10]; 
     int kor; 
     int com; 
     int mat; 
}; 

int compare(const void *a, const void *b)  //내림차순 정렬
{
    score *pa = (score*)a;
    score *pb = (score*)b;

    if(pa->sum < pb->sum)
        return 1;
    else if(pa->sum > pb->sum)
        return -1;
    else
    return 0;
}

int compare1(const void *a, const void *b)  //오름차순 정렬
{
    score *pa = (score*)a;
    score *pb = (score*)b;

    if(pa->sum > pb->sum)
        return 1;
    else if(pa->sum < pb->sum)
        return -1;
    else
        return 0;
}

int main() {
    struct score x[3]= {{"홍길동", 70, 80, 90, 0},
                             {"일지매", 80, 70, 80, 0},
                             {"임꺽정", 100, 60, 60, 0}};
    for(int i=0; i<3; i++) {
        x[i].sum = x[i].kor + x[i].com + x[i].mat;
    }

    qsort(x, 3, sizeof(score), compare);

    printf("내림차순정렬(합계)\n");
    printf("%6s %6s %6s %6s %6s\n", "이름", "국어", "컴퓨터", "수학", "합계");
    for(int i=0; i<3; i++) {
        printf("%6s %6d %6d %6d %6d\n", x[i].name,x[i].kor,x[i].com,x[i].mat,x[i].sum);
    }

    qsort(x, 3, sizeof(score), compare1);

    printf("\n오름차순정렬(합계)\n");
    printf("%6s %6s %6s %6s %6s\n", "이름", "국어", "컴퓨터", "수학", "합계");
    for(int i=0; i<3; i++) {
        printf("%6s %6d %6d %6d %6d\n", x[i].name,x[i].kor,x[i].com,x[i].mat,x[i].sum);
    }

    return 0;
}

내림차순정렬(합계)
  이름   국어 컴퓨터   수학   합계
홍길동     70     80     90    240
일지매     80     70     80    230
임꺽정    100     60     60    220

오름차순정렬(합계)
  이름   국어 컴퓨터   수학   합계
임꺽정    100     60     60    220
일지매     80     70     80    230
홍길동     70     80     90    240

'프로그램언어/C언어' Related Articles

Toplist

최신 우편물

태그