CS1101: Programming Methodology

Slides:



Advertisements
Similar presentations
Garfield AP Computer Science
Advertisements

Visual C++ Programming: Concepts and Projects
HST 952 Computing for Biomedical Scientists Lecture 9.
Faster Sorting Methods Chapter 9 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Sorting Chapter Sorting Consider list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending Some O(n.
CS 280 Data Structures Professor John Peterson. Project Questions?
Merge sort, Insertion sort
 2003 Prentice Hall, Inc. All rights reserved. 1 Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
CS 106 Introduction to Computer Science I 03 / 07 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 03 / 2008 Instructor: Michael Eckmann.
Merge sort, Insertion sort. Sorting I / Slide 2 Sorting * Selection sort or bubble sort 1. Find the minimum value in the list 2. Swap it with the value.
Searching and Sorting Arrays
CS 106 Introduction to Computer Science I 10 / 15 / 2007 Instructor: Michael Eckmann.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 9 Searching.
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.
CS1101: Programming Methodology Aaron Tan.
Week 11 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
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.
Chapter 19 Searching, Sorting and Big O
Searching and Sorting Topics Sequential Search on an Unordered File
CHAPTER 09 Compiled by: Dr. Mohammad Omar Alhawarat Sorting & Searching.
Lecture 10: Class Review Dr John Levine Algorithms and Complexity March 13th 2006.
Chapter 10 Strings, Searches, Sorts, and Modifications Midterm Review By Ben Razon AP Computer Science Period 3.
Methods Divide and conquer Programmer-declared methods Prepackaged methods – Java API Software reusability Debug-ability and maintainability AKA functions.
HKOI 2006 Intermediate Training Searching and Sorting 1/4/2006.
Data Structures & Algorithms CHAPTER 4 Searching Ms. Manal Al-Asmari.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 11 Sorting and Searching.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Searching Course Lecture Slides 28 May 2010 “ Some things Man was never.
COMP Recursion, Searching, and Selection Yi Hong June 12, 2015.
CS 61B Data Structures and Programming Methodology July 28, 2008 David Sun.
CS1101: Programming Methodology Aaron Tan.
Merge sort, Insertion sort. Sorting I / Slide 2 Sorting * Selection sort (iterative, recursive?) * Bubble sort.
Data Structures Using C++ 2E Chapter 9 Searching and Hashing Algorithms.
Sorting CS 105 See Chapter 14 of Horstmann text. Sorting Slide 2 The Sorting problem Input: a collection S of n elements that can be ordered Output: the.
Chapter 5 Searching and Sorting. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine the linear search and binary.
Sorting Chapter Sorting Consider list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending Some O(n.
Sorting CS 110: Data Structures and Algorithms First Semester,
1 Section 2.1 Algorithms. 2 Algorithm A finite set of precise instructions for performing a computation or for solving a problem.
CS1101: Programming Methodology
CS1101: Programming Methodology
Lecture #9: Sorting Algorithms خوارزميات الترتيب Dr. Hmood Al-Dossari King Saud University Department of Computer Science 22 April 2012.
Chapter 8 Sorting and Searching Goals: 1.Java implementation of sorting algorithms 2.Selection and Insertion Sorts 3.Recursive Sorts: Mergesort and Quicksort.
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.
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.
CS1020 Data Structures and Algorithms I Lecture Note #14
Sorting and Searching by Dr P.Padmanabham Professor (CSE)&Director
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
The Sorting Methods Lecture Notes 10. Sorts Many programs will execute more efficiently if the data they process is sorted before processing begins. –
CS1101: Programming Methodology
CS1101: Programming Methodology
Reading from a file, Sorting, and a little Searching Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics,
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Sorting.
Searching CSE 103 Lecture 20 Wednesday, October 16, 2002 prepared by Doug Hogan.
Today’s Material Sorting: Definitions Basic Sorting Algorithms
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 4 GEORGE KOUTSOGIANNAKIS Copyright: 2016 Illinois Institute of Technology/George Koutsogiannakis 1.
The Linear and Binary Search and more Lecture Notes 9.
Chapter 16: Searching, Sorting, and the vector Type.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Assignment 5 is posted. Exercise 8 is very similar to what you will be doing with assignment 5. Exam.
Searching and Sorting Searching algorithms with simple arrays
Lecture 14 Searching and Sorting Richard Gesick.
CS1010 Programming Methodology
Lecture 11 Searching and Sorting Richard Gesick.
Searching and Sorting Topics Sequential Search on an Unordered File
Sorting and Searching -- Introduction
Chapter 19 Searching, Sorting and Big O
Presentation transcript:

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