3/21/2016IT 2751 Tow kinds of Lists Array What can be done? What can be easily done? student 1 student 2 student 3 student 4 Linked List student 2 student.

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

IS 2610: Data Structures Sorting Feb 16, Sorting Algorithms: Bubble sort Bubble sort  Move through the elements exchanging adjacent pairs if the.
Problem Solving 5 Using Java API for Searching and Sorting Applications ICS-201 Introduction to Computing II Semester 071.
Session 3 Algorithm. Algorithm Algorithm is a set of steps that are performed to solve a problem. The example below describes an algorithm Example Check.
Basic Algorithms on Arrays. Learning Objectives Arrays are useful for storing data in a linear structure We learn how to process data stored in an array.
Data Structures: A Pseudocode Approach with C 1 Chapter 5 Contd... Objectives Explain the design, use, and operation of a linear list Implement a linear.
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."
21/3/00SEM107- Kamin & ReddyClass 15 - Recursive Sorting - 1 Class 15 - Recursive sorting methods r Processing arrays by recursion r Divide-and-conquer.
Searching and Sorting Linear Search Binary Search Selection Sort
1 Lecture 23 Searching Overview  What is Searching?  Linear Search and its Implementation.  Brief Analysis of Linear Search.  Binary Search and its.
CSE 373: Data Structures and Algorithms
1 Lecture 21 Introduction to Sorting I Overview  What is Sorting?  Some Useful Array Handling Methods.  Selection Sort and its Implementation.  Brief.
1 Searching Algorithms Overview  What is Searching?  Linear Search and its Implementation.  Brief Analysis of Linear Search.  Binary Search and its.
Unit 271 Searching and Sorting Linear Search Binary Search Selection Sort Insertion Sort Bubble (or Exchange) Sort Exercises.
1 © 2006 Pearson Addison-Wesley. All rights reserved Searching and Sorting Linear Search Binary Search ; Reading p Selection Sort ; Reading p
Analysis of Algorithm.
Searching Chapter 18 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Sorting Arrays. Selection Sort  One of the easiest ways to sort the elements of an array is by using the selection sort algorithm.  Assume that the.
1 Sorting Algorithms (Part I) Sorting Algoritms (Part I) Overview  What is Sorting?  Some Useful Array Handling Methods.  Selection Sort and its Implementation.
Simple Sort Algorithms Selection Sort Bubble Sort Insertion Sort.
Chapter 14: Sorting and searching. Chapter Goals To study several sorting and searching algorithms To appreciate that algorithms for the same task can.
Week 11 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Building Java Programs Chapter 13 Searching reading: 13.3.
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.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Sorting and Searching.
Sorting Algorithms. Sorting Sorting is a process that organizes a collection of data into either ascending or descending order. public interface ISort.
Computer Science Searching & Sorting.
French Territory of St. Pierre CSE 114 – Computer Science I Arrays.
1 Linear and Binary Search Instructor: Mainak Chaudhuri
Chapter 2 ARRAYS.
CSC 205 Java Programming II Algorithm Efficiency.
JAVA Arrays. Objectives Be able to declare and initialize arrays Be able to conceptualize (draw) how arrays are represented in computer memory. Be able.
CS261 – Recitation 5 Fall Outline Assignment 3: Memory and Timing Tests Binary Search Algorithm Binary Search Tree Add/Remove examples 1.
ARRAYS Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall
CSE 373 Data Structures and Algorithms
1 Arrays: Matrix Renamed Instructor: Mainak Chaudhuri
What/how do we care about a program? Robustness Correctness Efficiency (speed, space) 11/2/20151IT 179  Software Testing  Error Handling  Efficiency.
Comparison-Based Sorting & Analysis Smt Genap
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.
Introduction to Data Structures and Algorithms
Composition When one class contains an instance variable whose type is another class, this is called composition. Instead of inheritance, which is based.
Introduction to array: why use arrays ?. Motivational example Problem: Write a program that reads in and stores away 5 double numbers After reading in.
Arrays and ArrayLists Topic 6. One Dimensional Arrays Homogeneous – all of the same type Contiguous – all elements are stored sequentially in memory For.
Sorting and Searching. Searching  Problem definition: Given a value X, return the index of X in the array if such X exist. Otherwise, return NOT_FOUND(-1).
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 4 Introduction.
1 Arrays and Methods Java always passes arguments by value – that is a copy of the value is made in the called method and this is modified in the method.
COP 3540 Data Structures with OOP
1 Examples of class: Recursive data structures Instructor: Mainak Chaudhuri
Chapter 3: Sorting and Searching Algorithms 3.1 Searching Algorithms.
Array Sort. Sort Pass 1 Sort Pass 2 Sort Pass 3.
Sorting Algorithms. Sorting Sorting is a process that organizes a collection of data into either ascending or descending order. public interface ISort.
Methods. Creating your own methods Java allows you to create custom methods inside its main body. public class Test { // insert your own methods right.
Object Oriented Programming Lecture 2: BallWorld.
CMSC 202 Computer Science II for Majors. CMSC 202UMBC Topics Templates Linked Lists.
Chapter 3: Fundamental Data Structures: The Array and Linked Structures Data Structures in Java: From Abstract Data Types to the Java Collections Framework.
Sorting Mr. Jacobs.
Lecture 14 Searching and Sorting Richard Gesick.
Abstraction A tool (concept) to manage complexity
Announcements Project checkpoint next week after lab sessions
What/how do we care about a program?
The father of algorithm analysis
محاور المحاضرة خوارزمية الفقاعات الهوائية (Bubble Sort)
Lecture 11 Searching and Sorting Richard Gesick.
Topic 24 sorting and searching arrays
CSE 143 Lecture 5 References and Linked Nodes
Linked Lists.
Module 8 – Searching & Sorting Algorithms
Module 8 – Searching & Sorting Algorithms
Searching.
Module 8 – Searching & Sorting Algorithms
Presentation transcript:

3/21/2016IT 2751 Tow kinds of Lists Array What can be done? What can be easily done? student 1 student 2 student 3 student 4 Linked List student 2 student 4 student 1 student 3

3/21/2016IT 2752 Array What can be done? What can be easily done? student 1 student 2 student 3 student 4 Linked List student 2 student 4 student 1 student 3 Some typical operations on Lists 1.Random access 2.Add/remove from the head 3.Add/remove from the tail 4.Add/remove from the middle 5.Resize

3/21/2016IT 2753 Two types of arrays Static array –size determined at compile time –can’t change afterwards Dynamic array (JAVA’s type, objects) –Size determine at run time –Can be resized (larger or smaller)

3/21/2016IT 2754 Array Characteristics Array student 1 student 2 student 3 student 4 A A[0] A[1] A[2] A[3] Linked List student 2 student 4 student 1 student 3 The elements of an array are in adjacent memory locations An array is a random (direct) access data structure.

3/21/2016IT 2755 Example of Array Traversal: Linear Search – O(n) StatementsCost 1int linearSearch( int [] a, int target ) { 2 int n = a.length; 1 3 int i = 0; 1 4 while ( i < n ) { n if ( target == a[i] ) n 6 return i; 1 7 i++; n 8 } 9 return –1; 1 } TOTAL 3n + 4

3/21/2016 IT Creating Arrays public static void main(String[] args) { int size=4; int[] A=null; A = new int[size]; Double[] B = new Double[100]; String[] S = new String[size/2];..... } size A B S 4 Created during the run time Created during the compile time

3/21/2016 IT Creating Arrays public static void main(String[] args) { int size=4; int[] A=null; A = new int[size]; Double[] B = new Double[100]; String[] S = new String[size/2];..... } size A B S 4 Created during the run time Created during the compile time

3/21/2016IT 2758 Using Arrays public static void main(String[] args) { int size=4; int[] A=null; A = new int[size]; Double[] B = new Double[100]; String[] S = new String[size/2];..... A[2]=2; B[0]= new Double(3.14); S[1] = new String(“test”); // or S[1] = “test”;..... } size A B S 4 Double: 3.14 String: “test” 2

3/21/2016IT 2759 Arrays passed as References void public static void fun(int a[]) { int i; I=2; a[i] += 5; } aiai public static void main(String[] args) { int a[4]={10,200,300,400}; for (int i=0; i < a.length; i++) System.out.print(" "+a[i]); fun(a); for (int i=0; i < a.length; i++) System.out.print(" "+a[i]); } a a[2] Still, we use the call-by- value mechanism!! 2

3/21/2016IT Passing Arrays (an object) public static void main(String[] args) {..... int[] A=null; A = new int[30]; double[] B = new double[100]; String[] S = new String[2];..... } public static void getArray(int[] a) { for (int i=0; i<a.length; i++) a[i] = (int)(Math.random()*1000); }

3/21/2016IT Passing Arrays (exercise) // select the minimum between a[s] to the end // and return the index of the minimum // private int min(int a[], int s){ int m = s; for (int i=s; i < a.length;i++) { if (a[i] < a[m]) m=i; } return m; } // exchange the contents of a[i] and a[j] // private void exchange(int[] a, int i, int j) { int temp = a[i]; a[i] = a[j]; a[j] = temp; } O(n-s) where n is the size of the array O(3) = O(1)

3/21/2016IT Selection Sort (a quadratic sort) public void sort(int a[]) { // select right element for a[i] for (int i = 0; i<a.length-1; i++) { int j = min(a,i); exchange(a,i,j); } i j i j i j i j i j i j

3/21/2016IT Why sorting? For searching! Sequential search  Binary search O(n)  O (log n) 1,000,000  20 More sorting algorithms will be seen in this class?sorting algorithms Try here

3/21/2016 IT Two dimensional Arrays public static void main(String[] args) { int size=3; int[][] A = new int[size][4]; A[2][3] = 7; A[0][2] = 9; String[][] B;..... } 7 size A B 4 9 A[0] A[1] A[2] A[0][0] A[0][1] A[0][2] A[0][3] A[2][0] A[2][1] A[2][2] A[2][3]

3/21/2016 IT Two dimensional Arrays public static void main(String[] args) { String[][] B; B = new String[3][]; B[1] = new String[4]; B[1][2] = “Robinson”; B[0] = new String[5]; ……… } B B[0] B[1] B[2]B[1][0] B[1][1] B[1][2] B[1][3] Robinson

3/21/2016 IT Handling two dimensional arrays public static void main(String[] args) { String[][] B; B = new String[3][]; B[0] = new String[4]; B[1] = new String[5]; B[2] = new String[7]; ……… clear(B); ……… } public static void clear(String[][] X) { for (int i=0; i< X.length; i++) for (int j=o; j<X[i].length; j++) X[i][j] = null; }

3/21/2016IT Operations on Array Traversing – can be bidirectional O(n) Resizing – θ(size of new array) operation Replacing an element – an O(1) operation Inserting an element – an O(n) operation due to data movement Deleting an element – O(1) or O(n) depending on implementation

3/21/2016IT Some other operations on arrays are expensive Resizing – θ(size of new array) operation Inserting an element – an O(n) operation due to data movement Deleting an element – O(1) or O(n) depending on implementation (way more than you thought)