Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS1101: Programming Methodology

Similar presentations


Presentation on theme: "CS1101: Programming Methodology"— Presentation transcript:

1 CS1101: Programming Methodology http://www.comp.nus.edu.sg/~cs1101/ http://www.comp.nus.edu.sg/~cs1101/

2 Week 10: Sorting and Searching  Previous lecture:  Chapter 10: Arrays and Collections (cont’d)  Chapter 8: Exceptions  This week:  Chapter 11: Sorting and Searching  Some past-years’ exam questions  Next week:  Chapter 12: File Input and Output © CS1101 (AY2009-2010 Semester 1)Week10 - 2

3 Chapter 11 Sorting and Searching Let’ go over Thomas Wu’s slides now … We will skip Heapsort as it is not in the syllabus © CS1101 (AY2009-2010 Semester 1)Week10 - 3

4 Design Issue: Cohesion Note that the linearSearch() method does not display the subscript of the matching element in the array, nor does it even display any message on whether the search value is found or not. Instead, it returns the subscript of the first matching element, or –1 if the key is not found. This follows the principle of cohesion  A method should do a single task.  In this case, it should leave it to the caller to decide if any message needs to be displayed. © CS1101 (AY2009-2010 Semester 1)Week10 - 4

5 Binary Search: Divide and Conquer Binary Search employs the “divide-and-conquer” strategy, usually implemented in recursion. Recursion is not in the syllabus of CS1101. It will be covered in CS1102. © CS1101 (AY2009-2010 Semester 1)Week10 - 5

6 Bubble Sort (1/2) The Bubble Sort method in the textbook is an enhanced version. The original Bubble Sort algorithm, less efficient but very easy to code, is shown in the next page. © CS1101 (AY2009-2010 Semester 1)Week10 - 6

7 Bubble Sort (2/2) © CS1101 (AY2009-2010 Semester 1)Week10 - 7 // Bubble Sort routine on an integer array numbers. // Sort numbers in ascending order. public static void bubbleSort(int[] numbers) { int temp; for (int last = numbers.length-1; last > 0; last--) { for (int i = 0; i < last; i++) { if (numbers[i] > numbers[i+1]) { // swap numbers[i] with numbers[i+1] temp = numbers[i]; numbers[i] = numbers[i+1]; numbers[i+1] = temp; }

8 Insertion Sort (1/6) Algorithm basis  On pass i, Insert v[i] into the correct position in the sorted region to its left: v[0]…v[i-1]. Example – pass 1 (assume n = 5)  Compare v[1] with v[0]  If v[1] < v[0], move v[1] to the front of v[0] (shifting needed)  The sorted region now consists of two elements: v[0] and v[1]. © CS1101 (AY2009-2010 Semester 1)Week10 - 8

9 Insertion Sort (2/6) Example: pass 1 © CS1101 (AY2009-2010 Semester 1)Week10 - 9 v 0 1 2 3 4 105716 Sorted region v 510716 Sorted region Where to insert v[1] in the sorted region to its left?

10 Insertion Sort (3/6) Example: pass 2 © CS1101 (AY2009-2010 Semester 1)Week10 - 10 v 0 1 2 3 4 510716 Sorted region v 571016 Sorted region Where to insert v[2] in the sorted region to its left?

11 Insertion Sort (4/6) Example: pass 3 © CS1101 (AY2009-2010 Semester 1)Week10 - 11 v 0 1 2 3 4 571016 Sorted region v 157106 Sorted region Where to insert v[3] in the sorted region to its left?

12 Insertion Sort (5/6) Example: pass 4 (final pass) © CS1101 (AY2009-2010 Semester 1)Week10 - 12 v 0 1 2 3 4 157106 Sorted region v 156710 Sorted region Where to insert v[4] in the sorted region to its left? Sort completed.

13 Insertion Sort (6/6) An array of n elements requires n-1 passes in insertion sort. Try to code insertion sort yourself.  When inserting the element being examined into the sorted region, try to avoid using swaps (it’s inefficient).  Instead, shift the affected elements to the right one place to make way for the element to be inserted. © CS1101 (AY2009-2010 Semester 1)Week10 - 13

14 Stable Sorts A stable sort is one where the relative ordering of elements with the same value is preserved after sorting.  Two elements having the same key appear in the same order in the sorted sequence as they did in the original sequence. Example:  Stable sort: Before sorting:623 a 83 b 59 After sorting: 23 a 3 b 5689  Unstable sort: Before sorting:623 a 83 b 59 After sorting: 23 b 3 a 5689 Which of the three basic sorts – bubble sort, selection sort, insertion sort – is/are stable? © CS1101 (AY2009-2010 Semester 1)Week10 - 14

15 Take-home Lab #5 Any questions? Deadline: 26 October 2009, Monday, 23:59hr. © CS1101 (AY2009-2010 Semester 1)Week10 - 15

16 Some past-years’ exam questions You will be given some questions during lecture. © CS1101 (AY2009-2010 Semester 1)Week10 - 16

17 Summary for Today Searching algorithms Linear (sequential) search Binary search Basic sorting algorithms Selection sort Bubble sort Insertion sort © CS1101 (AY2009-2010 Semester 1)Week10 - 17

18 Announcements/Things-to-do Take-home lab #5 Deadline: 26 October 2009, Monday, 23:59hr To prepare for next lecture  Read Chapter 12 File Input and Output and the PowerPoint file before you come for lecture. Sit-in Lab #3  To be conducted during this week’s discussion session.  Topics tested include everything up to Chapter 10 (but exclude Chapter 8).  Sit-in lab #3 constitute 5% of your final grade. © CS1101 (AY2009-2010 Semester 1)Week10 - 18

19 End of File © CS1101 (AY2009-2010 Semester 1)Week10 - 19


Download ppt "CS1101: Programming Methodology"

Similar presentations


Ads by Google