main.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include "../log.c"
  5. #define LSIZE 100000
  6. #define LSTEP 10000
  7. static int *mk_array(int len) {
  8. int *arr = (int *)malloc(sizeof(int) * len), i;
  9. srand(time(NULL));
  10. for (i = 0; i < len; i++)
  11. arr[i] = (rand() % (len * 10));
  12. return arr;
  13. }
  14. static void *cp_array(int *arr, int len) {
  15. int *narr = (int *)malloc(sizeof(int) * len), i;
  16. for (i = 0; i < len; i++)
  17. narr[i] = arr[i];
  18. return narr;
  19. }
  20. static void bubble_sort(int *arr, int len) {
  21. int c, d, swp;
  22. for (c = 0; c < (len - 1); c++) {
  23. for (d = 0; d < (len - c - 1); d++) {
  24. if (arr[d] > arr[d+1]) {
  25. swp = arr[d];
  26. arr[d] = arr[d+1];
  27. arr[d+1] = swp;
  28. }
  29. }
  30. }
  31. }
  32. static void merge(int *arr, int min, int mid, int max) {
  33. int l1, l2, i,
  34. *buf = (int *)malloc(sizeof(int) * (max + 1));
  35. for (l1 = min, l2 = mid + 1, i = min; l1 <= mid && l2 <= max; i++)
  36. if (arr[l1] <= arr[l2]) buf[i] = arr[l1++];
  37. else buf[i] = arr[l2++];
  38. while (l1 <= mid)
  39. buf[i++] = arr[l1++];
  40. while (l2 <= max)
  41. buf[i++] = arr[l2++];
  42. for (i = min; i <= max; i++)
  43. arr[i] = buf[i];
  44. free(buf);
  45. }
  46. static void merge_sort(int *arr, int min, int max) {
  47. int mid;
  48. if (min < max) {
  49. mid = (min + max) / 2;
  50. merge_sort(arr, min, mid);
  51. merge_sort(arr, mid+1, max);
  52. merge(arr, min, mid, max);
  53. }
  54. }
  55. int main(int argc, char const *argv[]) {
  56. /**
  57. * set first argument to
  58. * a+: Append results to current log. Typically if you'd
  59. * make changes to the programs algorithms for each run, and
  60. * each execution should be compared.
  61. * w+: Each execution does several logging, and create a new log for
  62. * this for each run.
  63. */
  64. log_t *lt = log_init("actual", "w+", "Elements to sort", "Processing time", (logfunc_t)(gettime)),
  65. *la = log_init("average", "w+", "Elements to sort", "Avg. processing time", (logfunc_t)(gettime));
  66. logent_t *le_bt = logent_init(lt, "bubble sort total", "bubble"),
  67. *le_ba = logent_init(la, "bubble sort avg.", "bubble");
  68. logent_t *le_mt = logent_init(lt, "merge sort total", "merge"),
  69. *le_ma = logent_init(la, "merge sort avg.", "merge");
  70. int k = LSTEP, *arr, *bsort_arr_t, *bsort_arr_a, *msort_arr_t, *msort_arr_a;
  71. while (k <= LSIZE) {
  72. arr = mk_array(k);
  73. bsort_arr_t = cp_array(arr, k);
  74. bsort_arr_a = cp_array(arr, k);
  75. msort_arr_t = cp_array(arr, k);
  76. msort_arr_a = cp_array(arr, k);
  77. logent_start(le_bt);
  78. bubble_sort(bsort_arr_t, k);
  79. logent_end(le_bt, k, 1);
  80. logent_start(le_ba);
  81. bubble_sort(bsort_arr_a, k);
  82. logent_end(le_ba, k, k);
  83. logent_start(le_mt);
  84. merge_sort(msort_arr_t, 0, k-1);
  85. logent_end(le_mt, k, 1);
  86. logent_start(le_ma);
  87. merge_sort(msort_arr_a, 0, k-1);
  88. logent_end(le_ma, k, k);
  89. k += LSTEP;
  90. }
  91. // Closes log-entries
  92. log_destroy(lt);
  93. log_destroy(la);
  94. /*
  95. Now missing: (will probably be added)
  96. logfile_avg( log_for );
  97. logfile_avg( log_while );
  98. logfile_destroy( log_for );
  99. logfile_destroy( log_while );
  100. */
  101. return 0;
  102. }