Array operations II manipulating arrays and measuring performance.

Slides:



Advertisements
Similar presentations
Topic 14 Searching and Simple Sorts "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." -The.
Advertisements

Lesson 8 Searching and Sorting Arrays 1CS 1 Lesson 8 -- John Cole.
MATH 224 – Discrete Mathematics
Introduction to Computer Science Theory
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."
CMPT 225 Sorting Algorithms Algorithm Analysis: Big O Notation.
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 Algorithms. Motivation Example: Phone Book Searching Example: Phone Book Searching If the phone book was in random order, we would probably never.
Sorting and Searching. Searching List of numbers (5, 9, 2, 6, 3, 4, 8) Find 3 and tell me where it was.
1 ICS103 Programming in C Lecture 14: Searching and Sorting.
Objectives Learn how to implement the sequential search algorithm Explore how to sort an array using the selection sort algorithm Learn how to implement.
©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
Simple Sort Algorithms Selection Sort Bubble Sort Insertion Sort.
By D.Kumaragurubaran Adishesh Pant
Chapter 8 ARRAYS Continued
CSE 143 Lecture 5 Binary search; complexity reading: slides created by Marty Stepp and Hélène Martin
Chapter 13 Lists. List  List  A variable-length, linear collection of homogeneous components  Example  StudentRec Students[100];  A list of 100 students.
Week 11 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
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.
Applications of Arrays (Searching and Sorting) and Strings
Chapter 19: Searching and Sorting Algorithms
Chapter 2 Array Data Structure Winter Array The Array is the most commonly used Data Storage Structure. It’s built into most Programming languages.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
February 4, 2005 Searching and Sorting Arrays. Searching.
CS 162 Intro to Programming II Searching 1. Data is stored in various structures – Typically it is organized on the type of data – Optimized for retrieval.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Searching Course Lecture Slides 28 May 2010 “ Some things Man was never.
Big Oh Algorithms are compared to each other by expressing their efficiency in big-oh notation Big O notation is used in Computer Science to describe the.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington ArraySet and Binary Search.
CSE 373 Data Structures and Algorithms
Sorting – Insertion and Selection. Sorting Arranging data into ascending or descending order Influences the speed and complexity of algorithms that use.
Chapter 14: Searching and Sorting
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.
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.
SORTING 2014-T2 Lecture 13 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Marcus Frean.
Chapter 6 Sorting. Agenda Swapping two values in array of int Bubble sort O(n 2 ) Swapping/sorting an array of Object Swapping/sorting a Vector of Object.
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 9 Searching & Sorting.
1 Chapter 13-2 Applied Arrays: Lists and Strings Dale/Weems.
Binary search and complexity reading:
Computer Science 112 Fundamentals of Programming II Searching, Sorting, and Complexity Analysis.
© 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.
Chapter 10: Class Vector and String, and Enumeration Types Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 14 Searching and Sorting.
Chapter 9: Sorting1 Sorting & Searching Ch. # 9. Chapter 9: Sorting2 Chapter Outline  What is sorting and complexity of sorting  Different types of.
The Selection Sort Mr. Dave Clausen La Cañada High School.
 2006 Pearson Education, Inc. All rights reserved. 1 Searching and Sorting.
Building Java Programs Chapter 13 Lecture 13-1: binary search and complexity reading:
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.
COP 3503 FALL 2012 Shayan Javed Lecture 15
Lecture 14 Searching and Sorting Richard Gesick.
Introduction to Search Algorithms
Computer Science 112 Fundamentals of Programming II
Topic 14 Searching and Simple Sorts
CSE 143 Lecture 5 Binary search; complexity reading:
Algorithm design and Analysis
Mr. Dave Clausen La Cañada High School
Binary Search and Intro to Sorting
Lecture 11 Searching and Sorting Richard Gesick.
Mr. Dave Clausen La Cañada High School
Topic 14 Searching and Simple Sorts
Topic 24 sorting and searching arrays
Sum this up for me Let’s write a method to calculate the sum from 1 to some n public static int sum1(int n) { int sum = 0; for (int i = 1; i
Sorting and Searching -- Introduction
The Sequential Search (Linear Search)
Presentation transcript:

Array operations II manipulating arrays and measuring performance

D. Goforth, COSC 3106, fall Operations on arrays  searching – most common collection operation in computer science search for a value in a collection may succeed or fail basis of all data operations

D. Goforth, COSC 3106, fall Search algorithm int size; double[] data; … int search ( double x ) { for( int index = 0; index < size; index++ ) if ( data[index] == x ) return index; return -1; // signal for “not found” }

D. Goforth, COSC 3106, fall Intelligent searching  if data in the array is in order, the search can be more efficient K K

D. Goforth, COSC 3106, fall Binary search  data must be sorted  search is faster K K

BinarySearch algorithm int size; double[] data; int binarysearch ( double x ) { int low = 0, high = size-1, middle; while (low <= high ) { middle = ( low + high ) / 2; if ( data[middle] == x ) return middle; if ( data[middle] > x ) high = middle – 1; else low = middle + 1; } return -1; // signal for “not found” }

D. Goforth, COSC 3106, fall Sorting data in arrays  for binary search, data must be in order algorithms for sorting data by swapping positions

D. Goforth, COSC 3106, fall Basic swap operation int size; double[] data; double temp; temp = data[4]; data[4] = data[7]; data[7] = temp; temp data 1 2 3

D. Goforth, COSC 3106, fall Selection sort algorithm  find minimum item in array  swap minimum item with first item  repeat to find second item, third, etc. temp data minimum

Selection sort algorithm int size; double[] data; void selectionSort () { for ( int i = 0; i < size-1; i++ ) { int minIndex = i; for ( int j = i+1; j < size; j++ ) if ( data[j] < data[minIndex] ) minIndex = j; double temp = data[minIndex]; data[minIndex] = data[j]; data[j] = temp; }

D. Goforth, COSC 3106, fall Insertion sort algorithm  compare first, second items; rearrange if necessary  place third item properly with first second  repeat for all other items temp data sorted

Insertion sort algorithm int size; double[] data; void insertionSort () { for ( int i = 1; i < size; i++ ) { double temp = data[i]; int j = i-1; while (j >= 0 && data[j] > temp ) { data[j+1] = data[j]; j--; } data[j+1] = temp; }

D. Goforth, COSC 3106, fall Fast search: is it worth the sort?  tradeoff: slow linear search fast binary search BUT extra effort and time to sort data  good tradeoff if ‘many’ searches sort

D. Goforth, COSC 3106, fall Measuring performance of algorithms  performance in time (and space) time – execution time of method space – extra memory used in execution  performance due to coding comparing algorithms (not hardware speed)  dominant performance factor: dependence on collection size

D. Goforth, COSC 3106, fall Measuring performance – predicting algorithm efficiency  count number of executions of statements  watch number increase as collection size increases  what is rate of increase? Performance project

D. Goforth, COSC 3106, fall Performance categories – Big O  approximate measure of rate of increase in execution time with size of collection (or other factors) useful to compare algorithms e.g. – sort algorithms works with ‘random factor’ algorithms e.g. – search, insertion sort

D. Goforth, COSC 3106, fall Performance – big O notation  categories describe performance as approximate function of size linear function O(n) quadraticO(n 2 ) perf n n