forked from thantrieu/LearnC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bai4.3.c
67 lines (57 loc) · 1.01 KB
/
bai4.3.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
#include <stdio.h>
void nhap(int *arr, int *n) {
while(*n <= 0) {
printf("Nhap n > 0: ");
scanf("%d", n);
}
int i;
for(i = 0; i < *n; i++) {
printf("arr[%d] = ", i);
scanf("%d", &arr[i]);
}
}
void sapXepTang(int *arr, int n) {
int i, j;
for(i = 0; i < n - 1; i++) {
for(j = n - 1; j > i; j--) {
if(arr[j] < arr[j - 1]) {
int tmp = arr[j];
arr[j] = arr[j - 1];
arr[j - 1] = tmp;
}
}
}
}
void sapXepGiam(int *arr, int n) {
int i, j;
for(i = 0; i < n - 1; i++) {
for(j = n - 1; j > i; j--) {
if(arr[j] > arr[j - 1]) {
int tmp = arr[j];
arr[j] = arr[j - 1];
arr[j - 1] = tmp;
}
}
}
}
void hienThi(int *arr, int n) {
int i;
for(i = 0; i < n; i++) {
printf("%6d", arr[i]);
}
printf("\n");
}
int main() {
int n = 0;
int arr[100];
nhap(arr, &n);
printf("\nTruoc sap xep: \n");
hienThi(arr, n);
sapXepTang(arr, n);
printf("\nSau sap xep tang: \n");
hienThi(arr, n);
sapXepGiam(arr, n);
printf("\nSau sap xep giam: \n");
hienThi(arr, n);
return 0;
}