Post on 02-Jan-2016
1
Today’s Objectives
Announcements• Hand in Homework 4 – no late assignments accepted! No email submissions!• The final exam will be on 1-May at 7 p.m. in D201 – No alternate times, no makeup exam• Quiz 4 will be next week, 24-Apr
– Some simple Java, for example, write a Java statement that uses a public method of a class
– Know about using an ArrayList to store tree data
– Know how to draw a picture of a binary search tree if I give you a list of data to insert into it
– Know about separate chaining and open address collision resolution
– Know the runtime efficiencies of sorting algorithms that we’ve studied
Student presentations Maps and hash tables – (from last week’s slides)
• Map ADT• Hash function and hash code• Collision handling• Ordered search tables and binary search
Sorting• Bubble sort• Selection sort• Insertion sort
Week 14
3
Sorting
Arranging data into ascending or descending order
Influences the speed and complexity of algorithms that use the data, e.g. searching
Many algorithms – many implementations• Bubble sort• Selection sort• Insertion sort• Shell sort• Quick sort
Sorting (Knuth, 1–5)
4
Sorting
Some uses of sorting:
• Grouping equal items in a collection
• Matching data in more than one file
• Speeding up the process of searching for data
Sorting (Knuth, 1–5)
5
Sorting Algorithms
Two general types:
• For internal sorting – all data is kept in memory
• For external sorting – some of the data is stored on external media
Sorting (Knuth, 1–5)
6
Two Important Measuresof Sorting Efficiency
1. Running time• The most important measure of efficiency• Compare number of comparisons to number of elements (n)• Less important measure: number of swaps
2. Space• Amount of extra memory used is also important• Sorting in place
– Instead of transferring elements out of a sequence and then back into it, we just re-arrange them
– Uses a constant amount of memory
– More efficient space usage
Sorting (Sedgewick, 270–271; Dale, 586)
8
Bubble Sort
One way to sort elements stored in a linear sequence
Easy to understand
In each loop through the sequence, each successive pair of elements is compared
If the first element is larger than the second, they are swapped
Sequence (Goodrich 2004, 235–237)
13
Bubble Sort
abcde
abced
abecd
aebcd
eabcdSorted
First pass throughouter loopi = 0
Sequence (Goodrich 2004, 235–237)
j j+1
j j+1
j j+1
j j+1
14
Bubble Sort
abcde
abced
abecd
aebcd
eabcdSorted
Sequence (Goodrich 2004, 235–237)
j j+1
j j+1
j j+1
j j+1
First pass throughouter loopi = 0
for(int i=0;i<A.size()-1;++i){ for (int j=0; j<A.size()-(i+1);++j){
}}
for(int i=0;i<A.size()-1;++i){ for (int j=0; j<A.size()-(i+1);++j){
}}
15
Bubble Sort
eabcd
eabdc
eadbc
edabc
Second pass throughouter loopi = 1
Sorted
Sequence (Goodrich 2004, 235–237)
j j+1
j j+1
j j+1
16
Bubble Sort
edabc
edacb
edcab
Third pass throughouter loopi = 2
Sorted
Sequence (Goodrich 2004, 235–237)
j j+1
j j+1
17
Bubble Sort
edcab
edcba
Fourth pass throughouter loopi = 3
Sorted
Sequence (Goodrich 2004, 235–237)
j j+1
18
Bubble Sort Algorithm
import java.util.*;public class DemoSorting {
public static void bubbleSort(ArrayList<Integer> A){ for (int i=0; i<A.size()-1; ++i){ for (int j=0; j<A.size()-(i+1); ++j){ if (A.get(j) > A.get(j+1)){ Collections.swap(A, j, j+1); } } } }
public static void main(String[] args) { ArrayList<Integer> A = new ArrayList<Integer>(); A.add(6274); A.add(4892); A.add(2843); A.add(9523); A.add(1892); bubbleSort(A); for(Integer i : A) System.out.print(i + " "); } }}
What is the Big-Oh?
Sequence (Goodrich 2004, 235–237)
19
Sorting in Place – Selection SortSorting
The main idea:• We find the smallest element and put it in the first position.• Then we find the next smallest element and put it in the second position.• Continue this way until the entire collection is sorted.
20
Sorting in Place – Selection Sort
18929523284348926274
Unsorted
Sorting
62749523284348921892
Sorted
62749523489228431892
62749523489228431892
95236274489228431892
The main idea:• We find the smallest element and put it in the first position.• Then we find the next smallest element and put it in the second position.• Continue this way until the entire collection is sorted.
Unsorted array
21
Selection Sort
import java.util.*;public class Main {
public static void selectionSort(ArrayList<Integer> A){ for (int i=0; i<A.size()-1; ++i){ int minIndex = i; for (int j=i+1; j<A.size(); ++j) if (A.get(j) < A.get(minIndex)) minIndex = j; Collections.swap(A, i, minIndex); } } public static void main(String[] args) { ArrayList<Integer> A = new ArrayList<Integer>(); A.add(6274); A.add(4892); A.add(2843); A.add(9523); A.add(1892); selectionSort(A); for(Integer i : A) System.out.print(i + " "); }}
Sorting (Sedgewick, 273)
What’s the Big-Oh?
22
Sorting in Place – Insertion SortSorting (Sedgewick, 274; Bentley, 115–116)
The main idea:• Examine the elements one at a time• Insert each into its proper order among the elements already examined
23
Sorting in Place – Insertion Sort
18929523284348926274
Unsorted
Sorting (Sedgewick, 274; Bentley, 115–116)
18929523284348926274
Sorted
18929523284362744892
18929523627448922843
18929523627448922843
95236274489228431892
The main idea:• Examine the elements one at a time• Insert each into its proper order among the elements already examined
Unsorted array
24
Insertion Sort
import java.util.*;public class Main {
public static void insertionSort(ArrayList<Integer> A){ for( int i=1; i<A.size(); ++i ) for( int j=i; j>0 && A.get(j-1)>A.get(j); --j ){ Collections.swap(A, j, j-1); } } public static void main(String[] args) { ArrayList<Integer> A = new ArrayList<Integer>(); A.add(6274); A.add(4892); A.add(2843); A.add(9523); A.add(1892); insertionSort(A); for(Integer i : A) System.out.print(i + " "); }}
Sorting (Bentley, 115–116)
What’s the Big-Oh?
25
Stable Sorting
Items that have equal key values are kept in their original order
Sorting (Knuth, 1–5)
Second sort – with the state as a keyFirst sort – with the city as a keyUnsorted
San Francisco, CADetroit, MIAustin, TX
Los Angeles, CAAnn Arbor, MIDetroit, MI
Austin, TXMonterey, CADallas, TX
Detroit, MILos Angeles, CASan Francisco, CA
Ann Arbor, MIHouston, TXSan Antonio, TX
San Antonio, TXSan Francisco, CASacramento, CA
Houston, TXSan Antonio, TXLos Angeles, CA
Dallas, TXSacramento, CAAnn Arbor, MI
Sacramento, CADallas, TXMonterey, CA
Monterey, CAAustin, TXHouston, TX
26
Sorting Algorithms
So far, we’ve studied:
Sorting
O(n)O(n2)Insertion sort
O(n2)O(n2)Selection sort
O(n2)O(n2)Bubble sort
Best caseefficiencyWorst case efficiencyAlgorithm
Next• Shell sort• Quick sort
27
Sorting Demos
Race Demo:Bubble Sort vs. Heap Sort vs. Quick Sort
Animated Sorting Web Sites:http://www.cosc.canterbury.ac.nz/people/mukundan/dsal/appldsal.html
http://www.cs.ubc.ca/spider/harrison/Java/sorting-demo.html
http://cg.scs.carleton.ca/~morin/misc/sortalg/
Sorting
28
References
Bentley, J., Programming Pearls, Second Edition. Boston: Addison-Wesley, 2000.
Dale, N., C++ Plus Data Structures. Boston: Jones and Bartlett Publishers, 2003.
Gilberg, R. F. and B. A. Forouzan, Data Structures, A Pseudocode Approach with C. Boston: PWS Publishing Company, 1998.
Goodrich, M. T., D. Mounts, and R. Tamassia, Data Structures and Algorithms in C++. Hoboken, NJ: John Wiley & Sons, Inc., 2004.
Goodrich, M. T. and R. Tamassia, Data Structures and Algorithms in Java. Hoboken, NJ: John Wiley & Sons, Inc., 2006.
Knuth, D. E., The Art of Computer Programming, Second Edition, Volume 3 / Sorting and Searching. Boston: Addison-Wesley, 1998.
Sedgewick, R., Algorithms in C++, Third Edition. Boston: Addison-Wesley, 1998.