6. Use the bubble sort to put 5,1,4,2,8 into increasing order.
First Pass:
-----------------------
( 5 1 4 2 8 ) ( 1 5 4 2 8 ), Here, algorithm compares the first two elements, and swaps them since 5 > 1
( 1 5 4 2 8 ) ( 1 4 5 2 8 ), It then compares the second and third items and swaps them since 5 > 4
( 1 4 5 2 8 ) ( 1 4 2 5 8 ), Swap since 5 > 2
( 1 4 2 5 8 ) ( 1 4 2 5 8 ), Now, since these elements are already in order (8 > 5), algorithm does not swap them.
The algorithm has reached the end of the list of numbers and the largest number, 8, has bubbled to the top. It now starts again.
Second Pass:
( 1 4 2 5 8 ) ( 1 4 2 5 8 ), no swap needed
( 1 4 2 5 8 ) ( 1 2 4 5 8 ), Swap since 4 > 2
( 1 2 4 5 8 ) ( 1 2 4 5 8 ), no swap needed
( 1 2 4 5 8 ) ( 1 2 4 5 8 ), no swap needed
Now, the array is already sorted, but our algorithm does not know if it is completed. The algorithm needs one whole pass without any swap to know it is sorted.
Third Pass:
( 1 2 4 5 8 ) ( 1 2 4 5 8 )
( 1 2 4 5 8 ) ( 1 2 4 5 8 )
( 1 2 4 5 8 ) ( 1 2 4 5 8 )
( 1 2 4 5 8 ) ( 1 2 4 5 8 )
Finally, the array is sorted, and the algorithm can terminate.
بعد شوي بنزل الحل لكن أرجو من الجميع معرفة وفهم طريقة الحل متمني لكم دوام التوفيق
1. List all the steps used to search for 10 in the sequence 1, 3, 4, 5, 6, 8, 9, 11 using
a. a linear search
Start at the beginning of the list and compare 10 successively with 1, 3, 4, 5, 6, 8, 9, 11.
When we arrive at the end of the list and have not found 10, we conclude that 10 is not in the list.
b. a binary search
(6)
Begin the search on the entire list.
Set i = 1 and j = n = 8 and m = 4.
Compare 10 to the mth element of the list.
Since 10 > 5, restrict the search to the second half of the list.
(7)
Set with i = 5 and j = 8 and m = 6.
Compare 10 to the mth element of the list.
Since 10 > 8, restrict the search to the second half of the second half of the list.
(8)
Set i = 7 and j = 8 and
Set m = 7.
Compare 10 with the mth element of the list.
Since 10 > 9, restrict the search to the second half of the second half of the second half of the list.
(9)
Set i = 8 and j = 8 and m = 8.
Compare 10 with the mth element of the list.
Since 10 < 11, exit the loop and conclude that 10 is not on the list.