CS1101: Programming Methodology

Slides:



Advertisements
Similar presentations
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 11 Sorting and Searching.
Advertisements

Lesson 8 Searching and Sorting Arrays 1CS 1 Lesson 8 -- John Cole.
Garfield AP Computer Science
CSE Lecture 3 – Algorithms I
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Sorting Sorting is the process of arranging a list of items in a particular order The sorting process is based on specific value(s) Sorting a list of test.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 9A Sorting (Concepts)
Visual C++ Programming: Concepts and Projects
Sorting Algorithms. Motivation Example: Phone Book Searching Example: Phone Book Searching If the phone book was in random order, we would probably never.
Chapter 19: Searching and Sorting Algorithms
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 11: Sorting and Searching  Searching Linear Binary  Sorting.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 11 Sorting and Searching with objects.
Chapter 11 Sorting and Searching. Topics Searching –Linear –Binary Sorting –Selection Sort –Bubble Sort.
Searching Algorithms. Lecture Objectives Learn how to implement the sequential search algorithm Learn how to implement the binary search algorithm To.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 11 Searching and Sorting with Objects.
C++ Plus Data Structures
CS 106 Introduction to Computer Science I 03 / 07 / 2008 Instructor: Michael Eckmann.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 11 Sorting and Searching.
Algorithm Efficiency and Sorting
1 © 2006 Pearson Addison-Wesley. All rights reserved Searching and Sorting Linear Search Binary Search ; Reading p Selection Sort ; Reading p
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 11 Sorting and Searching.
1 Sorting/Searching and File I/O Sorting Searching Reading for this lecture: L&L
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
Sorting and Searching Arrays CSC 1401: Introduction to Programming with Java Week 12 – Lectures 1 & 2 Wanda M. Kunkle.
CHAPTER 7: SORTING & SEARCHING Introduction to Computer Science Using Ruby (c) Ophir Frieder at al 2012.
Describing algorithms in pseudo code To describe algorithms we need a language which is: – less formal than programming languages (implementation details.
Chapter 8 ARRAYS Continued
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--WuChapter Chapter 10 Sorting and Searching.
UNIT 18 Searching and Sorting.
Lecture 5 Searching and Sorting Richard Gesick. The focus Searching - examining the contents of the array to see if an element exists within the array.
1 Programming with Recursion. 2 Recursive Function Call A recursive call is a function call in which the called function is the same as the one making.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 19: Searching and Sorting Algorithms.
Applications of Arrays (Searching and Sorting) and Strings
Chapter 19: Searching and Sorting Algorithms
Lecture 12. Searching Algorithms and its analysis 1.
Data Structures & Algorithms CHAPTER 4 Searching Ms. Manal Al-Asmari.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 11 Sorting and Searching.
Searching. Linear (Sequential) Search Search an array or list by checking items one at a time. Linear search is usually very simple to implement, and.
Array (continue).
LAB#7. Insertion sort In the outer for loop, out starts at 1 and moves right. It marks the leftmost unsorted data. In the inner while loop, in starts.
CS1101: Programming Methodology Aaron Tan.
CSC 211 Data Structures Lecture 13
CS1101: Programming Methodology
Chapter 5 Searching and Sorting. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine the linear search and binary.
Data Structure Introduction.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 11 Searching and Sorting.
Chapter 18: Searching and Sorting Algorithms. Objectives In this chapter, you will: Learn the various search algorithms Implement sequential and binary.
SORTING Chapter 8 CS Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following.
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
Lecture No. 04,05 Sorting.  A process that organizes a collection of data into either ascending or descending order.  Can be used as a first step for.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 9 Searching & Sorting.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
Array Search & Sort (continues). On the fly questions Array declaration: int[] a, b, c; 1. a is an array of integers, b and c are two integers 2. a, b,
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Searching When we maintain a collection of data,
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
Chapter 9 Sorting. The efficiency of data handling can often be increased if the data are sorted according to some criteria of order. The first step is.
Sorting and Searching. Searching  Problem definition: Given a value X, return the index of X in the array if such X exist. Otherwise, return NOT_FOUND(-1).
Searching and Sorting Searching: Sequential, Binary Sorting: Selection, Insertion, Shell.
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
Sorting & Searching Geletaw S (MSC, MCITP). Objectives At the end of this session the students should be able to: – Design and implement the following.
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
Searching and Sorting Searching algorithms with simple arrays
Chapter 9: Sorting and Searching Arrays
CS1010 Programming Methodology
Introduction to Search Algorithms
CS1010 Programming Methodology
Module 8 – Searching & Sorting Algorithms
Chapter 11 Sorting and Searching
Presentation transcript:

CS1101: Programming Methodology

2 CS1101X: Lecture #11 Lecture 11: Sorting and Searching Acknowledgement: Thomas Wu. After you have read and studied this chapter, you should be able to Perform linear and binary search algorithms on small arrays. Determine whether a linear or binary search is more effective for a given situation. Perform selection and bubble sort algorithms. Apply basic sorting algorithms to sort an array of objects.

3 CS1101X: Lecture #11 Searching When we maintain a collection of data, one of the operations we need is a search routine to locate desired data quickly. Here’s the problem statement: Given a value X, return the index of X in the array, if such X exists. Otherwise, return NOT_FOUND (-1). We assume there are no duplicate entries in the array. We will count the number of comparisons the algorithms make to analyze their performance.  The ideal searching algorithm will make the least possible number of comparisons to locate the desired data.  Two separate performance analyses are normally done, one for successful search and another for unsuccessful search.

4 CS1101X: Lecture #11 Search Result number Unsuccessful Search: Successful Search: NOT_FOUND search( 45 ) search( 12 ) 4

5 CS1101X: Lecture #11 Linear Search Search the array from the first to the last position in linear progression. public int linearSearch ( int[] number, int searchValue ) { int loc = 0; while (loc < number.length && number[loc] != searchValue) { loc++; } if (loc == number.length) { // Not found return NOT_FOUND; } else { return loc; // Found, return the position }

6 CS1101X: Lecture #11 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.

7 CS1101X: Lecture #11 Linear Search Performance We analyze the successful and unsuccessful searches separately. We count how many times the search value is compared against the array elements. Successful Search  Best Case: 1 comparison  Worst Case: N comparisons (N is array size) Unsuccessful Search  Best Case = Worst Case: N comparisons

8 CS1101X: Lecture #11 Binary Search If the array is sorted, then we can apply the binary search technique. The basic idea is straightforward. First search the value in the middle position. If X is less than this value, then search the middle of the left half next. If X is greater than this value, then search the middle of the right half next. Continue in this manner. number

9 CS1101X: Lecture #11 Sequence of Successful Search (1/3) search( 44 ) lowhighmid 0808 #1 high low 38 < 44 low = mid+1 = 5 mid 4

10 CS1101X: Lecture #11 Sequence of Successful Search (2/3) search( 44 ) lowhighmid 0808 #1 44 < 77 high = mid-1=5 4 mid 6 highlow 5858 #2

11 CS1101X: Lecture #11 Sequence of Successful Search (3/3) search( 44 ) lowhighmid 0808 #1 44 == #2 6 highlow5 #3 mid 5 Successful Search!!

12 CS1101X: Lecture #11 Sequence of Unsuccessful Search (1/4) search( 45 ) lowhighmid 0808 #1 high low 38 < 45 low = mid+1 = 5 mid 4

13 CS1101X: Lecture #11 Sequence of Unsuccessful Search (2/4) search( 45 ) lowhighmid 0808 #1 45 < 77 high = mid-1=5 4 mid 6 highlow 5858 #2

14 CS1101X: Lecture #11 Sequence of Unsuccessful Search (3/4) search( 45 ) lowhighmid 0808 #1 44 < #2 6 highlow5 #3 mid 5 low = mid+1 = 6

15 CS1101X: Lecture #11 Sequence of Unsuccessful Search (4/4) search( 45 ) lowhighmid 0808 # #2 65 #3 5 highlow 6565 #4 Unsuccessful Search low > high no more elements to search

16 CS1101X: Lecture #11 Binary Search Routine public int binarySearch (int[] number, int searchValue) { int low = 0, high = number.length - 1, mid= (low + high) / 2; while (low <= high && number[mid] != searchValue) { if (number[mid] < searchValue) { low = mid + 1; } else { // number[mid] > searchValue high = mid - 1; } mid = (low + high) / 2; // integer division will truncate } if (low > high) { mid = NOT_FOUND; } return mid; }

17 CS1101X: Lecture #11 Binary Search: Divide and Conquer Binary Search employs the “divide-and-conquer” strategy, usually implemented in recursion. Recursion will be covered later.

18 CS1101X: Lecture #11 Binary Search Performance Successful Search  Best Case: 1 comparison  Worst Case: log 2 N comparisons Unsuccessful Search  Best Case = Worst Case: log 2 N comparisons Since the portion of an array to search is cut into half after every comparison, we compute how many times the array can be divided into halves. After K comparisons, there will be N/2 K elements in the list. We solve for K when N/2 K = 1, deriving K = log 2 N.

19 CS1101X: Lecture #11 Comparing N and log 2 N Performance Array SizeLinear: NBinary: log 2 N

20 CS1101X: Lecture #11 Sorting When we maintain a collection of data, many applications call for rearranging the data in certain order, e.g. arranging Person information in ascending order of age. Here’s the problem statement: Given an array of N integer values, arrange the values into ascending order. We will count the number of comparisons the algorithms make to analyze their performance.  The ideal sorting algorithm will make the least possible number of comparisons to arrange data in a designated order. We will compare different sorting algorithms by analyzing their worst-case performance.

21 CS1101X: Lecture #11 Selection Sort 1. Find the smallest element in the list min first exchange sorted unsorted This is the result of one pass. 2. Exchange the element in the first position and the smallest element. Now the smallest element is in the first position. 3. Repeat Step 1 and 2 with the list having one less element (i.e., the smallest element is discarded from further processing).

22 CS1101X: Lecture #11 Selection Sort Passes sorted Pass # Result AFTER one pass is completed.

23 CS1101X: Lecture #11 Selection Sort Routine public void selectionSort( int[] number ) { int startIndex, minIndex, length, temp; length = number.length; for (startIndex = 0; startIndex <= length-2; startIndex++){ //each iteration of the for loop is one pass minIndex = startIndex; //find the smallest in this pass at position minIndex for (i = startIndex+1; i <= length-1; i++) { if (number[i] < number[minIndex]) minIndex = i; } //exchange number[startIndex] and number[minIndex] temp = number[startIndex]; number[startIndex] = number[minIndex]; number[minIndex] = temp; }

24 CS1101X: Lecture #11 Selection Sort Performance We derive the total number of comparisons by counting the number of times the inner loop is executed. For each execution of the outer loop, the inner loop is executed length – start times. The variable length is the size of the array. Replacing length with N, the array size, the sum is derived as…

25 CS1101X: Lecture #11 Bubble Sort With the selection sort, we make one exchange at the end of one pass. The bubble sort improves the performance by making more than one exchange during its pass. By making multiple exchanges, we will be able to move more elements toward their correct positions using the same number of comparisons as the selection sort makes. The key idea of the bubble sort is to make pairwise comparisons and exchange the positions of the pair if they are out of order.

26 CS1101X: Lecture #11 One Pass of Bubble Sort exchange ok exchange The largest value 90 is at the end of the list.

27 CS1101X: Lecture #11 Bubble Sort Routine public void bubbleSort (int[] number) { int temp, bottom, i; boolean exchanged = true; bottom = number.length - 2; while (exchanged) { exchanged = false; for (i = 0; i <= bottom; i++) { if (number[i] > number[i+1]) { temp = number[i]; //exchange number[i] = number[i+1]; number[i+1] = temp; exchanged = true; //exchange is made } bottom--; }

28 CS1101X: Lecture #11 Bubble Sort Performance In the worst case, the outer while loop is executed N-1 times for carrying out N-1 passes. For each execution of the outer loop, the inner loop is executed bottom+1 times. The number of comparisons in each successive pass is N-1, N-2, …, 1. Summing these will result in the total number of comparisons. So the performances of the bubble sort and the selection sort are approximately equivalent. However, on the average, the bubble sort performs much better than the selection sort because it can finish the sorting without doing all N-1 passes.

29 CS1101X: Lecture #11 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].

30 CS1101X: Lecture #11 Insertion Sort (2/6) Example – pass 1 v Sorted region v Sorted region Where to insert v[1] in the sorted region to its left?

31 CS1101X: Lecture #11 Insertion Sort (3/6) Example – pass 2 v Sorted region v Sorted region Where to insert v[2] in the sorted region to its left?

32 CS1101X: Lecture #11 Insertion Sort (4/6) Example – pass 3 v Sorted region v Sorted region Where to insert v[3] in the sorted region to its left?

33 CS1101X: Lecture #11 Insertion Sort (5/6) Example – pass 4 v Sorted region v Sorted region Where to insert v[4] in the sorted region to its left? Sort completed.

34 CS1101X: Lecture #11 Insertion Sort (6/6) An array of n elements requires n-1 passes in insertion sort. Try to code insertion sort yourself.  In inserting the element being examined into the sorted region, try to avoid using swaps. Instead, shift the affected elements to the right one place to make way for the element to be inserted.

35 CS1101X: Lecture #11 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?

36 CS1101X: Lecture #11 Problem Statement Add the sorting capability to the AddressBook class from Chapter 10. The new AddressBook class will include a method that sort Person objects in alphabetical order of their names or in ascending order of their ages.

37 CS1101X: Lecture #11 Overall Plan Instead of going through the development steps, we will present three different implementations. The three versions are named AddressBookVer1, AddressBookVer2, and AddressBookVer3. (AddressBookVer3 uses Map which is not covered in this module.) These classes will implement the AddressBook interface.

38 CS1101X: Lecture #11 The AddressBook Interface interface AddressBook { public void add(Person newPerson); public boolean delete(String searchName); public Person search(String searchName); public Person[ ] sort (int attribute); }

39 CS1101X: Lecture #11 AddressBookVer1 Design We use an array of Person objects We provide our own sorting method in the AddressBookVer1 class We define the Person class so we can compare Person objects by name or by age

40 CS1101X: Lecture #11 Comparing Person Objects First, we need to determine how to compare Person objects The following does not make sense: Person p1 = …; Person p2 = …; if ( p1 < p2 ) { … }

41 CS1101X: Lecture #11 Modify the Person Class Modify the Person class to include a variable that tells which attribute to compare. class Person { … public static final int NAME = 0; public static final int AGE = 1; public static int compareAttribute = NAME; … public static void setCompareAttribute(int attribute) { compareAttribute = attribute; } … }

42 CS1101X: Lecture #11 The compareTo Method To compare Person objects, first set the comparison attribute and then call the compareTo Method. Person.setCompareAttribute (Person.NAME); int compResult = p1.compareTo(p2); if (compResult < 0) { //p1 “less than” p2 } else if (compResult == 0) { //p1 “equals” pw } else { //p2 “greater than” p2 } public int compareTo(Person p) { int compResult; if ( comparisonAttribute == AGE ) { int p2age = p.getAge(); if ( this.age < p2age ) { compResult = LESS; } … } else { //compare Name String p2Name = p.getName(); compResult = this.name. compareTo(p2Name); } return compResult; }

43 CS1101X: Lecture #11 The sort Method public Person[ ] sort (int attribute) { Person[ ] sortedList = new Person[count]; Person p1, p2, temp; //copy references to sortedList for (int i = 0; i < count; i++) { sortedList[i] = entry[i]; } //Set the comparison attribute Person.setCompareAttribute(attribute); //begin the bubble sort on sortedList … } return sortedList; }

44 CS1101X: Lecture #11 The Use of sortedList in the sort Method Before Sorting After Sorting This is the age value Return this array

45 CS1101X: Lecture #11 AddressBookVer1 Code Directory: Chapter11 Source File: AddressBookVer1.java Directory: Chapter11 Source File: AddressBookVer1.java

46 CS1101X: Lecture #11 AddressBookVer2 Design We use the java.util.Arrays class to sort an array of Person objects The Person class does not include any comparison methods. Instead, we implement the Comparator interface. We can define the implementation class so we can compare Person objects using any combination of their attributes  For example, we can define a comparator that compares Person objects by using both name and age

47 CS1101X: Lecture #11 The AgeComparator Class This class compares the age attributes of Person objects class AgeComparator implements Comparator { private final int LESS = -1; private final int EQUAL = 0; private final int MORE = 1; public int compare(Object p1, Object p2) { int comparisonResult; int p1age = ((Person)p1).getAge( ); int p2age = ((Person)p2).getAge( ); if (p1age < p2age) { comparisonResult = LESS; } else if (p1age == p2age) { comparisonResult = EQUAL; } else { assert p1age > p2age; comparisonResult = MORE; } return comparisonResult; }

48 CS1101X: Lecture #11 The NameComparator Class This class compares the age attributes of Person objects Because the name attribute is a string we can use the compareTo method of the String class class NameComparator implements Comparator { public int compare(Object p1, Object p2) { String p1name = ((Person)p1).getName( ); String p2name = ((Person)p2).getName( ); return p1name.compareTo(p2name); }

49 CS1101X: Lecture #11 The sort Method We use the sort method of the java.util.Arrays class public Person[ ] sort ( int attribute ) { if (!(attribute == Person.NAME || attribute == Person.AGE) ) { throw new IllegalArgumentException( ); } Person[ ] sortedList = new Person[ count ]; //copy references to sortedList for (int i = 0; i < count; i++) { sortedList[i] = entry[i]; } Arrays.sort(sortedList, getComparator(attribute)); return sortedList; }

50 CS1101X: Lecture #11 AddressBookVer2 Code Directory: Chapter11 Source File: AddressBookVer2.java Directory: Chapter11 Source File: AddressBookVer2.java

51 CS1101X: Lecture #11 End of Lecture #11