ArrayList is a class that implements the List interface. The List interface is a blueprint for its “implementor” classes. There is another implementor.

Slides:



Advertisements
Similar presentations
Introduction to Algorithms Quicksort
Advertisements

Algorithms Analysis Lecture 6 Quicksort. Quick Sort Divide and Conquer.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Practice Quiz Question
Fundamentals of Python: From First Programs Through Data Structures
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.
Chapter 9: Searching, Sorting, and Algorithm Analysis
Outline Polymorphic References Polymorphism via Inheritance Polymorphism via Interfaces Sorting Searching Event Processing Revisited File Choosers and.
Chapter 6: Arrays Java Software Solutions for AP* Computer Science
SORTING ROUTINES. OBJECTIVES INTRODUCTION BUBBLE SORT SELECTION SORT INSERTION SORT QUICK SORT MERGE SORT.
CPS120: Introduction to Computer Science Searching and Sorting.
Efficient Sorts. Divide and Conquer Divide and Conquer : chop a problem into smaller problems, solve those – Ex: binary search.
ISOM MIS 215 Module 7 – Sorting. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.
Chapter 19: Searching and Sorting Algorithms
Sorting Chapter 9.
Fundamentals of Algorithms MCS - 2 Lecture # 16. Quick Sort.
Sorting Algorithms What is it: An algorithm that puts the elements of a list in a certain order.
Algorithm An algorithm is a step-by-step set of operations to be performed. Real-life example: a recipe Computer science example: determining the mode.
Ver. 1.0 Session 5 Data Structures and Algorithms Objectives In this session, you will learn to: Sort data by using quick sort Sort data by using merge.
1 Sorting Algorithms (Part II) Overview  Divide and Conquer Sorting Methods.  Merge Sort and its Implementation.  Brief Analysis of Merge Sort.  Quick.
Chapter 11 Sorting and Searching. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Examine the linear search and.
General Computer Science for Engineers CISC 106 James Atlas Computer and Information Sciences 10/23/2009.
CHAPTER 11 Sorting.
1 Sorting/Searching and File I/O Sorting Searching Reading for this lecture: L&L
Aalborg Media Lab 15-Jul-15 Polymorphism Lecture 12 Chapter 9.
1 Data Structures and Algorithms Sorting. 2  Sorting is the process of arranging a list of items into a particular order  There must be some value on.
Recursion, Complexity, and Searching and Sorting By Andrew Zeng.
Week 11 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Recursion, Complexity, and Sorting By Andrew Zeng.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 19: Searching and Sorting Algorithms.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
 2005 Pearson Education, Inc. All rights reserved Searching and Sorting.
 Pearson Education, Inc. All rights reserved Searching and Sorting.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
 2006 Pearson Education, Inc. All rights reserved Searching and Sorting.
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.
© 2006 Pearson Addison-Wesley. All rights reserved10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
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.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 26 Sorting.
© 2006 Pearson Addison-Wesley. All rights reserved10 B-1 Chapter 10 (continued) Algorithm Efficiency and Sorting.
New Mexico Computer Science For All Sorting Algorithms Maureen Psaila-Dombrowski.
Chapter 18: Searching and Sorting Algorithms. Objectives In this chapter, you will: Learn the various search algorithms Implement sequential and binary.
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.
1 Introduction to Sorting Algorithms Sort: arrange values into an order Alphabetical Ascending numeric Descending numeric Two algorithms considered here.
Chapter 8 Sorting and Searching Goals: 1.Java implementation of sorting algorithms 2.Selection and Insertion Sorts 3.Recursive Sorts: Mergesort and Quicksort.
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
UNIT 5.  The related activities of sorting, searching and merging are central to many computer applications.  Sorting and merging provide us with a.
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.
Chapter 9 Sorting 1. The efficiency of data handling can often be increased if the data are sorted according to some criteria of order. The first step.
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.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
Data Structures - CSCI 102 Selection Sort Keep the list separated into sorted and unsorted sections Start by finding the minimum & put it at the front.
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
ICS201 Lecture 21 : Sorting King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department.
PREVIOUS SORTING ALGORITHMS  BUBBLE SORT –Time Complexity: O(n 2 ) For each item, make (n –1) comparisons Gives: Comparisons = (n –1) + (n – 2)
Experimental Study on the Five Sort Algorithms You Yang, Ping Yu, Yan Gan School of Computer and Information Science Chongqing Normal University Chongqing,
CPS120: Introduction to Computer Science Sorting.
SORTING Sorting is storage of data in some order, it can be in ascending or descending order. The term Sorting comes along-with the term Searching. There.
WHICH SEARCH OR SORT IS BETTER?. COMPARING ALGORITHMS Time efficiency refers to how long it takes an algorithm to run Space efficiency refers to the amount.
1 Algorithms Searching and Sorting Algorithm Efficiency.
Searching and Sorting Searching algorithms with simple arrays
Prof. U V THETE Dept. of Computer Science YMA
Searching and Sorting Algorithms
Warmup What is an abstract class?
24 Searching and Sorting.
Chapter 4.
Core Assessments Core #1: This Friday (5/4) Core #2: Tuesday, 5/8.
CS203 Lecture 15.
Presentation transcript:

ArrayList is a class that implements the List interface. The List interface is a blueprint for its “implementor” classes. There is another implementor of the List interface, called LinkedList, that you do not have to know about, but it helps to be aware of it. However, you do need to be aware of the List class. Often, you will see ArrayLists declared this way: List x = new ArrayList(); But why would we do this? This is called polymorphism, which we’ll get into in more detail on another day. But the point is that a later line of code might want to change x to be a LinkedList. If it had been declared as an ArrayList originally, this would be a problem.

Algorithm An algorithm is a step-by-step set of operations to be performed. Real-life example: a recipe Computer science example: determining the mode in an array

Sorting Sorting is the process of arranging the elements in an array in a particular order The sorting process is based on specific values – examples: sorting a list of test scores in ascending numeric order sorting a list of people alphabetically by last name There are many algorithms for sorting an array These algorithms vary in efficiency & speed

Big-O Notation The efficiency/performance or “time complexity” of a sorting algorithm is measured in “Big O” notation For example: if an algorithm has O(n^3) efficiency, that means that the maximum number of “touches” of the elements of the array necessary to sort it equals the cube of the number of elements in the array. So, for example, if an array holds 5 numbers, then in the worst-case scenario, it would take this particular sorting algorithm 125 different comparisons or moves of those numbers in order to sort it. If a sorting algorithm has efficiency O(n), this is called “linear time,” meaning that the algortithm’s efficiency is directly proportional to the size of the array.

You need to know the basics of these 4 sorting algorithms: 1)Selection Sort 2) Insertion Sort 3) Merge Sort 4) Quick Sort

Selection Sort 1.find the smallest element in the array 2.swap it with the first element 3.find the next-smallest element in the array 4.swap it with the second element 5.repeat until all values are in their proper places Efficiency: O(n^2) Inefficient for large lists

Example: original array: smallest is 1: smallest is 2: smallest is 3: smallest is 6:

Insertion Sort 1)pick an item and insert it into its proper place in a sorted sublist 2)repeat until all items have been inserted Efficiency: O(n) An example: original: insert 3: insert 6: insert 1: insert 2:

Merge Sort Divides a list in half, recursively sorts each half (one pair at a time), combines two pairs into a list of 4, etc, until merging the final two lists into one list. At the deepest level of recursion, one-element lists are reached The work of the sort comes in when the sorted sublists are merged together Efficiency: O(n log n) Very well-suited to large lists

Quick Sort Chooses a pivot value, then partitions the list into two sublists, (one list contains everything smaller than the pivot, the other contains everything larger), then recursively sorts each sublist Unlike a merge sort, a quick sort does most of its work when it divides the list It doesn’t need to combine sublists after the recursive steps; the list is already sorted at that point Also, unlike a merge sort, the 2 sublists do not have to be the same size One of the more popular sorting techniques Efficiency: O(n log n)

Review 1.Selection Sort: Swaps numbers in a list until the list is sorted 2.Insertion Sort: Sorts the first 2 #s in the list, then the first 3, then the first 4, etc… 3.Merge Sort: cuts list in half, sorts them, then combines the 2 lists 4.Quick Sort: cuts list in half using a pivot value; sorts each sublist, no need to combine the lists at the end since they are already sorted

Assignment (ItchyAndScratchy) Create an 11-by-11 board which is filled with dashes. Create a class, ItchyClass, containing one method, DisplayBoard. This method should receive the board as a parameter, display it, and return nothing. Randomly place an I and an S on the board. Allow the user to control the I (Itchy) by typing L, R, U, or D, for each of the 4 directions. This will move the I one space on the board. When Itchy catches Scratchy, the game is over. Display how many moves it took. Allow the option to play again. Optional challenge: Allow the user to choose a difficulty level, in which Scratchy (the computer) can move too.