164k views
1 vote
Consider the following algorithm:

1 Bubble-sort(a)
2 for i = () down to 1
3 for j = 1 to i-1
4 if a[j]>a[j+1]
5 swap(a[j],a[j+1]);
6 end if

What is its basic operation (write the line number of code which would define the execution time of the code)?

User NKol
by
7.6k points

1 Answer

5 votes

Final answer:

The basic operation that defines the execution time of the Bubble-Sort algorithm is the comparison of adjacent elements, signified by line 4 in the code.

Step-by-step explanation:

The basic operation of the given Bubble-Sort algorithm, which would define the execution time of the code, is the comparison operation within the nested loop. Specifically, this occurs at line 4 (if a[j]>a[j+1]), where each pair of adjacent elements is compared and potentially swapped if they are out of order. This comparison happens most frequently during the execution of the algorithm, making it the fundamental action that affects the performance and time complexity of the sorting process.

User Ian Varley
by
8.6k points