[출처 : http://inthechaos.tistory.com/entry/정렬-SortMainc]

#pragma warning(disable:4996)
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include "Sort.h"

void fillArray(int *array, int size); void printArray(const int *array, int size); void bubbleTest(const int *array, int size); void selectionTest(const int *array, int size); void insertionTest(const int *array, int size); void heapTest(const int *array, int size); void mergeTest(const int *array, int size); void quickTest(const int *array, int size); void main(void) { int *array=NULL; int i, size; srand((unsigned) time(NULL)); //초기화 size = 4096; array = malloc(size*128*sizeof(int)); //저속 정렬 printf("[저속 정렬]\n"); for(i=0; i<4; i++) { fillArray(array, size); printf("개수 : %d\n", size); bubbleTest(array, size); selectionTest(array, size); insertionTest(array, size); printf("\n"); size *= 2; } //고속 정렬 printf("\n[고속 정렬]\n"); for(i=0; i<4; i++) { fillArray(array, size); printf("개수 : %d\n", size); heapTest(array, size); mergeTest(array, size); quickTest(array, size); printf("\n"); size *= 2; } free(array); } void fillArray(int *array, int size) { int i; for(i=0; i<size; i++) { array[i] = rand() * rand(); } } void printArray(const int *array, int size) { int i; for(i=0; i<size; i++) { printf("%10d ", array[i]); if(i%7 == 6) { printf("\n"); } } printf("\n"); } void bubbleTest(const int *array, int size) { clock_t start, end; int *temp; temp = malloc(size*sizeof(int)); memcpy(temp, array, size*sizeof(int)); start = clock(); bubbleSort(temp, size); end = clock(); printf("버블 : %.3f\n", (end-start) / (double) CLOCKS_PER_SEC); free(temp); } void selectionTest(const int *array, int size) { clock_t start, end; int *temp; temp = malloc(size*sizeof(int)); memcpy(temp, array, size*sizeof(int)); start = clock(); selectionSort(temp, size); end = clock(); printf("선택 : %.3f\n", (end-start) / (double) CLOCKS_PER_SEC); free(temp); } void insertionTest(const int *array, int size) { clock_t start, end; int *temp; temp = malloc(size*sizeof(int)); memcpy(temp, array, size*sizeof(int)); start = clock(); insertionSort(temp, size); end = clock(); printf("삽입 : %.3f\n", (end-start) / (double) CLOCKS_PER_SEC); free(temp); } void heapTest(const int *array, int size) { clock_t start, end; int *temp; temp = malloc(size*sizeof(int)); memcpy(temp, array, size*sizeof(int)); start = clock(); heapSort(temp, size); end = clock(); printf("힙 : %.3f\n", (end-start) / (double) CLOCKS_PER_SEC); free(temp); } void mergeTest(const int *array, int size) { clock_t start, end; int *temp; temp = malloc(size*sizeof(int)); memcpy(temp, array, size*sizeof(int)); start = clock(); mergeSort(temp, size); end = clock(); printf("합병 : %.3f\n", (end-start) / (double) CLOCKS_PER_SEC); free(temp); } void quickTest(const int *array, int size) { clock_t start, end; int *temp; temp = malloc(size*sizeof(int)+4); temp[size] = INT_MAX; memcpy(temp, array, size*sizeof(int)); start = clock(); quickSort(temp, size); end = clock(); printf("퀵 : %.3f\n", (end-start) / (double) CLOCKS_PER_SEC); free(temp); }

+ Recent posts