bubble sort - PowerPoint PPT Presentation

1 / 15
About This Presentation
Title:

bubble sort

Description:

Traverse a collection of elements Move from the front to the end Bubble the largest value to the end using pair-wise comparisons and swapping – PowerPoint PPT presentation

Number of Views:299
Avg rating:3.0/5.0
Slides: 16
Provided by: MikeSc154
Category:
Tags: bubble | sort | sorting

less

Transcript and Presenter's Notes

Title: bubble sort


1
bubble sort
  • Traverse a collection of elements
  • Move from the front to the end
  • Bubble the largest value to the end using
    pair-wise comparisons and swapping

1 2 3 4 5
6
12
101
5
35
42
77
2
bubble sort
  • Traverse a collection of elements
  • Move from the front to the end
  • Bubble the largest value to the end using
    pair-wise comparisons and swapping

1 2 3 4 5
6
Swap
12
101
5
35
42
77
3
bubble sort
  • Traverse a collection of elements
  • Move from the front to the end
  • Bubble the largest value to the end using
    pair-wise comparisons and swapping

1 2 3 4 5
6
Swap
12
101
5
35
77
42
4
bubble sort
  • Traverse a collection of elements
  • Move from the front to the end
  • Bubble the largest value to the end using
    pair-wise comparisons and swapping

1 2 3 4 5
6
Swap
12
101
5
77
35
42
5
bubble sort
  • Traverse a collection of elements
  • Move from the front to the end
  • Bubble the largest value to the end using
    pair-wise comparisons and swapping

1 2 3 4 5
6
77
101
5
12
35
42
No need to swap
6
bubble sort
  • Traverse a collection of elements
  • Move from the front to the end
  • Bubble the largest value to the end using
    pair-wise comparisons and swapping

1 2 3 4 5
6
Swap
77
101
5
12
35
42
7
bubble sort
  • Traverse a collection of elements
  • Move from the front to the end
  • Bubble the largest value to the end using
    pair-wise comparisons and swapping

1 2 3 4 5
6
101
77
5
12
35
42
Largest value correctly placed
8
Insertion Sort
  • Insertion sort is a simple sorting algorithm that
    is appropriate for small inputs (not good for
    large amounts of data)
  • In each step of an insertion sort, one or more
    pieces of data are inserted into their correct
    location in an ordered list (just as a card
    player picks up cards and places them in his hand
    in order).

9
Running Time
  • The insertion sort is quadratic in the worst and
    average cases
  • The running time is linear O(N) if input is
    pre-sorted.
  • The running time depends on the amount of the
    input and the specific ordering of input
  • An inversion is a pair of elements that are out
    of order in an array
  • Any ordered pair (i,j) has the property that iltj
    but AigtAj
  • E.g., 8, 5, 9, 2, 6, 3 has 10 inversions
  • Number of inversions measures the unsortedness
  • The average number of inversion in an array of N
    distinct numbers in N(N-1)/4

10
Merge sort
  • Merge sort uses linear extra memory merging two
    sorted lists
  • Additional work in copying to the temporary array
    and back
  • Excessive copying can be avoided with more work,
    but the linear extra memory can not be removed
    without excessive time penalities.
  • Switching the role of a and tempArray

11
Quick Sort
  • Quick sort is fast because the partitioning step
    can be performed quickly and in place
  • Significantly faster than merging step
  • Without an extra array
  • Best case O(NlogN) two half-sized recursive
    calls with linear overhead
  • Worst case occurs when the partition repeatedly
    generates an empty subsets.
  • T(N)T(N-1)N
  • The running time is then O(N2)
  • Average case is O(NlogN)

12
Summary I
Sort Best Case Worst Case Average Case
Insertion O(n) O(n2) O(n2)
Bubble O(n) O(n2) O(n2)
Heap O(nlog2n) O(nlog2n) O(nlog2n)
Quick O(nlog2n) O(n2) O(nlog2n)
Merge O(nlog2n) O(nlog2n) O(nlog2n)
13
Summary II
Name In place Method
Bubble sort yes Exchanging
Insertion sort yes Insertion
Heap Sort yes Heap
Merge sort no Merging
Quicksort yes Partitioning
A sorting algorithm is in-place if it uses only a
small Number of memory in addition to that
needed to store the input array. In other words,
only a constant Number of array elements are
stored outside the input array at any time.
14
Some Remarks
  • Insertion-sort is a good choice for small input
    size (say, less than 50) and for sequences that
    are already almost sorted.
  • Merge-sort is difficult to run in-place, is an
    excellent algorithm for situations where the
    input can not fit into main memory, but must be
    stored in blocks on an external memory device,
    e.g., disks.
  • Quick sort is an excellent choice for
    general-purpose, in-memory sorting. In spite of
    its slow worst-case running time. The constant
    factors hidden in O(nlgn) for average case are
    quite small.

15
Summary III
Name Stable Method
Bubble sort yes Exchanging
Insertion sort yes Insertion
Heap Sort no Heap
Merge sort yes Merging
Quicksort no Partitioning
Write a Comment
User Comments (0)
About PowerShow.com