Week 14 Todays Objectives Announcements Hand in Homework
Week 14 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 D 201 – No alternate times, no makeup exam Quiz 4 will be next week, 24 -Apr – – – Student presentations Maps and hash tables – (from last week’s slides) • • Some simple Java, for example, write a Java statement that uses a public method of a class Know about using an Array. List 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 Map ADT Hash function and hash code Collision handling Ordered search tables and binary search Sorting • • • Bubble sort Selection sort Insertion sort 1
Sorting Bubble Sort Selection Sort Insertion Sort 2
Sorting (Knuth, 1– 5) 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 3
Sorting (Knuth, 1– 5) 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 4
Sorting (Knuth, 1– 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 5
Sorting (Sedgewick, 270– 271; Dale, 586) Two Important Measures of 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 6
Bubble Sort 7
Sequence (Goodrich 2004, 235– 237) 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 8
Sequence (Goodrich 2004, 235– 237) Bubble Sort j j+1 9
Sequence (Goodrich 2004, 235– 237) Bubble Sort j j+1 10
Sequence (Goodrich 2004, 235– 237) Bubble Sort j j+1 11
Sequence (Goodrich 2004, 235– 237) Bubble Sort j j+1 12
Sequence (Goodrich 2004, 235– 237) Bubble Sort j j+1 j First pass through outer loop i = 0 j+1 j j+1 Sorted 13
Sequence (Goodrich 2004, 235– 237) Bubble Sort j j+1 j First pass through outer loop i = 0 for(int i=0; i<A. size()-1; ++i){ for (int j=0; j<A. size()-(i+1); ++j){ j+1 j j+1 } } Sorted 14
Sequence (Goodrich 2004, 235– 237) Bubble Sort j Second pass through outer loop i = 1 j+1 j j+1 Sorted 15
Sequence (Goodrich 2004, 235– 237) Bubble Sort j Third pass through outer loop i = 2 j+1 j j+1 Sorted 16
Sequence (Goodrich 2004, 235– 237) Bubble Sort j j+1 Fourth pass through outer loop i = 3 Sorted 17
Sequence (Goodrich 2004, 235– 237) Bubble Sort Algorithm import java. util. *; public class Demo. Sorting { public static void bubble. Sort(Array. List<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); } What is the Big-Oh? } } public static void main(String[] args) { Array. List<Integer> A = new Array. List<Integer>(); A. add(6274); A. add(4892); A. add(2843); A. add(9523); A. add(1892); bubble. Sort(A); for(Integer i : A) System. out. print(i + " "); } } 18
Sorting in Place – Selection Sort 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. 19
Sorting in Place – Selection Sort 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 Sorted Unsorted 20
Sorting (Sedgewick, 273) Selection Sort import java. util. *; public class Main { public static void selection. Sort(Array. List<Integer> A){ for (int i=0; i<A. size()-1; ++i){ int min. Index = i; for (int j=i+1; j<A. size(); ++j) if (A. get(j) < A. get(min. Index)) min. Index = j; Collections. swap(A, i, min. Index); } } public static void main(String[] args) { Array. List<Integer> A = new Array. List<Integer>(); A. add(6274); A. add(4892); A. add(2843); A. add(9523); A. add(1892); selection. Sort(A); for(Integer i : A) System. out. print(i + " "); } What’s the Big-Oh? } 21
Sorting (Sedgewick, 274; Bentley, 115– 116) Sorting in Place – Insertion Sort The main idea: • • Examine the elements one at a time Insert each into its proper order among the elements already examined 22
Sorting (Sedgewick, 274; Bentley, 115– 116) Sorting in Place – Insertion Sort The main idea: • • Examine the elements one at a time Insert each into its proper order among the elements already examined Unsorted array Sorted Unsorted 23
Sorting (Bentley, 115– 116) Insertion Sort import java. util. *; public class Main { public static void insertion. Sort(Array. List<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) { Array. List<Integer> A = new Array. List<Integer>(); A. add(6274); A. add(4892); A. add(2843); A. add(9523); A. add(1892); insertion. Sort(A); for(Integer i : A) System. out. print(i + " "); } What’s the Big-Oh? 24
Sorting (Knuth, 1– 5) Stable Sorting Items that have equal key values are kept in their original order 25
Sorting Algorithms So far, we’ve studied: Next • Shell sort • Quick sort 26
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/ 27
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. 28
- Slides: 28