Follow

Translate

Friday, March 11, 2016

Number Sorted Of Bubble Sort

Suppose the following numbers are stored in an array A:

                                                             32, 51, 27, 85, 66, 23, 13, 57

We apply the bubble sort to the array A. We discuss each pass separately. Pass 1. We have the following comparisons:


(a)  Compare A1  and A2. Since 32 < 51, the list is not altered.
(b)  Compare A2  and A3. Since 51 <  27, interchange 51 and 27 as follows:
                                                      
                                                  32, 27, 51, 85, 66, 23, 13, 57

(c)  Compare A3  and A4. Since 51< 85, the list is not altered.
(d)  Compare A4  and A5. Since 85 > 66, interchange 85 and 86 as follows:
                                                      
                                                  32, 27, 51, 66, 85, 23, 13, 57

(e)  Compare A5  and A6. Since 85 > 23, interchange 85 and 86 as follows:
                                                      
                                                  32, 27, 51, 66,  23, 85, 13, 57

(f)  Compare A6  and A7 Since 85 > 13, interchange 85 and 13 to yield:
                                                      
                                                  32, 27, 51, 66,  23, 13,  85, 57

(g)  Compare A7  and A8. Since 85 > 57, interchange 85 and 51 to yield:
                                                      
                                                  32, 27, 51, 66,  23, 13,  57, 85


At the end of this first pass, the largest number, 85, has moved to the last position. However, the rest of the numbers are not sorted, even though some of them have changed their position.


For the remainder of the passes, We show only the interchange.

Pass 2.  27, 33, 51, 66, 23, 13, 57, 85
             27, 33, 51, 23, 66, 13, 57, 85
             27, 33, 51, 23, 13, 66, 57, 85
             27, 33, 51, 23, 13, 57, 66, 85

At the end of Pass 2, the second largest number, 66, has moved its way down to the next-to-last position.

Pass 3.      27, 33, 23, 51, 13, 57, 66, 85
                 27, 33, 23, 13, 51, 57, 66, 85 

Pass 4.     27,  23, 33, 13, 51, 57, 66, 85  
                27,  23, 13, 33, 51, 57, 66, 85  

Pass 5.     23, 27, 13, 33, 51, 57, 66, 85 
                23, 13, 27, 33, 51, 57, 66, 85   

Pass 6.   13, 23, 27, 33, 51, 57, 66, 85       

Pass 6 actually has two comparisons, A1 with A2 and A3. The second comparison does not involve an interchange. 

Pass 7. Finally,  A1 is compared with A2. Since 13 < 23, no interchange takes place.

Since the list has 8 elements; it is sorted after the seventh pass. 
           
If You want to learn about the technology, computer science & engineering, web programming, freelancing, earning please click here :CSE SOLVE

0 comments:

Post a Comment