Advertisement
Guest User

Untitled

a guest
Nov 17th, 2019
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.73 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include "sort.h"
  4. #include "vector_utils.h"
  5.  
  6. int bubble_sort_asc(int tab[], int size)
  7. {
  8. if (size <= 0)
  9. return 1;
  10. int tmp = 0;
  11. int i = 0;
  12. int j = 0;
  13. if (size == 1)
  14. {
  15. printf("%d", tab[0]);
  16. return 0;
  17. }
  18. int check = 0;
  19. for (i = 0 ; i < size - 1; i++)
  20. {
  21. for (j = 0 ; j < size - 1; j++)
  22. {
  23. if (tab[j] > tab[j + 1])
  24. {
  25. tmp = tab[j];
  26. tab[j] = tab[j + 1];
  27. tab[j + 1] = tmp;
  28. check++;
  29. }
  30. }
  31. if (check == 0)
  32. {
  33. display_vector(tab, size);
  34. break;
  35. }
  36. if (check != 0)
  37. {
  38. display_vector(tab, size);
  39. printf("\n");
  40. }
  41. check = 0;
  42. //display_vector(tab, size);
  43.  
  44. }
  45.  
  46. return 0;
  47. }
  48.  
  49.  
  50. int bubble_sort_desc(int tab[], int size)
  51. {
  52. if (size <= 0)
  53. return 1;
  54. int tmp = 0;
  55. int i = 0;
  56. int j = 0;
  57. if (size == 1)
  58. {
  59. printf("%d", tab[0]);
  60. return 0;
  61. }
  62. int check = 0;
  63. for (i = 0 ; i < size - 1; i++)
  64. {
  65. for (j = 0 ; j < size - 1; j++)
  66. {
  67. if (tab[j] < tab[j + 1])
  68. {
  69. tmp = tab[j];
  70. tab[j] = tab[j + 1];
  71. tab[j + 1] = tmp;
  72. check++;
  73. }
  74. }
  75. if (check == 0)
  76. {
  77. display_vector(tab, size);
  78. break;
  79. }
  80. if (check != 0)
  81. {
  82. display_vector(tab, size);
  83. printf("\n");
  84. }
  85.  
  86. check = 0;
  87. // display_vector(tab, size);
  88.  
  89. }
  90.  
  91. return 0;
  92. }
  93.  
  94.  
  95. int bubble_sort(int tab[], int size, enum direction dir)
  96. {
  97. if (dir == ASCENDING)
  98. {
  99. return bubble_sort_asc(tab, size);
  100. }
  101.  
  102. else if (dir == DESCENDING)
  103. {
  104. return bubble_sort_desc(tab, size);
  105. }
  106.  
  107. return 1;
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement