0% found this document useful (0 votes)
154 views

Assignment

The document provides a step-by-step explanation of how the bubble sort algorithm works to sort an array of numbers in ascending order. It shows the algorithm being applied to the array [23, 55, 43, 78, 12, 33, 42, 2]. Over multiple passes, adjacent elements are compared and swapped if out of order, until the array is fully sorted from lowest to highest as [2, 12, 23, 33, 42, 43, 55, 78].

Uploaded by

Gautam kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
154 views

Assignment

The document provides a step-by-step explanation of how the bubble sort algorithm works to sort an array of numbers in ascending order. It shows the algorithm being applied to the array [23, 55, 43, 78, 12, 33, 42, 2]. Over multiple passes, adjacent elements are compared and swapped if out of order, until the array is fully sorted from lowest to highest as [2, 12, 23, 33, 42, 43, 55, 78].

Uploaded by

Gautam kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 7

Q1. Consider number 23,55,43,78,12,33,42,2 .

Apply logic toarrange them in ascending order showcase step by step for the same ?

Ans. Bubble shot approch to arrange element in ascending order.

--complexity of bubble shot is O(n)2 .

--simple and short code is preferred.

WORKING OF BUBBLE SHOT ALGORITHM.

Now , let's see the working of bubble shot algorithm.

23 55 43 78 12 33 42 2

We are taking a short and

accurate array, as we know the complexity of bubble sort is O(n2) 2

Let the elements of array are -

23 55 43 78 12 33 42 2

FIRST PASS--

Sorting will start from the initial two elements. Let compare them to check which is greater

23 55 43 78 12 33 42 2

Here, 55 is greater than 23 (55 > 23), so it is already sorted. Now, compare 55 with 43.

23 55 43 78 12 33 42 2

Here, 43 is smaller than 55. So, swapping is required. After swapping new array will look like -

23 43 55 78 12 33 42 2

now, we compare 55 and 78.


23 43 55 78 12 33 42 2

Here, 78 is greater than 55. So, there is no swapping required as they are already sorted.

Now, the comparison will be in between 78 and 12.

23 43 55 78 12 33 42 2

Here, 12 is smaller than 78 that are not sorted. So, swapping is required

now, we compare 78 and 33.

23 43 55 12 78 33 42 2

Here, 78 is greater than 33 . so ,swapping is required.

now, we compare 78 and 42.

23 43 55 12 33 78 42 2

Here,78 is greater than 42. so, swapping is required.

now, we compare 78 and 2.

23 43 55 12 33 42 78 2

Here 78 is greater than 2 . so , swapping is required.Now, we reach at the end of the array.

After first pass, the array will be

23 43 55 12 33 42 2 78

SECOND PASS--

23 43 55 12 33 42 2 78
23 43 55 12 33 42 2 78

23 43 55 12 33 42 2 78

Here, 12 is smaller than 55. So, swapping is required. After swapping, the array will be--

now , we compare 55 and 33.

23 43 12 55 33 42 2 78

Here, 33 is smaller than 55. So, swapping is required. After swapping, the array will be--

now , we compare 55 and 42.

23 43 12 33 55 42 2 78

Here, 42 is smaller than 55. So, swapping is required. After swapping, the array will be--

now, we compare 42 and 2.

23 43 12 33 42 55 2 78

Here, 2 is smaller than 55. So, swapping is required. After swapping, the array will be--

now, we compare 55 and 78.

23 43 12 33 42 2 55 78

Here, 78 is greater than 55 . so , there will we no swapping required.

23 43 12 33 42 2 55 78

Now, move to the third iteration.

THIRD PASS---

same process will be follow.


23 43 12 33 42 2 55 78

23 43 12 33 42 2 55 78

Here, 12 is smaller than 43 . so, swapping is required.

now, we compare 43 and 33.

23 12 43 33 42 2 55 78

Here, 33 is smaller than 43 . so , swapping is required.

now, we compare 43 and 42.

23 12 33 43 42 2 55 78

Here , 42 is smaller than 43 . so, swapping is required.

now, we compare 43 and 2.

23 12 33 42 43 2 55 78

Here 2 is smaller than 43. so ,swapping is requirred .

23 12 33 42 2 43 55 78

Hence, there is no swapping required.

23 12 33 42 2 43 55 78

Hence, there is no swapping required

After third pass. the array will be--

23 12 33 42 2 43 55 78
FPURTH PASS--

same process will be follow--

23 12 33 42 2 43 55 78

Here , 12 is smaller than 23 . so swapping is required.

now, we compare 23 and 33.

12 23 33 42 2 43 55 78

Hence, there is no swapping required.

now , we compare 33 and 42.

12 23 33 42 2 43 55 78

Hence, there is no swapping required.

now, we compare 42 and 2.

12 23 33 42 2 43 55 78

Here , 2 is smaller than 42. so, swapping is required.

12 23 33 2 42 43 55 78

12 23 33 2 42 43 55 78

12 23 33 2 42 43 55 78

Hence, there is no swapping required,

the array will be---

12 23 33 2 42 43 55 78
FIFTH PASS--

same process wil be follow-

12 23 33 2 42 43 55 78

12 23 33 2 42 43 55 78

12 23 33 2 42 43 55 78

Here , 2 is smaller than 33 . so, swapping is required.

12 23 2 33 42 43 55 78

12 23 2 33 42 43 55 78

12 23 2 33 42 43 55 78

Hence, there is no swapping required.

SIXTH PASS--

same process will be follow.

12 23 2 33 42 43 55 78

12 23 2 33 42 43 55 78

Here, 2 is smaller than 23 . so, swapping is required .

12 2 23 33 42 43 55 78

12 2 23 33 42 43 55 78

12 2 23 33 42 43 55 78

12 2 23 33 42 43 55 78

12 2 23 33 42 43 55 78
Hence, there is no swapping required.

SEVENTH PASS---

same process will be follow.

12 2 23 33 42 43 55 78

Here, 2 is smaller than 12 . so, swapping is required.

2 12 23 33 42 43 55 78

2 12 23 33 42 43 55 78

2 12 23 33 42 43 55 78

2 12 23 33 42 43 55 78

2 12 23 33 42 43 55 78

2 12 23 33 42 43 55 78

Hence, there is no swapping required.so the array is completely sorted.

THANK YOU.

You might also like