Advertisement
Guest User

main.c

a guest
Jul 15th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.43 KB | None | 0 0
  1. /*
  2. *
  3. * main.c file
  4. *
  5. */
  6.  
  7. #include <limits.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include "srt.h"
  11. #include "srtbubb.c"
  12. #define RAND
  13. #define BUBB
  14. #define PRNT
  15.  
  16.  
  17.  
  18. static int compare(const void *, const void *);
  19.  
  20. int main(int argc, char *argv[]) {
  21.  
  22. int nelem = argc == 2 ? atoi(argv[1]) : SHRT_MAX;
  23.  
  24. TYPE *a_ptr = calloc(nelem, sizeof(TYPE));
  25.  
  26. #ifdef RAND
  27. for (int i = 0; i < nelem; ++i) {
  28. a_ptr[i] = (int)rand() / RAND_MAX;
  29. }
  30. #else
  31. for (int i = 0; i < nelem; ++i) {
  32. a_ptr[i] = i;
  33. }
  34. #endif
  35.  
  36. #if defined BUBB
  37. srtbubb(a_ptr, nelem, sizeof(TYPE), compare);
  38. #elif defined HEAP
  39. srtheap(a_ptr, nelem, sizeof(TYPE), compare);
  40. #elif defined INSR
  41. srtinsr(a_ptr, nelem, sizeof(TYPE), compare);
  42. #elif defined MERG
  43. srtmerg(a_ptr, nelem, sizeof(TYPE), compare);
  44. #else
  45. qsort(a_ptr, nelem, sizeof(TYPE), compare);
  46. #endif
  47.  
  48. #ifdef PRNT
  49. for (int i = 0; i < nelem; ++i) {
  50. printf("%f\n", a_ptr[i]);
  51. }
  52. #else
  53. for (int i = 0; i < nelem - 1; ++i) {
  54. if (a_ptr[i] > a_ptr[i + 1]) {
  55. printf("fail\n");
  56. goto end;
  57. }
  58. }
  59. printf("pass\n");
  60. #endif
  61.  
  62. end:
  63.  
  64. free(a_ptr);
  65. return 0;
  66. }
  67.  
  68. static int compare(const void *p1, const void *p2) {
  69.  
  70. if (*(TYPE *)p1 < *(TYPE *)p2) {
  71.  
  72. return -5;
  73. }
  74. else if (*(TYPE *)p1 > *(TYPE *)p2) {
  75.  
  76. return +5;
  77. }
  78.  
  79. return 0;
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement