The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 19, 2005.

Slides:



Advertisements
Similar presentations
Selection Sort. Selection Sort Algorithm (ascending) 1.Find smallest element (of remaining elements). 2.Swap smallest element with current element (starting.
Advertisements

Chapter 3 Brute Force Brute force is a straightforward approach to solving a problem, usually directly based on the problem’s statement and definitions.
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.
Chapter 9: Searching, Sorting, and Algorithm Analysis
Chapter 6: Arrays Java Software Solutions for AP* Computer Science
Copyright © 2012 Pearson Education, Inc. Chapter 8: Searching and Sorting Arrays.
CS0007: Introduction to Computer Programming Array Algorithms.
HST 952 Computing for Biomedical Scientists Lecture 9.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 8, 2005.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 6, 2005.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 14, 2005.
COMP 10 Introduction to Programming Mr. Joshua Stough October 29, 2007.
Sorting and Searching. Searching List of numbers (5, 9, 2, 6, 3, 4, 8) Find 3 and tell me where it was.
COMP 14 Introduction to Programming Mr. Joshua Stough April 4, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.
COMP 14 Introduction to Programming Miguel A. Otaduy June 4, 2004.
Searching Arrays. COMP104 Lecture 22 / Slide 2 Unordered Linear Search * Search an unordered array of integers for a value and return its index if the.
Chapter 8 Search and Sort Asserting Java ©Rick Mercer.
CS 106 Introduction to Computer Science I 03 / 07 / 2008 Instructor: Michael Eckmann.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 11 Sorting and Searching.
Searching and Sorting Arrays
COMP 14 Introduction to Programming Miguel A. Otaduy June 1, 2004.
CS 106 Introduction to Computer Science I 03 / 17 / 2008 Instructor: Michael Eckmann.
Chapter 8 ARRAYS Continued
Seattle Preparatory School Advanced Placement Computer Science Seattle Preparatory School Advanced Placement Computer Science LESSON 62 FEBRUARY 12, 2015.
Week 11 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
COMP102 Lab 131 COMP 102 Programming Fundamentals I Presented by : Timture Choi.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8: Searching and Sorting Arrays.
Copyright © 2012 Pearson Education, Inc. Chapter 8: Searching and Sorting Arrays.
ITEC 2620A Introduction to Data Structures
(C) 2010 Pearson Education, Inc. All rights reserved. Java How to Program, 8/e.
Chapter 19 Searching, Sorting and Big O
Today  Table/List operations  Parallel Arrays  Efficiency and Big ‘O’  Searching.
Computer Science 101 Fast Searching and Sorting. Improving Efficiency We got a better best case by tweaking the selection sort and the bubble sort We.
Data Structures & Algorithms CHAPTER 4 Searching Ms. Manal Al-Asmari.
Chapter 8 Searching and Sorting Arrays Csc 125 Introduction to C++ Fall 2005.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 8: Searching and Sorting Arrays.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
COMP Recursion, Searching, and Selection Yi Hong June 12, 2015.
1 Today’s Material Iterative Sorting Algorithms –Sorting - Definitions –Bubble Sort –Selection Sort –Insertion Sort.
Sorting – Insertion and Selection. Sorting Arranging data into ascending or descending order Influences the speed and complexity of algorithms that use.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use by MSU Dept. of Computer Science.
Week # 2: Arrays.  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently  Types of data.
Searching CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
CSCI 51 Introduction to Programming March 12, 2009.
Review 1 Arrays & Strings Array Array Elements Accessing array elements Declaring an array Initializing an array Two-dimensional Array Array of Structure.
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.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 4 Introduction.
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
1 Chapter 9 Arrays Java Programming from Thomson Course Tech, adopted by kcluk.
CSCI 51 Introduction to Programming March 10, 2009.
1 Chapter 9 Arrays Java Programming from Thomson Course Tech, adopted by kcluk.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
1 compares each element of the array with the search key. works well for small arrays or for unsorted arrays works for any table slow can put more commonly.
Lesson 9 Arrays. Miscellaneous About Arrays An array is an object. Because of this, the array name is a reference variable Therefore, in order to start.
Chapter 9: Sorting and Searching Arrays
Introduction to Search Algorithms
Chapter 9: Searching, Sorting, and Algorithm Analysis
Algorithm design and Analysis
MSIS 655 Advanced Business Applications Programming
Searching and Sorting 1-D Arrays
Search,Sort,Recursion.
Principles of Computing – UFCFA3-30-1
Search,Sort,Recursion.
Chapter 19 Searching, Sorting and Big O
Principles of Computing – UFCFA3-30-1
Presentation transcript:

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 19, 2005

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 2 Today Review arrays Searching arrays for a particular value Sorting arrays

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 3 Exercises 1. Find Sum and Average of Array 2. Determine Largest and Smallest Elements in Array 01 23

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 4 Example double[] sale={3.5, 4.6, 5.2, 3.8}; double sum = 0; double average; Find Sum and Average of Array 01 23

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 5 Example double[] sale={3.5, 4.6, 5.2, 3.8}; double sum = 0; for(int ind = 0; ind < sale.length; ind++) { sum = sum + sale[ind]; } double average; if(sale.length != 0) average = sum / sale.length; else average = 0.0; Find Sum and Average of Array 01 23

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 6 Example double[] sale={3.5, 4.6, 5.2, 3.8}; int maxIndex = 0, minIndex = 0; double largestSale; double smallestSale; Determining Largest/Smallest Element in Array 01 23

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 7 Example double[] sale={3.5, 4.6, 5.2, 3.8}; int maxIndex = 0, minIndex = 0; for (int ind = 1; ind < sale.length; ind++) { if (sale[ind] > sale[maxIndex]) maxIndex = ind; else if (sale[ind] < sale[minIndex]) minIndex = ind; } double largestSale = sale[maxIndex]; double smallestSale = sale[minIndex]; Determining Largest/Smallest Element in Array 01 23

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 8 Searching Arrays Find one/several particular element(s) in an array of many elements Complexity (How Long To Search?) ♦ find a parking space - linear ♦ look up a word in a dictionary - complex 500K+ words in Oxford Dictionary search - very complex over 3 trillion web pages

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 9 Time Complexity Important feature in Computer Science research: ♦ How long does an algorithm take to complete? ♦ Given an input (e.g. an array) of size n, how long does it take for the algorithm (e.g. search for a particular value) to complete? Linear algorithm: time = k 1 * n Quadratic algorithm: time = k 2 * n 2 etc.

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 10 Linear Searching Algorithm: ♦ Get a test value and a list of values list can be ordered or unordered ♦ loop through the list repeatedly ask: Is this a match? quit when the answer is yes (use break statement) ♦ if you finish all items, there is no match Inefficient ♦ worst time to search is the length of the list Relatively easy to program

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 11 Example int[] list={3, 6, 27, 8, 33, 54, 23}; int foundAt = -1, element = 33; Linear Search 01 23

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 12 Example int[] list={3, 6, 27, 8, 33, 54, 23}; int foundAt = -1, element = 33; for(int i=0; i<list.length; i++) { if(list[i] == element) { foundAt = i; break; } } Linear Search 01 23

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 13 Array of Objects Declare array of Student objects (ref. variables) Instantiate array of size 10 Instantiate each of the Student objects ♦ Ask for age (int) and name (String) ♦ Instantiate object

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 14 Example Student[] students; students = new Student[10]; for(int i = 1; i < students.length; i++) { //get int age //get String name students[i]=new Student(name, age); } Array of Student objects 01 23

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 15 Example int foundAt=-1; String name=“Mark”; for(int i = 0; i < students.length; i++) { if(students[i].getName().equals(name)) { fountAt=i; break; } Search for student Mark 01 23

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 16 Binary Search Requires ordered (sorted) list Set searchRange to the entire list Repeat: ♦ pick a “test value” in the middle of searchRange ♦ if test value == value searching for Stop! ♦ if test value > value searching for searchRange = lower half of searchRange ♦ if test value < value searching for searchRange = upper half of searchRange

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 17 Example Looking for 46 Trial

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 18 Sorting Sort students by birth date Get a group of 5 students Another student will sort them by birth date Analyze the sorting strategy ♦ Can we devise an algorithm following that strategy? Goal: sort following a methodology ♦ We should be able to write it as an algorithm and then program it Demonstrate selection sort Write algorithm

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 19 Selection Sort General Algorithm Scan the list to find the smallest value Swap that value with the value in the first position in the list Scan rest of list to find the next smallest value Swap that value with the value in the second position in the list And so on, until you get to the end of the list

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 20 Selection Sort Methods Scan the list to find the smallest value Swap that value with the value in the first position in the list Scan rest of list to find the next smallest value Swap that value with the value in the second position in the list And so on, until you get to the end of the list loop

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 21 Selection Sort Sorts in ascending order Can be changed to sort in descending order ♦ look for max instead of min

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 22 Homework 6 Read/write files Array of ints Array of Participant objects Next: ♦ Sort array of ints ♦ Sort array of participants ♦ Extra credit: group participants

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 23 Tomorrow Java applets HTML GUIs Reading Assignment: ♦ Chapter 6 pp (pp in old book) ♦ skim Chapter 13 pp (pp in old book)