Mảng và các thao tác cơ bản
Mảng là nhóm các mục dữ liệu liên quan lưu trữ dưới một tên duy nhất. Ví dụ: int student[30];
Các thao tác: Tìm kiếm (Searching), Sắp xếp (Sorting), Duyệt (Traversing), Chèn (Inserting), Xóa (Deleting).
Logic sắp xếp tăng dần (kỹ thuật hoán đổi)
for (i = 0; i < 10 - 1; i++) {
for (j = i + 1; j < 10; j++) {
if (element[i] > element[j]) {
temp = element[i];
element[i] = element[j];
element[j] = temp;
}
}
}
Chương trình hoàn chỉnh
#include <stdio.h>
int main() {
int element[10], i, j, temp;
printf("Nhap 10 so nguyen: ");
for (i = 0; i < 10; i++)
scanf("%d", &element[i]);
for (i = 0; i < 9; i++) {
for (j = i + 1; j < 10; j++) {
if (element[i] > element[j]) {
temp = element[i];
element[i] = element[j];
element[j] = temp;
}
}
}
printf("Cac phan tu sau khi sap xep tang dan:\n");
for (i = 0; i < 10; i++)
printf("%d\n", element[i]);
return 0;
}
Kết quả
Nhap 10 so nguyen: 12 23 34 1 22 34 56 45 33 78 Cac phan tu sau khi sap xep tang dan: 1 12 22 23 33 34 34 45 56 78