Title: Chapter 3 Sorting
1Chapter 3 Sorting
2Outlines
- How would you do it ?
- Bubble Sort
- Selection Sort
- Insertion Sort
- Sorting Objects
- Comparing the Simple Sorts
3Sorting 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.
4Kids-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.
5How Would You Do It?
From
To
6Difference 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.
7Main 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.
8Bubble 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 ?
9The bubbleSort Workshop Applet
10Java Code for a Bubble Sort
P041043
11Java 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()
12Invariants 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.
13Efficiency 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
14Efficiency 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).
15Selection 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.
16Selection 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. )
17The selectSort Workshop Applet
18Java Code for a Selection Sort
P041043
19Java 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()
20Invariant of Selection Sort
- In the selectSort.java program, the data items
with indices less than or equal to outer are
always sorted.
21Efficiency 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
22Insertion 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.
23Insertion Sort
24The insertSort Workshop Applet
25Java Code for a Insertion Sort
P041043
26Java 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()
27Invariants 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.
28Efficiency 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.
29Sorting 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)
30Comparing 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