void bucket_sort(int *list, int l, int r) { int i, size, max=0; for (i = l; i <= r; i ++) if (list[i]>max) max = list[i]; size = max/10; init_bucket(size); insert_bucket(list, l, r); sort_bucket(list, size); del_bucket(size); }
maina() { int i = 0; int *task_array; int selected_sort = 1, selected_array = 1, selected_size = 1, size_of_array = 0, rand_min = 0, rand_max = 100; int command; int *array_selected = 0; bool recalled = FALSE; long double *ld_array, sum, variance, avg; setlocale(LC_ALL, "Korean"); { printf("\t정렬 알고리즘 정밀 측정기\n\n"); RE_COLE1: printf("\t알고리즘 선택\n\t1. 선택\t\t2. 삽입\t3. 거품\t4. 쉘\t5. 퀵\t6. 퀵(비재귀)\n\t7. 퀵(랜덤)\t8. 퀵(부분)\t9. 퀵(가운데)\n" "\t10. 기수\t11. 버킷\t12. 힙\t13. 병합\n\t14. 자동생성\n선택 : "); scanf_s("%d", &selected_sort); if (selected_sort < 1 || selected_sort > 14) { printf("다시 선택하세요.\n"); goto RE_COLE1; } else if (selected_sort == 14) { printf("\n\t오류가 심한 버킷알고리즘은 제외하고 생성합니다.\n\t시작하려면 아무키나 눌러주세요..."); _getch(); printf("\n"); report_auto(); } if (recalled) goto RECALLED; RE_COLE2: printf("\n\t배열 선택\n\t1. 순배열\t2. 역순배열\t3. 중복없는 임의배열\n\t4. 중복있는 임의배열\n선택 : "); scanf_s("%d", &selected_array); if (selected_array < 1 || selected_array > 4) { printf("다시 선택하세요.\n"); goto RE_COLE2; } if (selected_array > 2) { RE_SET1: printf("랜덤최소 숫자 : "); scanf_s("%d", &rand_min); printf("랜덤최고 숫자 : "); scanf_s("%d", &rand_max); if (rand_min >= rand_max || rand_min < 0) { printf("다시 맞춰라\n"); goto RE_SET1; } } if (recalled) goto RECALLED; RE_COLE3: printf("\n\t배열 크기 선택\n\t1. 10\t2. 100\t3. 1,000\t4. 10,000\t5. 100,000\n\t6. 1,000,000\t7. 직접입력\n선택 : "); scanf_s("%d", &selected_size); if (selected_size < 1 || selected_size > 7) { printf("다시 선택하세요.\n"); goto RE_COLE3; } if (selected_size == 7) { printf("횟수 입력 : "); scanf_s("%d", &size_of_array); } else size_of_array = pow(10, selected_size); if (recalled) goto RECALLED; } recalled = TRUE; { RECALLED: fast_cls(); while (TRUE) { wprintf(L"\t선택된 알고리즘: %s\n\t선택된 배 열: %s\n\t선택된 배열크기: %s\n", sort_name[selected_sort - 1], array_name[selected_array - 1], commasput(size_of_array)); if (selected_array > 2) { printf("\t숫 자 범 위: %d ~ %d", rand_min, rand_max); } printf("\n\n"); printf("\t1. 알고리즘변경\t\t2. 배열변경\t3. 크기변경"); if (selected_array > 2) { printf("\t4. 범위변경\n\t5. 실험시작"); } else printf("\t4. 실험시작"); printf("\n>> "); scanf_s("%d", &command); if (selected_array > 2 && command == 4) goto RE_SET1; else if (command == 1) goto RE_COLE1; else if (command == 2) goto RE_COLE2; else if (command == 3) goto RE_COLE3; else if (command == 4 || (selected_array > 2 && command == 5)) { RAND_MIN = rand_min; RAND_SECTION = rand_max - rand_min + 1; SIZE_OF_ARRAY = size_of_array; array_direct = (int *)malloc(sizeof(int)*size_of_array); array_reverse = (int *)malloc(sizeof(int)*size_of_array); array_random = (int *)malloc(sizeof(int)*size_of_array); array_overlap = (int *)malloc(sizeof(int)*size_of_array); fast_cls(); printf("진행횟수? "); scanf_s("%d", &command); fast_cls(); selected_array_g = selected_array; task_array = (int *)malloc(sizeof(int) * SIZE_OF_ARRAY); ld_array = (long double *)malloc(sizeof(long double) * command); for (i = 0; i < command; i++) { init_array(); if (selected_array == 1) array_selected = array_direct; else if (selected_array == 2) array_selected = array_reverse; else if (selected_array == 3) array_selected = array_random; else if (selected_array == 4) array_selected = array_overlap; switch (selected_sort) { case 1: timer_start(); sort_selection (array_selected, SIZE_OF_ARRAY); timer_finish(); break; case 2: timer_start(); sort_insertion (array_selected, SIZE_OF_ARRAY); timer_finish(); break; case 3: timer_start(); sort_bubble (array_selected, SIZE_OF_ARRAY); timer_finish(); break; case 4: timer_start(); sort_shell (array_selected, SIZE_OF_ARRAY); timer_finish(); break; case 5: timer_start(); sort_quick0 (array_selected, SIZE_OF_ARRAY); timer_finish(); break; case 6: timer_start(); sort_quick1 (array_selected, SIZE_OF_ARRAY); timer_finish(); break; case 7: timer_start(); sort_quick2 (array_selected, SIZE_OF_ARRAY); timer_finish(); break; case 8: timer_start(); sort_quick3 (array_selected, SIZE_OF_ARRAY); timer_finish(); break; case 9: timer_start(); sort_quick4 (array_selected, SIZE_OF_ARRAY); timer_finish(); break; case 10: timer_start(); radix_sort (array_selected, SIZE_OF_ARRAY); timer_finish(); break; case 11: timer_start(); sort_bucket (array_selected, SIZE_OF_ARRAY, task_array); timer_finish(); break; case 12: timer_start(); sort_heap (array_selected, SIZE_OF_ARRAY); timer_finish(); break; case 13: timer_start(); sort_merge (array_selected, SIZE_OF_ARRAY, task_array); timer_finish(); break; } ld_array[i] = timer_getns(); } free(task_array); printf("\n\n--------------실험 결과--------------\n"); sum = 0.0f; for (i = 0; i < command; i++) { printf("%.12lf ns\n", ld_array[i]); sum += ld_array[i]; } avg = sum / command; variance = 0.0f; for (i = 0; i < command; i++) { variance += pow(ld_array[i] - avg, 2); } variance /= command; printf("-------------------------------------\n"); printf("합계 : %.12lf ns\n평균 : %.12lf ns\n분산 : %.12lf\t표준편차 : %.12lf\n\n", sum, avg, variance, sqrt(variance)); getchar(); } else printf("없는 명령입니다."); } } return 0; }
void report_auto() { TCHAR appData[MAX_PATH]; FILE *fp; int algorithm, array, count, i; int size_of_array = 0; int *task_array; int *array_selected = 0; bool recalled = FALSE; int cxt = 0; long double *ld_array, sum, variance, avg; char addr[256]; RAND_MIN = 0; RAND_SECTION = 10000000 - 0 + 1; /*if (SUCCEEDED(SHGetFolderPath(NULL, CSIDL_LOCAL_APPDATA | CSIDL_FLAG_CREATE, NULL, SHGFP_TYPE_CURRENT, appData))) {*/ //sprintf_s(addr, 256, "%s%s%d%s", appData, "\\report_", (int)time(NULL), ".log"); //sprintf_s(addr, 256, "%s%s", "C:", "\\report_.log"); _wfopen_s(&fp, _T("C:\\rollrat\\report_sort.log"), _T("w+")); /*fwprintf(fp, _T("정렬 알고리즘 정밀 측정기으로부터 자동으로 생성되었음.\n\n")); fwprintf(fp, _T("[알고리즘] [배열] [횟수] 순으로 알고리즘 목록이 생성되며, 바로 밑에 합계, 평균, 분산, 표준편차가 기제됩니다. 최대 횟수는 십만번이며, ") _T("천 개 이하의 자료개수에서만 중복없는 임의배열이 적용됩니다. 랜덤최소 값은 0이며 최대값은 10000으로 설정됩니다. 횟수는 모두 10회입니다.\n\n"));*/ for (algorithm = 1; algorithm < 14; algorithm++) { if (algorithm == 11 || algorithm == 7 || algorithm == 8 || algorithm == 9) continue; // 버킷 알고리즘 사용안함 for (array = 0; array < 4; array++) { for (count = 1; count < 5; count++) { //if (count > 3 && array == 2) // break; // 중복없는 임의배열 적용안함 size_of_array = pow(10, count);//(double)((double)count / 2)); SIZE_OF_ARRAY = size_of_array; task_array = (int *)malloc(sizeof(int) * size_of_array); array_direct = (int *)malloc(sizeof(int)*size_of_array); array_reverse = (int *)malloc(sizeof(int)*size_of_array); array_random = (int *)malloc(sizeof(int)*size_of_array); array_overlap = (int *)malloc(sizeof(int)*size_of_array); #define _MAX_COUNT 30 ld_array = (long double *)malloc(sizeof(long double) * _MAX_COUNT); for (i = 0; i < _MAX_COUNT; i++) { selected_array_g = array + 1; init_array(); if (array == 0) array_selected = array_direct; else if (array == 1) array_selected = array_reverse; else if (array == 2) array_selected = array_random; else if (array == 3) array_selected = array_overlap; switch (algorithm) { case 1: timer_start(); sort_selection(array_selected, SIZE_OF_ARRAY); timer_finish(); break; case 2: timer_start(); sort_insertion(array_selected, SIZE_OF_ARRAY); timer_finish(); break; case 3: timer_start(); sort_bubble(array_selected, SIZE_OF_ARRAY); timer_finish(); break; case 4: timer_start(); sort_shell(array_selected, SIZE_OF_ARRAY); timer_finish(); break; case 5: timer_start(); sort_quick0(array_selected, SIZE_OF_ARRAY); timer_finish(); break; case 6: timer_start(); sort_quick1(array_selected, SIZE_OF_ARRAY); timer_finish(); break; case 7: timer_start(); sort_quick2(array_selected, SIZE_OF_ARRAY); timer_finish(); break; case 8: timer_start(); sort_quick3(array_selected, SIZE_OF_ARRAY); timer_finish(); break; case 9: timer_start(); sort_quick4(array_selected, SIZE_OF_ARRAY); timer_finish(); break; case 10: timer_start(); radix_sort(array_selected, SIZE_OF_ARRAY); timer_finish(); break; case 11: timer_start(); sort_bucket(array_selected, SIZE_OF_ARRAY, task_array); timer_finish(); break; case 12: timer_start(); sort_heap(array_selected, SIZE_OF_ARRAY); timer_finish(); break; case 13: timer_start(); sort_merge(array_selected, SIZE_OF_ARRAY, task_array); timer_finish(); break; } ld_array[i] = timer_getns(); } sum = 0.0f; for (i = 0; i < _MAX_COUNT; i++) { sum += ld_array[i]; } avg = sum / _MAX_COUNT; variance = 0.0f; for (i = 0; i < _MAX_COUNT; i++) { variance += pow(ld_array[i] - avg, 2); } variance /= _MAX_COUNT; fwprintf(fp, _T("[%s] [%s] [%s]\n"), sort_name[algorithm - 1], array_name[array], commasput(size_of_array)); fwprintf(fp, _T("합계 : %.12lf ns\n평균 : %.12lf ns\n분산 : %.12lf\n표준편차 : %.12lf\n\n"), sum, avg, variance, sqrt(variance)); printf("[%d/216]완료됨\n", ++cxt); // 오래걸리고 많이하면 과부하걸려서 걍 없앰 /*free(ld_array); free(array_overlap); free(array_random); free(array_reverse); free(array_direct); free(task_array);*/ } } } fwprintf(fp, _T("end")); fclose(fp); //} printf("C:\\rollrat\\report_sort.log에 성공적으로 생성하였습니다."); }