Partitioning 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?
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: "12, 13, 14, 15, 17, 19, 16, 18". Chốt là x bằng a[low] tức 15, i bắt đầu ở 0, j ở 7. Vòng một: i chạy lên và dừng ở 18 vì 18 lớn hơn 15, j dừng ngay ở 14 vì 14 không lớn hơn 15, đổi chỗ hai số đó được 15, 13, 14, 19, 17, 12, 16, 18. Vòng hai: i dừng ở 19, j lùi qua 18 và 16 rồi dừng ở 12, đổi chỗ được 15, 13, 14, 12, 17, 19, 16, 18. Vòng ba: i dừng ở 17, j lùi qua 19 và 17 rồi về 12, lúc này i đã vượt j nên không đổi chỗ và vòng lặp dừng; bước cuối đổi a[low] là 15 với a[j] là 12. "12, 13, 15, 14, 17, 19, 16, 18" để 15 trước 14, nghĩa là chốt không về đúng vị trí phân hoạch của nó. "12, 13, 14, 15, 16, 17, 19, 18" đã sắp lại cả phần bên phải chốt. "12, 13, 14, 15, 17, 16, 18, 19" cũng vậy, trong khi một lượt phân hoạch chỉ bảo đảm bên trái chốt không lớn hơn chốt và bên phải không nhỏ hơn, chứ không sắp xếp nội bộ hai phần.
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ỏiThe following is the main part of bubble sort pseudocode:
do
swapped := false
for i := 0 to n-2
if a[i] > a[i+1] then
swap(a[i],a[i+1])
swapped := true
end if
end for
while swapped
Consider the list of ten integers below:
5, 3, 9, 10, 1, 8, 0, 2, 6, 4
What is the list after the FIRST iteration (for i = 0 to n-2) in a bubble sort? (sorting from smallest to largest).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