Presentation is loading. Please wait.

Presentation is loading. Please wait.

Upcoming schedule F 11/11 hw#4 due F 11/20 midterm #2.

Similar presentations


Presentation on theme: "Upcoming schedule F 11/11 hw#4 due F 11/20 midterm #2."— Presentation transcript:

1 Upcoming schedule F 11/11 hw#4 due F 11/20 midterm #2

2 Simple Sorting (review) Copyright Prentice Hall (with additions / modifications by Evan Korth)

3 Sorting F Sorting data is one of the most important computing applications. F Examples: –Banks sort all checks by account number in order to prepare individual bank statements. –Telephone companies sort accounts by last name, first name in order to create phone books. F Sorting data has attracted intense research efforts in computer science. F In this lecture, we explore the two of the simplest known sorting algorithms.

4 Selection Sort Applet F Before examining any code, let us first try out a Java applet for Selection Sort. F The Applet lets you step through each step in the algorithm, so that you can gain an intuitive sense of how it works. F Just go to: http://math.hws.edu/TMCM/java/xSortLab/ –Click the “Start Again” Button to get a new set of random numbers. –Click “Go” to sort. –Click “Step” to step through the algorithm.

5 Example 5.12: (Cont) Using Arrays in Sorting int[] myList = {2, 9, 5, 4, 8, 1, 6}; // Unsorted Find the largest element in myList and swap it with the last element in myList. 2, 9, 5, 4, 8, 1, 6 => 2, 6, 5, 4, 8, 1, 9 (size = 7) 2, 6, 5, 4, 8, 1 => 2, 6, 5, 4, 1, 8 (size = 6) 2, 6, 5, 4, 1 => 2, 1, 5, 4, 6 (size = 5) 2, 1, 5, 4 => 2, 1, 4, 5 2, 1, 4 => 2, 1, 4, 2, 1 => 1, 2 Sort it to produce 1, 2, 4, 5, 6, 8, 9

6 Example 5.12 Using Arrays in Sorting  Objective: Use the selectionSort method to write a program that will sort a list of double floating-point numbers. int[] myList = {2, 9, 5, 4, 8, 1, 6}; // Unsorted Sort it to produce 1, 2, 4, 5, 6, 8, 9 2, 9, 5, 4, 8, 1, 6 SelectionSortRun

7 Bubble Sort F Bubble sort is another option for sorting a list of numbers. F It’s called bubble sort because –larger values gradually “bubble” their way upward to the end of the array like air bubbles rising in water. F The technique is to make several passes through the array. –On each pass, pairs of elements are compared. –If the pair is in increasing order, we leave the values as they are. –If the pair is in decreasing order, we swap the values.

8 Bubble Sort Applet F Before examining any code, let us first try out a Java applet for Bubble Sort. F The Applet lets you step through each step in the algorithm, so that you can gain an intuitive sense of how it works. F Just go to: http://math.hws.edu/TMCM/java/xSortLab/ –Click the “Start Again” Button to get a new set of random numbers. –Click “Go” to sort. –Click “Step” to step through the algorithm.

9 Advantages / Disadvantages of Bubble Sort and Selection Sort F The main advantage of these two sorting algorithms is that it is easy to program. F The main disadvantage is that it is very slow. F Much work in computer science has been geared towards creating more efficient sorting algorithms. F For example: –Merge Sort –Bi-directional Bubble Sort –Quick Sort F For another quick demo, check out: http://java.sun.com/applets/jdk/1.1/demo/SortDem o/example1.html http://java.sun.com/applets/jdk/1.1/demo/SortDem o/example1.html

10 Heap Sort F How can we use a heap to sort an array?

11 Heap Sort F Take our unsorted data –Build a heap from it u O(n) –DeleteMin for each element u O(n * log n) –Heap Sort is O (n log n)


Download ppt "Upcoming schedule F 11/11 hw#4 due F 11/20 midterm #2."

Similar presentations


Ads by Google