Chapter 3 Sorting - PowerPoint PPT Presentation

1 / 30
About This Presentation
Title:

Chapter 3 Sorting

Description:

Kids-league baseball team. Imagine that your kids-league baseball team is lined up on the field. ... The players to the left of this marker are partially sorted. ... – PowerPoint PPT presentation

Number of Views:26
Avg rating:3.0/5.0
Slides: 31
Provided by: INT165
Category:

less

Transcript and Presenter's Notes

Title: Chapter 3 Sorting


1
Chapter 3 Sorting
  • James Chen
  • 2005/9/26

2
Outlines
  • How would you do it ?
  • Bubble Sort
  • Selection Sort
  • Insertion Sort
  • Sorting Objects
  • Comparing the Simple Sorts

3
Sorting is important!
  • As soon as you create a significant database,
    you'll probably think of reasons to sort it in
    various ways.
  • Because sorting is so important and potentially
    so time-consuming, it has been the subject of
    extensive research in computer science, and some
    very sophisticated methods have been developed.

4
Kids-league baseball team
  • Imagine that your kids-league baseball team is
    lined up on the field. You want to arrange the
    players in order of increasing height.

5
How Would You Do It?
From
To
6
Difference between Human and Computer
  • As a human being, you have advantages over a
    computer program.
  • You can see all the kids at once, and you can
    pick out the tallest kid almost instantly you
    don't need to laboriously measure and compare
    everyone.
  • A computer program isn't able to glance over the
    data in this way.
  • It can only compare two players at once, because
    that's how the comparison operators work.

7
Main Steps of Sorting Algorithms
  • The three algorithms in this chapter all involve
    two steps, executed over and over until the data
    is sorted
  • Compare two items.
  • Swap two items or copy one item.
  • However, each algorithm handles the details in a
    different way.

8
Bubble Sort
Basic Rules 0. Start from the left side. 1.
Compare two players. 2. If the one on the left is
taller, swap them. 3. Move one position right. 4.
When you reach the first sorted player, start
over at the left end of the line. Otherwise, loop
step 2.
The end of first run ?
9
The bubbleSort Workshop Applet
10
Java Code for a Bubble Sort
  • Relationship

P041043
11
Java Code for a Bubble Sort
  • public void bubbleSort()
  • int out, in
  • for(outnElems-1 outgt1 out--) // outer loop
    (backward)
  • for(in0 inltout in) // inner loop (forward)
  • if( ain gt ain1 ) // out of order?
  • swap(in, in1) // swap them
  • // end bubbleSort()

12
Invariants of Bubble Sort
  • In many algorithms there are conditions that
    remain unchanged as the algorithm proceeds. These
    conditions are called invariants.
  • In the bubbleSort.java program, the invariant is
    that the data items to the right of outer are
    sorted.

13
Efficiency of the Bubble Sort
  • In general, where N is the number of items in the
    array, there are N1 comparisons on the first
    pass, N2 on the second, and so on.
  • Thus the algorithm makes about N2/2 comparisons
    (ignoring the 1, which doesn't make much
    difference, especially if N is large).

(N1) (N2) (N3) ... 1 N(N1)/2
14
Efficiency of the Bubble Sort
  • If the data is random, a swap is necessary about
    half the time, so there will be about N2/4 swaps.
  • Both swaps and comparisons are proportional to
    N2.
  • ? Bubble sort runs in O(N2).

15
Selection Sort
  • The selection sort improves on the bubble sort by
    reducing the number of swaps necessary from O(N2)
    to O(N).
  • find the correct one for the specified position,
    then swap them.
  • Unfortunately, the number of comparisons remains
    O(N2).
  • However, the selection sort can still offer a
    significant improvement for large records that
    must be physically moved around in memory,
    causing the swap time to be much more important
    than the comparison time.

16
Selection Sort
Basic Rules 0. find the Min of (1n), then swap
Min with the 1st position. 2. Move one position
right. Find Min of (2n), then swap Min with 2nd
position. 3. And so on ( until the data are
sored. )
17
The selectSort Workshop Applet
18
Java Code for a Selection Sort
  • Relationship

P041043
19
Java Code for a Selection Sort
  • public void selectionSort()
  • int out, in, min
  • for(out0 outltnElems-1 out) // outer loop
  • min out // minimum
  • for(inout1 inltnElems in) // inner loop
  • if(ain lt amin ) // if min greater,
  • min in // we have a new min
  • swap(out, min) // swap them
  • // end for(outer)
  • // end selectionSort()

20
Invariant of Selection Sort
  • In the selectSort.java program, the data items
    with indices less than or equal to outer are
    always sorted.

21
Efficiency of the Selection Sort
  • Number of comparisons N(N1)/2.
  • Number of swaps N.
  • Comparisons time will dominate swaps time.
  • we would have to say that the selection sort runs
    in O(N2) time

22
Insertion Sort
  • Partial Sorting
  • At this point there's an imaginary marker
    somewhere in the middle of the line.
  • The players to the left of this marker are
    partially sorted. This means that they are sorted
    among themselves each one is taller than the
    person to his left.
  • However, they aren't necessarily in their final
    positions, because they may still need to be
    moved when previously unsorted players are
    inserted between them.

23
Insertion Sort
24
The insertSort Workshop Applet
25
Java Code for a Insertion Sort
  • Relationship

P041043
26
Java Code for a Insertion Sort
  • public void insertionSort()
  • int in, out
  • for(out1 outltnElems out) // out is dividing
    line
  • double temp aout // remove marked item
  • in out // start shifts at out
  • while(ingt0 ain-1 gt temp) // until one is
    smaller,
  • ain ain-1 // shift item right,
  • --in // go left one position
  • ain temp // insert marked item
  • // end for
  • // end insertionSort()

27
Invariants of Insertion Sort
  • At the end of each pass, following the insertion
    of the item from temp, the data items with
    smaller indices than outer are partially sorted.

28
Efficiency of the Insertion Sort
  • Number of comparisons
  • 1 2 3 ... N1 N(N1)/2
  • Number of swaps
  • N(N1)/4
  • The insertion sort runs in O(N2) time for random
    data.
  • For data that is already sorted or almost sorted,
    it takes O(N).
  • However, for data arranged in inverse sorted
    order, it takes O(N2), slower than bubble sort.

29
Sorting Objects
  • Some sorting routines will more likely be applied
    to objects than primitive types.
  • Examples Comparison of last name of Person
    object.
  • Lexicographical Comparisons
  • if (ain-1.getLast().compareTo(temp.getLast(
    ))gt0)

30
Comparing the Simple Sorts
  • The bubble sort is so simple you can write it
    from memory.
  • The selection sort minimizes the number of swaps,
    but the number of comparisons is still high.
  • The insertion sort is the most versatile of the
    three and is the best bet in most situations,
    assuming the amount of data is small or the data
    is almost sorted.
  • All three of the algorithms in this chapter carry
    out their sort in place, meaning that, beside the
    initial array, very little extra memory is
    required.
  • For larger amounts of data, quicksort is
    generally considered the fastest approach
Write a Comment
User Comments (0)
About PowerShow.com