01/10/2018, 10:50
Giải thích vòng lặp for lồng nhau?
Ai giải thích kĩ cho em hiểu với ạ, em mới học lập trình nên tư duy còn kém quá, hi
void sap_xep_theo_thu_tu_giam_dan_diem_so(Doibong ds[], int n)
{
for(int i = 0;i < n-1;i++)
{
for(int j = i+1;j < n;j++)
{
if(ds[i].tongdiem < ds[j].tongdiem)
{
Doibong tam = ds[i];
ds[i] = ds[j];
ds[j] = tam;
}
}
}
}
Bài liên quan
Code này là selection sort.
Link:
GeeksforGeeks – 31 Jan 14
Selection Sort - GeeksforGeeks
The selection sort algorithm sorts an array by repeatedly finding the minimum element (considering ascending order) from unsorted part and putting it at the beginning.… Read More »
một topic khác tương tự:
Selection phiên bản lỗi
Tớ k hiểu đoạn kichthuoc - 1 ấy nghĩa là gì
i = kichthuoc - 1 -> j = i + 1 = kichthuoc
-> vòngfor (j = i+1; j < kichthuoc; j++)
sẽ không được thực hiện ->i < kichthuoc - 1
Phải lùi lại 1 slot vì vòng lặp trong sẽ bắt đầu với phần tử liền sau nó.
Cảm ơn các bác, em hiểu rồi ạ
You can refer this resource for detailed explanation on “nested for loop”.