-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbubble-sort.c
75 lines (60 loc) · 1.88 KB
/
bubble-sort.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include<stdio.h>
void receiveInformationInVector(int *pVector, int vectorSize);
int menuGrowingOrDecreasing();
void growingAndDecreasingBubble(int *pVector, int vectorSize, int option);
void printVector(int *pVector, int vectorSize);
int main(){
int vectorSize, option;
printf("============= Bubble Sort =============\n\n");
printf("Inform the vector size: ");
scanf("%i", &vectorSize);
int vector[vectorSize];
option = menuGrowingOrDecreasing();
receiveInformationInVector(vector, vectorSize);
growingAndDecreasingBubble(vector, vectorSize, option);
printVector(vector, vectorSize);
return 0;
}
void receiveInformationInVector(int *pVector, int vectorSize){
int i;
printf("\n");
for (i = 0; i < vectorSize; i++){
printf("number %i = ", i + 1);
scanf("%i", (pVector + i));
}
}
int menuGrowingOrDecreasing(){
int option;
do{
printf("\nType it:\n[1] Growing\n[2] Decreasing\n\nOption: [ ]\b\b");
scanf("%i", &option);
if(option != 1 && option != 2){
printf("Invalid option, retype\n");
system("pause");
}
} while (option != 1 && option != 2);
return option;
}
void growingAndDecreasingBubble(int *pVector, int vectorSize, int option){
int i, proceed, end = vectorSize, subsidiary;
do{
proceed = 0;
for (i = 0; i < end - 1; i++){
// growing
if(*(pVector + i) > *(pVector + i + 1) && option == 1){
subsidiary = *(pVector + i);
*(pVector + i) = *(pVector + i + 1);
*(pVector + i + 1) = subsidiary;
proceed = i;
}
}
end--;
} while (proceed != 0);
}
void printVector(int *pVector, int vectorSize){
int i;
printf("\nOrdered vector: \n");
for (i = 0; i < vectorSize; i++){
printf("%i ", *(pVector + i));
}
}