Chapter 10 Strings, Searches, Sorts, and Modifications Midterm Review By Ben Razon AP Computer Science Period 3.

Slides:



Advertisements
Similar presentations
Garfield AP Computer Science
Advertisements

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Topic 24 sorting and searching arrays "There's nothing in your head the sorting hat can't see. So try me on and I will tell you where you ought to be."
Chapter 9: Searching, Sorting, and Algorithm Analysis
Visual C++ Programming: Concepts and Projects
CompSci Searching & Sorting. CompSci Searching & Sorting The Plan  Searching  Sorting  Java Context.
Chapter 6: Arrays Java Software Solutions for AP* Computer Science
CS0007: Introduction to Computer Programming Array Algorithms.
CPS120: Introduction to Computer Science Searching and Sorting.
HST 952 Computing for Biomedical Scientists Lecture 9.
Searches & Sorts V Deena Engel’s class Adapted from W. Savitch’s text An Introduction to Computers & Programming.
Computer Programming Sorting and Sorting Algorithms 1.
Searching and Sorting Arrays
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.
Chapter 3: The Fundamentals: Algorithms, the Integers, and Matrices
Week 11 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Outline ArrayList Searching Sorting Copyright © 2012 Pearson Education, Inc.
Array Processing Simple Program Design Third Edition A Step-by-Step Approach 7.
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 09 Compiled by: Dr. Mohammad Omar Alhawarat Sorting & Searching.
Chapter 10 Applications of Arrays and Strings. Chapter Objectives Learn how to implement the sequential search algorithm Explore how to sort an array.
Chapter 11 Arrays Continued
Chapter 8 Searching and Sorting Arrays Csc 125 Introduction to C++ Fall 2005.
 2006 Pearson Education, Inc. All rights reserved Searching and Sorting.
Array Processing.
111 © 2002, Cisco Systems, Inc. All rights reserved.
1 Today’s Material Iterative Sorting Algorithms –Sorting - Definitions –Bubble Sort –Selection Sort –Insertion Sort.
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.
CS1101: Programming Methodology
ArrayList Class An ArrayList is an object that contains a sequence of elements that are ordered by position. An ArrayList is an object that contains a.
Sorting Dr. Yingwu Zhu. 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.
Searching & Sorting Programming 2. Searching Searching is the process of determining if a target item is present in a list of items, and locating it A.
Sorting and Searching. Selection Sort  “Search-and-Swap” algorithm 1) Find the smallest element in the array and exchange it with a[0], the first element.
Chapter 8 Sorting and Searching Goals: 1.Java implementation of sorting algorithms 2.Selection and Insertion Sorts 3.Recursive Sorts: Mergesort and Quicksort.
ALGORITHMS.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
Composition When one class contains an instance variable whose type is another class, this is called composition. Instead of inheritance, which is based.
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.
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Elementary Data Structures Array Lists Array Lists Dale.
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
8.1 8 Algorithms Foundations of Computer Science  Cengage Learning.
COMPUTER PROGRAMMING 2 ArrayLists. Objective/Essential Standard Essential Standard 3.00Apply Advanced Properties of Arrays Essential Indicator 3.02 Apply.
Data Structure and Algorithms
CPS120: Introduction to Computer Science Sorting.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Basic Sorting Algorithms Dr. Yingwu Zhu. Sorting Problem Consider list x 1, x 2, x 3, … x n Goal: arrange the elements of the list in order Ascending.
Arrays, Link Lists, and Recursion Chapter 3. Sorting Arrays: Insertion Sort Insertion Sort: Insertion sort is an elementary sorting algorithm that sorts.
Chapter 16: Searching, Sorting, and the vector Type.
Sort Algorithm.
Sections 10.5 – 10.6 Hashing.
Searching and Sorting Algorithms
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
Lecture 14 Searching and Sorting Richard Gesick.
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
Binary Search Back in the days when phone numbers weren’t stored in cell phones, you might have actually had to look them up in a phonebook. How did you.
Bubble, Selection & Insertion sort
Lecture 11 Searching and Sorting Richard Gesick.
MSIS 655 Advanced Business Applications Programming
Standard Version of Starting Out with C++, 4th Edition
24 Searching and Sorting.
Principles of Computing – UFCFA3-30-1
Topic 24 sorting and searching arrays
Searching and Sorting Arrays
Principles of Computing – UFCFA3-30-1
Arrays.
Presentation transcript:

Chapter 10 Strings, Searches, Sorts, and Modifications Midterm Review By Ben Razon AP Computer Science Period 3

Table of Contents Strings – 3 Searching – 5 –Binary Search – 6 Sorts – 7 Insertions and Removals – 11 Arrays of Objects – 12 ArrayList – 13 Wrappers – 16 Review Questions – 17

Strings There are many predefined methods for strings that allow programmers to manipulate strings more easily. A table of these functions can be found on page 317 of the textbook. **There are no mutator functions for strings because they are immutable objects**

Searching Sometimes you need to find a target element in a collection. Searching is the general term for this. Two main searches: –Linear Search Simple, examines elements in order until target element is found or collection is exhausted –Binary Search More Complicated but more efficient

Binary Search Unlike linear search, binary search uses the divide and conquer approach which is more efficient. Each time the middle term is examined to see if it is the target term. If it is not the collection is split into two halves and the appropriate half is examined in the same way. Binary search requires an ordered list and elements that implement the comparable interface.

Sorts In order to use binary search, the collection must be sorted. This process of placing items stored in a linear collection in order is called sorting. There are several sorting algorithms that you must know for the midterm and AP test.

Selection Sort Basically find the smallest value and replace it with the first element and then continue doing the same thing with the rest of the elements. Really easy to write the code for, but inefficient (think about how you sort things in real life) Code can be found on pg 325

Bubble Sort Bubble sort works by swapping two adjacent elements if they are out of order with respect to each other. This causes the last element to bubble to the end of the array after every pass. If a pass doesn't interchange any items, the array must be sorted, which allows for the partial ordering of the array to reduce the number of passes.

Insertion Sort During each pass the kth element is inserted into the correct spot in the previously sorted elements. This is like how most people sort their hand when playing cards. This sort takes k passes to sort an array of k items and takes advantage of partial ordering of the array.

Insertions and Removals Arrays have a fixed size, but a variable logical size. You must compare the sizes before trying to insert or remove. When inserting shift the last item one further. And then do the same for the previous spot until you get to the target index. When removing move the next element one back and then repeat with the next element until the last element is reached.

Arrays of Objects An array can contain objects that aren't all of the exact same type. When using an array of objects you must cast the objects to their type before using class specific methods. Using the instanceof method allows you to check what type of object is in the array.

ArrayList Arrays have a fixed size which poses many problems to computer programmers To combat this the ArrayList class was made which basically makes an array with a dynamic size. The ArrayList class is found in java.util.ArrayList

ArrayList Continued The ArrayList also automatically shifts elements when adding or removing elements using the add and remove methods. For a complete list of ArrayList methods check page 353 of the textbook.

Sample Code

Wrapper Class ArrayList only works with objects not primitive types like int. An easy work around for this is to use wrapper classes. These classes function like their corresponding primitive data type, but are objects and immutable. For a list of wrapper classes and their unwrapping methods check page 356.

Multiple Choice Question 1 Identify the following code:

a) Selection Sort b) Insertion Sort c) Bubble Sort d) Binary Sort e) None of the Above Answer Choices

And the Answer is….!!! a) Selection Sort b) Insertion Sort c) Bubble Sort d) Binary Sort e) None of the Above The code finds the smallest value and then puts it in the front of the array. It then repeats this process for the rest of the elements. This is how selection sort works.

Question Number 2 If you performed an Insertion sort and outputted the numbers after each change what would the output look like with the following input: ? a b c d e. None of these

If you performed an Insertion sort and outputted the numbers after each change what would the output look like with the following input: ? a b c d e. None of these Explanation: Insertion sort places the Kth term in the right spot within the first k terms. B does this. And the Answer is…!

Code Writing When working with arrays it is sometimes useful to have structures capable of dynamic resizing. Write the code for the constructor, add method, and remove method for this class called dynamicArray. You can assume that all elements will be of type integer. The add method should allow you to insert an element at any index or after the last index. This should change the size of the array and shift the elements. The remove method should be capable of removing an element at a certain index, and also resize and shift the array. The constructer should create dynamicArray based on an integer array that is passed in.