The following is the main part of selection sort pseudocode: for i := 0 to n-2 // large loop find a[k], where a[k] = min {a[i], a[i+1],...,a[n-1]} if k # i then swap a[i] with a[k] end for Consider the list of ten integers below: 7, 5, 11, 12, 3, 10, 2, 4, 8, 6 What is the list after the FIRST TWO iterations of the large loop in a selection sort? (sorting from smallest to largest).
4 phương ánCó giải thích
Các phương án trả lời
Chọn một phương ánLời giảiChọn phương án để mở
Đáp án đúng: "2, 3, 11, 12, 5, 10, 7, 4, 8, 6". Selection sort mỗi lượt tìm phần tử nhỏ nhất của đoạn chưa sắp rồi hoán vị nó với phần tử đứng đầu đoạn, chứ không dịch cả dãy. Lượt i = 0: nhỏ nhất trong cả mười số là 2, nó đổi chỗ với 7 cho 2, 5, 11, 12, 3, 10, 7, 4, 8, 6. Lượt i = 1: nhỏ nhất của đoạn còn lại là 3, nó đổi chỗ với 5 cho 2, 3, 11, 12, 5, 10, 7, 4, 8, 6. "2, 3, 7, 5, 11, 12, 10, 4, 8, 6" sai vì nó dồn các số lớn sang phải một ô theo kiểu insertion sort, trong khi mã giả chỉ swap đúng hai phần tử nên 11 và 12 phải đứng nguyên chỗ. "2, 3, 11, 12, 5, 10, 7, 4, 6, 8" sai vì nó đổi thêm 8 với 6 ở cuối dãy, còn hai lượt đầu chưa hề chạm tới đuôi. "2, 3, 11, 12, 10, 5, 7, 4, 8, 6" sai vì nó đưa 5 vào ô đang giữ 10, trong khi hoán vị phải đặt 5 vào đúng ô mà 3 vừa rời đi.
Cùng phần 6
Select the most correct statement about the complexity of bubble sortCó lời giảiCâu hỏiThe following is the main part of insertion sort pseudocode:
for i := 1 to n-1
insert a[i] into sorted sublist {a[0],...,a[i-1]}
so that the sortedness is still kept.
end for
Select the most correct solution:
Consider the list of ten integers below:
16, 5, 2, 11, 10, 8, 12, 3, 8, 6
What is the list after the FIRST TWO steps of insertion sort? (sorting from smallest to largest).Có lời giảiCâu hỏiPartitioning is a technique used in quicksort. The following is a partition pseudocode for sorting from smallest to largest:
partition the array a from index low to index up
x := a[low], i=low, j=up
do
increase i and stop at i, where a[i]>x
decrease j and stop at j, where a[j]<=x
if(i<j) swap a[i] with a[j]
while(i<j)
swap a[low] with a[j]
Consider the list of eight integers (n=8) below:
15, 13, 18, 19, 17, 12, 16, 14
What is the list after it is partitioned with low = 0 and up = n-1?Có lời giảiCâu hỏiCả 20 câu của chủ đề20 câu
Xuất hiện trong 1 bộ đề
Đi tiếp từ đây