Upload
warren-banks
View
217
Download
0
Embed Size (px)
Citation preview
Comparison-Based Sorting & AnalysisComparison-Based Sorting & Analysis
Smt Genap 2011-2012
OutlineOutlineSeveral sorting algorithms:
◦Bubble Sort◦Selection Sort◦ Insertion Sort◦Shell Sort
For each algorithm:◦Basic Idea◦Example◦Implementation◦Algorithm Analysis
Smt Genap 2011-2012
SortingSorting Sorting = ordering. Sorted = ordered based on a particular way. Generally, collections of data are presented in a
sorted manner. Examples of Sorting:
◦ Words in a dictionary are sorted (and case distinctions are ignored).
◦ Files in a directory are often listed in sorted order.◦ The index of a book is sorted (and case distinctions
are ignored).◦ Many banks provide statements that list checks in
increasing order (by check number).◦ In a newspaper, the calendar of events in a
schedule is generally sorted by date.◦ Musical compact disks in a record store are
generally sorted by recording artist. Why?
◦ Imagine finding the phone number of your friend in your mobile phone, but the phone book is not sorted.
Smt Genap 2011-2012
Bubble Sort: IdeaBubble Sort: IdeaIdea: bubble in water.
◦Bubble in water moves upward. Why?
How?◦When a bubble moves upward, the
water from above will move downward to fill in the space left by the bubble.
Smt Genap 2011-2012
Bubble Sort: ExampleBubble Sort: Example
Notice that at least one element will be in the correct position each iteration.
40 2 1 43 3 65 0 -1 58 3 42 4
652 1 40 3 43 0 -1 58 3 42 4
65581 2 3 40 0 -1 43 3 42 4
1 2 3 400 65-1 43 583 42 4
1
2
3
4
Smt Genap 2011-2012
Bubble Sort: ExampleBubble Sort: Example
Smt Genap 2011-2012
1 0 -1 32 653 43 5842404
0 -1 1 2 653 43 58424043
-1 0 1 2 653 43 58424043
6
7
8
Stop here… why?
1 2 0 3-1 3 40 6543 584245
Bubble Sort: Bubble Sort: ImplementationImplementation
void sort(int a[]){ for (int i = a.length; i>=0; i--) { boolean swapped = false; for (int j = 0; j<i; j++) { ... if (a[j] > a[j+1]) { int T = a[j]; a[j] = a[j+1]; a[j+1] = T; swapped = true; } ... } if (!swapped) return; }}
Smt Genap 2011-2012
Bubble Sort: AnalysisBubble Sort: AnalysisRunning time:
◦Worst case: O(N2)◦Best case: O(N) -- when? why?
Variant:◦bi-directional bubble sort
original bubble sort: only works to one direction
bi-directional bubble sort: works back and forth.
Smt Genap 2011-2012
Selection Sort: IdeaSelection Sort: Idea1. We have two group of items:
◦ sorted group, and◦ unsorted group
2. Initially, all items are in the unsorted group. The sorted group is empty. ◦ We assume that items in the unsorted
group unsorted. ◦ We have to keep items in the sorted group
sorted. 3. Select the “best” (eg. smallest) item from
the unsorted group, then put the “best” item at the end of the sorted group.
4. Repeat the process until the unsorted group becomes empty.
Smt Genap 2011-2012
Selection Sort: Example Selection Sort: Example
Smt Genap 2011-2012
4240 2 1 3 3 4 0 -1 655843
40 2 1 43 3 4 0 -1 42 65583
40 2 1 43 3 4 0 -1 58 3 6542
40 2 1 43 3 65 0 -1 58 3 42 4
Selection Sort: Example Selection Sort: Example
Smt Genap 2011-2012
4240 2 1 3 3 4 0 655843-1
42-1 2 1 3 3 4 0 65584340
42-1 2 1 3 3 4 655843400
42-1 2 1 0 3 4 655843403
Selection Sort: Example Selection Sort: Example
Smt Genap 2011-2012
1
42-1 2 1 3 4 6558434030
42-1 0 3 4 6558434032
1 42-1 0 3 4 6558434032
1 420 3 4 6558434032-1
1 420 3 4 6558434032-1
Selection Sort: Selection Sort: ImplementationImplementationvoid sort(int a[]) throws Exception { for (int i = 0; i < a.length; i++) { int min = i;
int j; /* Find the smallest element in the unsorted list */ for (j = i + 1; j < a.length; j++) ...
} if (a[j] < a[min]) { min = j; } ... }
Smt Genap 2011-2012
Selection Sort: ImplementationSelection Sort: Implementation
/* Swap the smallest unsorted element into the end of the sorted list. */ int T = a[min]; a[min] = a[i]; a[i] = T; ...
}}
Smt Genap 2011-2012
Selection Sort: AnalysisSelection Sort: AnalysisRunning time:
◦Worst case: O(N2)◦Best case: O(N2)
Based on big-oh analysis, is selection sort better than bubble sort?
Does the actual running time reflect the analysis?
Smt Genap 2011-2012
Insertion Sort: IdeaInsertion Sort: IdeaIdea: sorting cards.
◦8 | 5 9 2 6 3◦5 8 | 9 2 6 3◦5 8 9 | 2 6 3◦2 5 8 9 | 6 3◦2 5 6 8 9 | 3◦2 3 5 6 8 9 |
Smt Genap 2011-2012
Insertion Sort: IdeaInsertion Sort: Idea1. We have two group of items:
◦ sorted group, and◦ unsorted group
2. Initially, all items in the unsorted group and the sorted group is empty. ◦ We assume that items in the unsorted
group unsorted. ◦ We have to keep items in the sorted
group sorted. 3. Pick any item from, then insert the
item at the right position in the sorted group to maintain sorted property.
4. Repeat the process until the unsorted group becomes empty.
Smt Genap 2011-2012
Insertion Sort: Example Insertion Sort: Example
Smt Genap 2011-2012
40 2 1 43 3 65 0 -1 58 3 42 4
2 40 1 43 3 65 0 -1 58 3 42 4
1 2 40 43 3 65 0 -1 58 3 42 4
40
Insertion Sort: Example Insertion Sort: Example
Smt Genap 2011-2012
1 2 3 40 43 65 0 -1 58 3 42 4
1 2 40 43 3 65 0 -1 58 3 42 4
1 2 3 40 43 65 0 -1 58 3 42 4
Insertion Sort: ExampleInsertion Sort: Example
Smt Genap 2011-2012
1 2 3 40 43 65 0 -1 58 3 42 4
1 2 3 40 43 650 -1 58 3 42 4
1 2 3 40 43 650 58 3 42 41 2 3 40 43 650-1
Insertion Sort: Example Insertion Sort: Example
Smt Genap 2011-2012
1 2 3 40 43 650 58 3 42 41 2 3 40 43 650-1
1 2 3 40 43 650 58 42 41 2 3 3 43 650-1 5840 43 65
1 2 3 40 43 650 42 41 2 3 3 43 650-1 5840 43 65
1 2 3 40 43 650 421 2 3 3 43 650-1 584 43 6542 5840 43 65
Insertion Sort: Insertion Sort: ImplementationImplementationInsertion sort to sort an array of integers
public static void insertionSort (int[] a){ for (int ii = 1; ii < a.length; ii++) { int jj = ii; while (( jj > 0) && (a[jj] < a[jj - 1])) { int temp = a[jj]; a[jj] = a[jj - 1]; a[jj - 1] = temp; jj--; } }}
Note: value of a[jj] always the same possibility for improvement of efficiency.
Smt Genap 2011-2012
Insertion Sort: Efficient Insertion Sort: Efficient ImplementationImplementationA slightly more efficient Insertion
sortpublic static void insertionSort2 (int[] a){ for (int ii = 1; ii < a.length; ii++) { int temp = a[ii]; int jj = ii; while (( jj > 0) && (temp < a[jj - 1])) {
a[jj] = a[jj - 1]; jj--; } a[jj] = temp; }}
Smt Genap 2011-2012
Insertion Sort: Analysis Insertion Sort: Analysis Running time analysis:
◦ Worst case: O(N2)◦ Best case: O(N)
Is insertion sort faster than selection sort?
Notice the similarity and the difference between insertion sort and selection sort.
Smt Genap 2011-2012
A Lower BoundA Lower BoundBubble Sort, Selection Sort,
Insertion Sort all have worst case of O(N2).
Turns out, for any algorithm that exchanges adjacent items, this is the best worst case: Ω(N2)
In other words, this is a lower bound!
See proof in Section 8.3 of Weiss
Smt Genap 2011-2012
Shell Sort: IdeaShell Sort: Idea
Smt Genap 2011-2012
40 2 1 43 3 65 0 -1 58 3 42 4
Original:
5-sort: Sort items with distance 5 element:
40 2 1 43 3 65 0 -1 58 3 42 4
Donald Shell (1959): Exchange items that are far apart!
Shell Sort: ExampleShell Sort: Example
Smt Genap 2011-2012
40 2 1 43 3 65 0 -1 58 3 42 4
Original:
40 0 -1 43 3 42 2 1 58 3 65 4
After 5-sort:
2 0 -1 3 1 4 40 3 42 43 65 58
After 3-sort:
After 1-sort:
1 2 3 40 43 650 421 2 3 3 43 650-1 584 43 6542 5840 43 65
Shell Sort: Gap ValuesShell Sort: Gap ValuesGap: the distance between items
being sorted.As we progress, the gap decreases.
Shell Sort is also called Diminishing Gap Sort.
Shell proposed starting gap of N/2, halving at each step.
There are many ways of choosing the next gap.
Smt Genap 2011-2012
Shell Sort: AnalysisShell Sort: Analysis
Smt Genap 2011-2012
Shell's Odd Gaps Only Dividing by 2.21000 122 11 11 92000 483 26 21 234000 1936 61 59 548000 7950 153 141 114
16000 32560 358 322 26932000 131911 869 752 57564000 520000 2091 1705 1249
ShellsortN
Insertion Sort
O(N3/2)? O(N5/4)? O(N7/6)?
So we have 3 nested loops, but Shell Sort is still better than Insertion Sort! Why?
Generic SortGeneric SortSo far we have methods to sort integers.
What about Strings? Employees? Cookies?
A new method for each class? No!In order to be sorted, objects should be
comparable (less than, equal, greater than).
Solution:◦ use an interface that has a method to
compare two objects.Remember: A class that implements an
interface inherits the interface (method definitions) = interface inheritance, not implementation inheritance.
Smt Genap 2011-2012
The The ComparableComparable Interface Interface In Java, generic aspect of
“comparable” is defined in an interface in package java.lang:public interface Comparable{ public int compareTo (Object ob);}◦ method compareTo returns:
negative integer: the object (this) is smaller than the parameter ‘ob’
0: the object is equal to the parameter ‘ob’ positive integer: the object (this) is greater
than the parameter ‘ob’
Smt Genap 2011-2012
Interface: ExampleInterface: Examplepublic class CircleComparable extends Circle implements Comparable{ public CircleComparable (double r) {super (r);}
public int compareTo (Object other) { CircleComparable otherCircle = (CircleComparable) other; if (radius < otherCircle.getRadius ()) return -1; else if (radius > otherCircle.getRadius ()) return 1; else return 0; }}
Smt Genap 2011-2012
Insertion Sort: Generic Insertion Sort: Generic SortSortGeneric Insertion sortpublic static void insertionSort3 (Comparable[] a){ for (int ii = 1; ii < a.length; ii++) { Comparable temp = a[ii]; int jj = ii; while (( jj > 0) && (temp.compareTo (a[jj - 1]) < 0)) { a[jj] = a[jj - 1]; jj--; } a[jj] = temp; }}
Smt Genap 2011-2012
import java.util.*;
public class SortCircle
{ public static void main (String args[])
{ CircleComparable[] ling =
new CircleComparable[20];
Random generator = new Random ();
for (int ii = 0; ii < ling.length; ii++) {
ling[ii] = new CircleComparable (
1 + generator.nextInt (100));
System.out.print (ling[ii].getRadius () + " ");
}
System.out.println ();
Sort.insertionSort3 (ling);
for (int ii = 0; ii < ling.length; ii++) {
System.out.print (ling[ii].getRadius () + " ");
}
System.out.println ();
}
}
Smt Genap 2011-2012
Other kinds of sortOther kinds of sortMerge SortQuick SortHeap sort. Postman sort / Radix Sort.etc.
Smt Genap 2011-2012
Further ReadingFurther ReadingWeiss book, chapter 7: Sorting
Algorithm
Smt Genap 2011-2012