# Bubble sort

Oct 20th, 2021
719
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. #include <stdio.h>
2. #define SIZE 10
3.
4. void bubble(int work[], const int size, int (*compare)(int a, int b));
5. int ascending(int a, int b);
6. int descending(int a, int b);
7.
8. int main(){
9.
10.     int order;
11.     int counter;
12.
13.     int a[SIZE] = {3,5,14,7,8,6,11,13,10,9};
14.
15.     printf("Para ordem crescente 1,\n"
16.            "Para ordem decrescente 2: ");
17.
18.     scanf( "%d", &order);
19.
20.     printf("\nItens de dados na ordem original\n");
21.     for(counter = 0; counter < SIZE; counter++){
22.         printf( "%5d", a[counter]);
23.     }
24.
25.     if(order == 1){
26.         bubble(a, SIZE, ascending);
27.         printf("\n\nItens de dados na ordem crescente\n");
28.     }
29.     else{
30.         bubble(a, SIZE, descending);
31.         printf("\n\nItens de dados na ordem decrescente\n");
32.     }
33.
34.     for(counter = 0; counter < SIZE; counter++){
35.         printf( "%5d", a[counter]);
36.     }
37.
38.     printf("\n");
39.
40.     return 0;
41. }
42.
43.
44.
45. void bubble(int work[], const int size, int (*compare)(int a, int b)){
46.
47.     int pass;
48.     int counter;
49.
50.     void invert(int *element1Ptr, int *element2Ptr);
51.
52.     for(pass = 0; pass < size; pass++){
53.
54.         for(counter = 0; counter < (size - 1); counter++){
55.             if((*compare)(work[counter], work[counter + 1])){
56.                 invert(&work[counter], &work[counter +1]);
57.             }
58.         }
59.     }
60.
61.
62. }//fim bubble
63.
64.
65.
66. void invert(int *element1Ptr, int *element2Ptr){
67.     int upkeep;
68.
69.     upkeep = *element1Ptr;
70.     *element1Ptr = *element2Ptr;
71.     *element2Ptr = upkeep;
72.
73. }//fim invert
74.
75.
76.
77. int ascending(int a, int b){
78.     return a > b;
79. }//fim crescente
80.
81.
82.
83. int descending(int a, int b){
84.     return a < b;
85. }//fim decrescente
86.
87.
88.
RAW Paste Data