Basic arrays, binary search, selection sort by amelia bateman

Slides:



Advertisements
Similar presentations
Arrays.
Advertisements

C++ for Engineers and Scientists Third Edition
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
Lec 19 Array Intro. Agenda Arrays—what are they Declaring Arrays Constructing Arrays Accessing Array cells.
© 2007 Lawrenceville Press Slide 1 Chapter 10 Arrays  Can store many of the same kind of data together  Allows a collection of related values to be stored.
AP Comp Sci A Chapter 12 - Arrays. Ch 12 Goals: Goals: Declare and create arrays Declare and create arrays Access elements in arrays Access elements in.
Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in.
Lecture 7 Introduction to Programming in C Arne Kutzner Hanyang University / Seoul Korea.
IN THE NAME OF ALLAH WHO IS THE MOST BENEFICENT AND MOST MERCIFUL.
Array Objectives To understand the concept of arrays To understand the purpose to which we use arrays. To be able to declare references to arrays. To be.
Arrays and ArrayLists. int numbers[] = new int[10]; numbers Other ways to declare the same array 1) int[] numbers = new.
Composition When one class contains an instance variable whose type is another class, this is called composition. Instead of inheritance, which is based.
C# E1 CSC 298 Arrays in C#. C# E2 1D arrays  A collection of objects of the same type  array of integers of size 10 int[] a = new int[10];  The size.
CS1020 Data Structures and Algorithms I Lecture Note #2 Arrays.
Chapter 8 Slides from GaddisText Arrays of more than 1 dimension.
Objectives You should be able to describe: One-Dimensional Arrays
L EC. 05: A RRAYS 0. C ONTENT  Features of arrays  Declaring, creating and using arrays  Enhanced for -construct  Variable length parameter-list 1.
Chapter 5 Arrays F Introducing Arrays F Declaring Array Variables, Creating Arrays, and Initializing Arrays F Passing Arrays to Methods F Copying Arrays.
Java Arrays and ArrayLists COMP T1 #5
Arrays Chapter 7.
Java for Beginners Level 6 University Greenwich Computing At School
Data Structures I (CPCS-204)
Arrays (review) CSE 2011 Winter May 2018.
Arrays.
Two Dimensional Array Mr. Jacobs.
Lecture 5 D&D Chapter 6 Arrays and ArrayLists Date.
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
Testing and Debugging.
Tutorial 8 Pointers and Strings
Fifth Lecture Arrays and Lists
ARRAYLIST AND VECTOR.
Introduction to Algorithms
Arrays and the ArrayList Class The ArrayList Class
Chapter-7 part3 Arrays Two-Dimensional Arrays The ArrayList Class.
Array List Pepper.
Hash tables Hash table: a list of some fixed size, that positions elements according to an algorithm called a hash function … hash function h(element)
Building Java Programs
CSE 143 Lecture 5 Binary search; complexity reading:
The ArrayList Class An ArrayList is a complex data structure that allows you to add or remove objects from a list and it changes size automatically. The.
Can store many of the same kind of data together
CS Week 9 Jim Williams, PhD.
Chapter 8 Slides from GaddisText
Chapter 8 Arrays Objectives
CS 200 Objects and ArrayList
© A+ Computer Science - Arrays and Lists © A+ Computer Science -
ArrayLists.
Lecture 5: complexity reading:
Can store many of the same kind of data together
Object Oriented Programming in java
CS2011 Introduction to Programming I Arrays (I)
CSCI 3328 Object Oriented Programming in C# Chapter 7: Arrays
Data Structures (CS212D) Week # 2: Arrays.
Arrays Chapter 7.
Building Java Programs
Chapter 7 Single-Dimensional Arrays and C-Strings
slides created by Marty Stepp
Managing Collections of Data
Building Java Programs
Can store many of the same kind of data together
CS 200 Objects and ArrayList
Chapter 8 Arrays Objectives
Chapter 6 Arrays.
Building Java Programs
CSE 143 Lecture 2 ArrayIntList, binary search
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
CIS 110: Introduction to Computer Programming
CS 200 Objects and ArrayList
Arrays.
Implementing ArrayIntList
Warmup Which of the 4 sorting algorithms uses recursion?
Presentation transcript:

Basic arrays, binary search, selection sort by amelia bateman

to follow along in code… Use Eclipse and set break points in order to inspect your arrays in order to see their current values Import java.util.Arrays (or java.util.*) This is not necessary for using arrays, but is necessary for printing arrays using toString System.out.println(Arrays.toString(arr)); Once we see looping, write a loop which prints each element

Recall arraylists Expanding size ArrayList x = new ArrayList(); Generics: ArrayList<String> x = new ArrayList<String>(); Methods: add, remove, get, size

Arrays versus arraylists Fixed size, declared upon creation Expands as needed Memory and computation efficient Less efficient (slightly) Stores Objects or primitives Can only store Objects Stores only one data type Can store any objects (if not using generics)

Array creation dataType[] name = new dataType[length]; int[] x = new int[5]; //[ _ , _ , _ , _ , _ ]? Recall default values: int->0, double->0.0, Object->null x: [0,0,0,0,0]

Array creation: literals dataType[] name = {val1, val2, …}; int[] x = {1,2,3}; //[1,2,3] String[] strs = {“hi”, “there”}; //[“hi”, “there”] Be careful with types! Attempting to use the wrong literal yields incompatible types error

Using arrays: setting values Set: use index directly int[] x = new int[5]; //[0,0,0,0,0] x[1] = 16; //[0,16,0,0,0] String[] strs = new String[2]; //[null, null] strs[0] = new String(“a”); //[“a”, null] strs[1] = “b”; //[“a”, “b”]

using arrays: getting values Retrieve value at an index directly, with no get method int[] nums = [0,1,2,3]; int x = nums[3]; //x=3 No need to cast, since type is known

Using arrays: remove No such thing! Not comparable to ArrayList.remove(), anyway Length is unchangeable, so no removing elements completely Can loop through array (see next slide) and reset undesirable values

using arrays: length and looping arr.length is an attribute, not a method like size() Index of last elements? arr.length – 1 for(int i=0; i<arr.length; i++)

Using arrays: looping example Looping to set values in an array. Want events up to 100. int[] arr = new int[51]; for(int i=0; i<arr.length; i++){ arr[i]=i*2; } for(int i=0; i<arr.length; i++){ System.out.print(arr[i] + “ “; }

2D arrays Array of arrays, has simplified notation int[][] flag = new int[5][4]; What does this look like in memory? Is it 5 rows and 4 columns or vice versa? Depends on how you think about it! If you draw the first array (5) horizontally, there are 5 columns and 4 rows Code example

Jagged array Like 2D array, but does not have to be an even rectangle

application: binary search Problem: find a specific element in a sorted array Analogy: when searching a phone book, open to the middle. Then open to the middle of whichever half is correct. Repeat until successful Why not just search the entire thing? Runtime efficiency

Pseudocode min=0, max=0 if(max < min) -> stop and report failure mid = average(min, max) if(arr[mid] = target) -> success if(arr[mid] < target]) -> min=mid+1 if(arr[mid] > target) -> max=mid-1 Loop back to start

Application: selection sort