Bubble Sorting - PowerPoint PPT Presentation

About This Presentation
Title:

Bubble Sorting

Description:

Light elements rise to the 'top' Heavy elements sink to the 'bottom' 'Top' / 'bottom' can arbitrarily be defined to be the beginning or end of array ... – PowerPoint PPT presentation

Number of Views:343
Avg rating:3.0/5.0
Slides: 18
Provided by: Yi
Category:
Tags: bubble | heavy | sorting | top

less

Transcript and Presenter's Notes

Title: Bubble Sorting


1
Bubble Sorting
  • Comp 208 Computers in Engineering
  • Yi Lin
  • Winter, 2007

2
Analogy to buoyancy
  • Bubble sort is an analogy
  • Light elements rise to the top
  • Heavy elements sink to the bottom
  • Top / bottom can arbitrarily be defined to be
    the beginning or end of array to obtain
    increasing / decreasing orders

3
One pass bubbling
  • One pass bubbling
  • go through the list comparing each adjacent
    elements,
  • if they're not in order swap them,
  • doing one pass of this guarantees that the
    smallest element will end up at the beginning of
    the list.

4
2
3
1
4
2
1
3
4
1
2
3
1
4
2
3
4
Multiple passes
  • Go through the list again until all the elements
    are in order. A total of N-1 passes need to be
    made for all the list to be in order.

1
4
2
3
1
2
4
3
1
2
4
3
1
2
3
4
2nd pass
3rd pass
5
Bubble sort
  • First pass bubbling

32
15
38
56
23
35
74
32
15
38
23
56
35
74
32
15
23
38
56
35
74
15
32
23
38
56
35
74
6
Bubble sort
  • 2nd pass

15
32
23
38
56
35
74
15
32
23
38
35
56
74
15
32
23
35
38
56
74
15
23
32
35
38
56
74
7
Bubble sort function example 4
  • void bubbleSort(int a, int size)
  • int i, j
  • for(i0 iltsize i)
  • for(jsize-1 jgt0 j--)
  • if(aj lt aj-1)
  • swap(aj, aj-1)

8
Two optimizations
  • We know that after X passes the X smaller
    elements are at the beginning of the list, it
    therefore isn't useful to pass through these
    elements again, therefore pass X will have N-X
    comparisons instead of N
  • If we haven't swapped in a pass then the list is
    in order, therefore we can finish sorting

9
Bubble sort optimization 1Example 5
  • void bubbleSort(int a, int size)
  • int i, j
  • for(i0 iltsize-1 i)
  • for(jsize-1 jgti j--) // only need to
    check ai to asize-1
  • if(aj lt aj-1)
  • swap(aj, aj-1)

10
Bubble sort optimization 2Example 6
  • void bubbleSort(int a, int size)
  • int i, j
  • int swapped
  • for(i0 iltsize-1 i)
  • swapped0
  • for(jsize-1 jgti j--)
  • if(aj lt aj-1)
  • swap(aj, aj-1)
  • swapped1
  • if(swapped0)
  • break

11
Insertion Sorting
  • Comp 208 Computers in Engineering
  • Yi Lin
  • Winter, 2007

12
How do you update a sorted list?
  • What if you already had a sorted list and wanted
    to add one more member to it?
  • If you cant arbitrarily insert (as we cant)
  • Put the element at the beginning / end
  • Use a single bubble pass to move it to its
    final location
  • This can be used to sorted completely unsorted
    lists too!

13
Insertion sort
  • First pass
  • 2nd pass

32
15
38
56
23
35
74
15
32
38
56
23
35
74
15
32
38
56
23
35
74
14
Insertion sort
  • 3rd pass
  • 4th pass

15
32
38
56
23
35
74
15
23
32
38
56
35
74
15
32
38
56
23
35
74
15
32
38
23
56
35
74
15
32
23
38
56
35
74
15
Insertion sort
  • 5th pass

15
23
32
38
56
35
74
15
23
32
38
35
56
74
15
23
32
38
35
56
74
15
23
32
35
38
56
74
16
Insertion sort, example 7
  • void insertionSort(int a, int size)
  • int i, j
  • for(i1 iltsize i) // i0 also works fine.
  • for(ji jgt0 j--)
  • if(aj lt aj-1)
  • swap(aj, aj-1)

17
Insertion sort optimizationExample 8
  • void insertionSort(int a, int size)
  • int i, j
  • for(i1 iltsize i) // i0 also works fine.
  • for(ji jgt0 j--)
  • if(aj lt aj-1)
  • swap(aj, aj-1)
  • else
  • break
Write a Comment
User Comments (0)
About PowerShow.com