Computer Science, asked by Chakshu290703, 1 year ago

How many times a bubble sort of an array of 8 elements execute?
a. 7!
b. 7+6+5+4...
Pls explain briefly too

Answers

Answered by UnknownDude
1
It will be B
In the first iteration of the loop, the array will start at the first element and each element will be checked. So first is 7 times.
Then, the loop will leave the first element and start with the second element. So, 6 elements will be checked. 
This goes on till the last element.
So no. of iterations is 7+6+5+....
Answered by siddhartharao77
4
Short note on bubble sort:

(1) At first, we know that sorting takes an unordered collection and makes it an ordered one.

(2) In bubble sort, each pair of an adjacent element is compared and elements are swapped if they are not in order.

(3) Bubble sort is useful only for some data sets.

(4) Its worst-case complexity is O(n^2) and best case complexity is O(n).


Now,

Given that an array consists of 8 elements. Let it be:

2 3 4 1 9 6 7 8 ---------- Size of the array = 8.

Here if u sort 4 elements, automatically 5th element will get its position.

1st Iteration:

2 3 and compared.

2 > 3  ----------- False(The position remains same)


2nd Iteration:

3 > 4   ------------- False(The position remains same)


3rd Iteration:

4 > 1   -------------- True(The position gets interchanged).

4 swaps to right and 1 swap left.

Now,

It will be 2 3 1 4 9 6 7 8 

Iteration (4):

4 > 9   ----------- False(The position remains same)

Iteration (5):

9 > 6   ------------ True(9 is swapped to right and 6 is swapped to left).


It looks like 2 3 1 4 6 9 7 8 


Iteration (6):

9 > 7   ---------- True(9 is swapped to right and 7 is swapped to left)

It looks like 2 3 1 4 6 7 9 8 


Iteration (7): 

9 > 8   -------------- True(9 is swapped to right and 8 is swapped to left)

It looks like 2 3 1 4 6 7 8 9 


Iteration (8):  ---- From Beginning again.

2 > 3   ----------- False(The position remains same)


Iteration(9):

3 > 1   ----------- True(3 is swapped to right and 1 is swapped to left)

It looks like 2 1 3 4 6 7 8 9.

Iteration (10):

2 > 1  ----------- True(2 is swapped to right and 1 is swapped to left)

It looks like 1 2 3 4 6 7 8 9.


Now,

We can see that the list is completely sorted now. 

The final outcome is 1 2 3 4 6 7 8 9.


Your answer is 7 + 6 + 5 + 4 + 3 + 2 + 1.

Because We are going to compare i and i + 1.


Hope this helps!

siddhartharao77: So, u r saying that i have copied the answer?
siddhartharao77: Then?
siddhartharao77: I am also answered
siddhartharao77: to his other question.
Rajdeep11111: :)
Similar questions