Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- #include "sort.h"
- #include "vector_utils.h"
- int bubble_sort_asc(int tab[], int size)
- {
- if (size <= 0)
- return 1;
- int tmp = 0;
- int i = 0;
- int j = 0;
- if (size == 1)
- {
- printf("%d", tab[0]);
- return 0;
- }
- int check = 0;
- for (i = 0 ; i < size - 1; i++)
- {
- for (j = 0 ; j < size - 1; j++)
- {
- if (tab[j] > tab[j + 1])
- {
- tmp = tab[j];
- tab[j] = tab[j + 1];
- tab[j + 1] = tmp;
- check++;
- }
- }
- if (check == 0)
- {
- display_vector(tab, size);
- break;
- }
- if (check != 0)
- {
- display_vector(tab, size);
- printf("\n");
- }
- check = 0;
- //display_vector(tab, size);
- }
- return 0;
- }
- int bubble_sort_desc(int tab[], int size)
- {
- if (size <= 0)
- return 1;
- int tmp = 0;
- int i = 0;
- int j = 0;
- if (size == 1)
- {
- printf("%d", tab[0]);
- return 0;
- }
- int check = 0;
- for (i = 0 ; i < size - 1; i++)
- {
- for (j = 0 ; j < size - 1; j++)
- {
- if (tab[j] < tab[j + 1])
- {
- tmp = tab[j];
- tab[j] = tab[j + 1];
- tab[j + 1] = tmp;
- check++;
- }
- }
- if (check == 0)
- {
- display_vector(tab, size);
- break;
- }
- if (check != 0)
- {
- display_vector(tab, size);
- printf("\n");
- }
- check = 0;
- // display_vector(tab, size);
- }
- return 0;
- }
- int bubble_sort(int tab[], int size, enum direction dir)
- {
- if (dir == ASCENDING)
- {
- return bubble_sort_asc(tab, size);
- }
- else if (dir == DESCENDING)
- {
- return bubble_sort_desc(tab, size);
- }
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement