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> } |
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> 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; } |
내림차순정렬(합계) |
'프로그램언어 > C언어' 카테고리의 다른 글
균형 이진 탐색트리 (0) | 2020.01.07 |
---|---|
아스키코드 (0) | 2019.11.12 |
c언어 문자열 분리함수와 문자열에서 일치되는 문자 검색과 일치되지 않는 문자검색 함수 (0) | 2019.10.18 |
c언어 문자을 검색하는 함수와 문자열를 검색하는 함수 (0) | 2019.10.18 |
c언어 문자열의 대문자, 소문자 변환 함수와 문자열을 거꾸로 뒤집는 함수 (0) | 2019.10.18 |