//common.h #pragma once //헤더 파일을 한 번만 포함해서 컴파일 #include <stdio.h> #include <stdlib.h> #include <conio.h> #include <string.h> #include <malloc.h> #include <memory.h> #include <time.h> #pragma warning(disable:4996) //4996컴파일 경고 메시지 출력 해제
//Book.h #pragma once #include "common.h" #define MAX_TIT_LEN 200 #define MAX_AUT_LEN 20 typedef struct _Book Book; struct _Book { char title[MAX_TIT_LEN+1]; char author[MAX_AUT_LEN+1]; int num; }; Book *New_Book(const char *title,const char *author,int num); void Delete_Book(Book *book); void Book_View(Book *book); int Book_CompareTitle(Book *book,const char *title); int Book_CompareAuthor(Book *book,const char *author); int Book_CompareNum(Book *book,int num);
//Book.c #include "Book.h" void Book_Book(Book *book,const char *title,const char *author,int num); Book *New_Book(const char *title,const char *author,int num) { Book *book = 0; book = (Book *)malloc(sizeof(Book)); Book_Book(book,title,author,num); return book; } void Book_Book(Book *book,const char *title,const char *author,int num) { memset(book,0,sizeof(Book)); strncpy(book->title,title,MAX_TIT_LEN); strncpy(book->author,author,MAX_AUT_LEN); book->num = num; } void Delete_Book(Book *book) { free(book); } void Book_View(Book *book) { printf("<%010d>:<%s>\n",book->num,book->title); printf("\t 저자:%s\n",book->author); } int Book_CompareTitle(Book *book,const char *title) { return strcmp(book->title,title); } int Book_CompareAuthor(Book *book,const char *author) { return strcmp(book->author,author); } int Book_CompareNum(Book *book,int num) { return book->num-num; }
//Program.c #include "common.h" #include "Book.h" typedef void *Element; typedef int (*Compare)(Element , Element); void merge_sort(Element *base, int n, Compare compare) { int ahalf = n/2; int bhalf = n - ahalf; int ai=0, bi=ahalf; int i=0; Element *tbase=0; if(n<=1){ return; } merge_sort(base, ahalf, compare); merge_sort(base+ahalf, bhalf, compare); tbase = (Element *)malloc(sizeof(Element)*n); memcpy(tbase,base,sizeof(Element)*n); while((ai<ahalf)&&(bi<n)) { if(compare(tbase[ai],tbase[bi])<=0) { base[i] = tbase[ai]; ai++; } else { base[i] = tbase[bi]; bi++; } i++; } while(ai<ahalf) { base[i] = tbase[ai]; i++; ai++; } while(bi<n) { base[i] = tbase[bi]; i++; bi++; } free(tbase); } #define MAX_BOOK 4000 Book *books[MAX_BOOK]={0}; void SimulationInit() { char title[MAX_TIT_LEN+1]=""; char author[MAX_AUT_LEN+1]=""; int i = 0; for(i=0; i<MAX_BOOK; ++i) { sprintf_s(title,sizeof(title),"%010d",rand()); sprintf_s(author,sizeof(author),"%010d",rand()); books[i] = New_Book(title,author,rand()); } } int CompareByTitle(void *b1,void *b2) { Book *book1=(Book *)b1; Book *book2=(Book *)b2; return Book_CompareTitle(book1,book2->title); } int CompareByNum(void *b1,void *b2) { Book *book1=(Book *)b1; Book *book2=(Book *)b2; return Book_CompareNum(book1,book2->num); } void ListBook(int n) { int i = 0; for(i=0; i<n; ++i){ Book_View(books[i]); } } void Simulation1() { merge_sort((Element *)books,10,CompareByTitle); printf("--------제목순-------\n"); ListBook(10); merge_sort((Element *)books,10,CompareByNum); printf("--------번호순-------\n"); ListBook(10); } void Simulation2() { clock_t st,et; st = clock(); merge_sort((Element *)books,MAX_BOOK/10,CompareByNum); et=clock(); printf("%d개 정렬에 걸린 시간:%d\n",MAX_BOOK/10,et-st); st = clock(); merge_sort((Element *)books,MAX_BOOK,CompareByNum); et=clock(); printf("%d개 정렬에 걸린 시간:%d\n",MAX_BOOK,et-st); } void SimulationClear() { int i = 0; for(i=0; i<MAX_BOOK; ++i){ Delete_Book(books[i]); } } int main() { SimulationInit(); Simulation1(); Simulation2(); SimulationClear(); return 0; }
▷실행 결과
--------제목순------- <0000006334>:<0000000041> 저자:0000018467 <0000017421>:<0000000292> 저자:0000012382 <0000011942>:<0000000491> 저자:0000002995 <0000032391>:<0000004827> 저자:0000005436 <0000026962>:<0000011478> 저자:0000029358 <0000000153>:<0000014604> 저자:0000003902 <0000019895>:<0000018716> 저자:0000019718 <0000009961>:<0000023281> 저자:0000016827 <0000028145>:<0000024464> 저자:0000005705 <0000015724>:<0000026500> 저자:0000019169 --------번호순------- <0000000153>:<0000014604> 저자:0000003902 <0000006334>:<0000000041> 저자:0000018467 <0000009961>:<0000023281> 저자:0000016827 <0000011942>:<0000000491> 저자:0000002995 <0000015724>:<0000026500> 저자:0000019169 <0000017421>:<0000000292> 저자:0000012382 <0000019895>:<0000018716> 저자:0000019718 <0000026962>:<0000011478> 저자:0000029358 <0000028145>:<0000024464> 저자:0000005705 <0000032391>:<0000004827> 저자:0000005436 400개 정렬에 걸린 시간:0 4000개 정렬에 걸린 시간:4