In: Computer Science
Given the following array
[17, 15,21,208,16,122,212,53,119,43]
Apply bubble sort algorithm and show the status of the array after
each pass. Also calculate how many comparisons you will be required
to pass
The array is given as,
17 , 15, 21, 208,16,122,21,53,119,43
1) first 17 and 15 are compared ---> comparison -1
Swap 15 and 17
2) Comparing 17 and 21 ------> comparison - 2
3) compare 21 and 208 ---------> comparison ->3
4) compare 208 and 16 ---------> comparison ->4
swap 16 and 208
5) compare 122 and 208 ---------> comparison ->5
swap 122 and 208
6) compare 208 and 212 ---------> comparison ->6
7) compare 212 and 53 ---------> comparison ->7
swap 212 and 53
8) Compare 212 and 119 ---------> Comparison -> 8
swap 119 and 212
9) compare 212 and 43 ---------> comparison ->9
swap 212 and 43
10) compare 15 and 17---------> comparison ->10
11) compare 17 and 21 ---------> comparison ->11
12) compare 21 and 16 ---------> comparison ->12
swap 16 and 21
13) compare 21 and 122 --------> comparison ->13
14 )compare 122 and 208 ---------> comparison ->14
15) compare 2018 and 53 ---------> comparison ->15
swap 53 and 208
16) compare 208 and 119 ---------> comparison ->16
swap 119 and 208
17) compare 208 and 43 ---------> comparison ->17
swap 208 and 43
18 ) compare 208 and 212 ------> comparison -> 18
19) compare 15 and 17 ---------> comparison ->19
20) compare 17 and 16 ---------> comparison ->20
swap 16 and 17
21) compare 17 and 21 ---------> comparison ->21
22) compare 21 and 122 ---------> comparison ->22
23) compare 122 and 53 ---------> comparison ->23
swap 53 and 122
24) compare 119 and 122 --------> comparison ->24
swap 119 and 122
25) compare 122 and 43 ---------> comparison ->25
swap 122 and 43
26) compare 122 and 208 ---------> comparison ->26
27) compare 208 and 212 ---------> comparison ->27
28) compare 16 ad 15 ---------> comparison ->28
29) compare 16 and 17 ---------> comparison ->29
30) compare 21 and 17 ---------> comparison ->30
31) compare 21 and 53 ---------> comparison ->31
32) compare 53 and 119 ---------> comparison ->32
33) compare 119 and 43 --------> comparison ->33
swap 43 and 119
34) compare 119 and 122 ---------> comparison ->34
35) compare 122 and 208 ---------> comparison ->35
36) compare 208 and 212 ---------> comparison ->36
37) compare 15 and 16---------> comparison ->37
38) compare 16 and 17 ---------> comparison ->38
39) compare 17 and 21 ---------> comparison ->39
40) compare 21 and 53 ---------> comparison ->40
41) compare 53 and 43 ---------> comparison ->41
swap 43 and 53
42) compare 53 and 119 ---------> comparison ->42
43) compare 119 and 122 ---------> comparison ->43
44) compare 122 and 208 ---------> comparison -> 44
45 ) compare 208 and 212 ----------> comparison-> 45
there are 45 comparisons and 16 swaps made and the sorted array is :
15,16,17,21,43,53,119,122,208,212.