CS1101: Programming Methodology
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 (AY Semester 1)Week10 - 2
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 (AY Semester 1)Week10 - 3
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 (AY Semester 1)Week10 - 4
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 (AY Semester 1)Week10 - 5
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 (AY Semester 1)Week10 - 6
Bubble Sort (2/2) © CS1101 (AY Semester 1)Week // 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; }
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 (AY Semester 1)Week10 - 8
Insertion Sort (2/6) Example: pass 1 © CS1101 (AY Semester 1)Week v Sorted region v Sorted region Where to insert v[1] in the sorted region to its left?
Insertion Sort (3/6) Example: pass 2 © CS1101 (AY Semester 1)Week v Sorted region v Sorted region Where to insert v[2] in the sorted region to its left?
Insertion Sort (4/6) Example: pass 3 © CS1101 (AY Semester 1)Week v Sorted region v Sorted region Where to insert v[3] in the sorted region to its left?
Insertion Sort (5/6) Example: pass 4 (final pass) © CS1101 (AY Semester 1)Week v Sorted region v Sorted region Where to insert v[4] in the sorted region to its left? Sort completed.
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 (AY Semester 1)Week
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 (AY Semester 1)Week
Take-home Lab #5 Any questions? Deadline: 26 October 2009, Monday, 23:59hr. © CS1101 (AY Semester 1)Week
Some past-years’ exam questions You will be given some questions during lecture. © CS1101 (AY Semester 1)Week
Summary for Today Searching algorithms Linear (sequential) search Binary search Basic sorting algorithms Selection sort Bubble sort Insertion sort © CS1101 (AY Semester 1)Week
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 (AY Semester 1)Week
End of File © CS1101 (AY Semester 1)Week