Docstoc

Data Structures Bubble Sort

Document Sample
Data Structures Bubble Sort Powered By Docstoc
					           DATA
        STRUCTURES

                MAHESH GOYANI
MAHATMA GANDHI INSTITUE OF TECHNICAL EDUCATION & RESEARCH CENTER
                     mgoyani@rediffmail.com




                          (C) GOYANI MAHESH                        1
BUBBLE
 SORT

 (C) GOYANI MAHESH   2
TERMINOLOGY




       In plain English: traverse the array, comparing adjacent
       elements, and swapping them if out of order, such that the
       largest element percolates to the end of the array; repeat over
       the unsorted sub array until no swaps occur


       In pseudo code:
  do
        for each element e from first to penultimate unsorted
           if e is out of order with the following element
               swap the elements
  while swap occurred in last pass

                              (C) GOYANI MAHESH                          3
 6
12    6
     12   14
          22          22
                      14
                       8       8
                              17
                              22   22
                                   17


6    12   14
          8           14
                      8       17   22




          (C) GOYANI MAHESH             4
6    8
    12   12
         8           14      17   22


6   8    12          14      17   22




         (C) GOYANI MAHESH             5
45312




        (C) GOYANI MAHESH   6
45312

45312




        (C) GOYANI MAHESH   7
45312

45312

43512




        (C) GOYANI MAHESH   8
45312

45312

43512

43152


        (C) GOYANI MAHESH   9
45312

45312

43512

43152

43125   (C) GOYANI MAHESH   10
45312   43125

45312

43512

43152

43125      (C) GOYANI MAHESH   11
45312   43125

45312   34125

43512

43152

43125      (C) GOYANI MAHESH   12
45312   43125

45312   34125

43512   31425

43152

43125      (C) GOYANI MAHESH   13
45312   43125

45312   34125

43512   31425

43152   31245

43125      (C) GOYANI MAHESH   14
45312   43125           31245

45312   34125

43512   31425

43152   31245

43125      (C) GOYANI MAHESH    15
45312   43125           31245

45312   34125           13245

43512   31425

43152   31245

43125      (C) GOYANI MAHESH    16
45312   43125           31245

45312   34125           13245

43512   31425           12345

43152   31245

43125      (C) GOYANI MAHESH    17
45312   43125           31245   12345

45312   34125           13245

43512   31425           12345

43152   31245

43125      (C) GOYANI MAHESH       18
45312   43125           31245   12345

45312   34125           13245   12345

43512   31425           12345

43152   31245

43125      (C) GOYANI MAHESH       19
SIMMULATION




    7 2 8 5 4   2 7 5 4 8         2 5 4 7 8   2 4 5 7 8
    2 7 8 5 4   2 7 5 4 8         2 5 4 7 8   2 4 5 7 8
    2 7 8 5 4   2 5 7 4 8         2 4 5 7 8     (done)
    2 7 5 8 4   2 5 4 7 8
    2 7 5 4 8




                      (C) GOYANI MAHESH                   20
COMPLEXITY




   Each pass bubbles the highest remaining element to the top of the array; in
 the worst case there could be n-1 passes, thus the outer loop would execute n-1
 times;
      for the first iteration of the outer loop, the inner loop executes n-1 times
      for the second iteration of the outer loop, the inner loop executes n-2
    times
      etc
      for the last iteration of the outer loop, the inner loop executes 1 time
      thus the execution time is (n-1) + (n-2) + (n-3) +         +3+2+1
      the sum of this series is (n2 - n) / 2
      bubble sort is O(n2 )




                                 (C) GOYANI MAHESH                               21

				
DOCUMENT INFO