Lecture 4 on Data Structure Array. Prepared by, Jesmin Akhter, Lecturer, IIT, JU Searching : Linear search Searching refers to the operation of finding.

Slides:



Advertisements
Similar presentations
College of Information Technology & Design
Advertisements

Efficiency of Algorithms Csci 107 Lecture 6-7. Topics –Data cleanup algorithms Copy-over, shuffle-left, converging pointers –Efficiency of data cleanup.
Chapter 9: Searching, Sorting, and Algorithm Analysis
CS0007: Introduction to Computer Programming Array Algorithms.
Dr. Sajib Datta  We can also have arrays of arrays, also known as multidimensional arrays.  E.g., A two-dimensional array is an array of several.
Search and Recursion CS221 – 2/23/09. List Search Algorithms Linear Search: Simple search through unsorted data. Time complexity = O(n) Binary Search:
Chapter 11 Sorting and Searching. Topics Searching –Linear –Binary Sorting –Selection Sort –Bubble Sort.
Sorting and Searching. Searching List of numbers (5, 9, 2, 6, 3, 4, 8) Find 3 and tell me where it was.
Chapter 8 Search and Sort Asserting Java ©Rick Mercer.
 2003 Prentice Hall, Inc. All rights reserved. 1 Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
Algorithm Efficiency and Sorting
Efficiency of Algorithms February 11th. Efficiency of an algorithm worst case efficiency is the maximum number of steps that an algorithm can take for.
 2003 Prentice Hall, Inc. All rights reserved Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
© 2006 Pearson Addison-Wesley. All rights reserved10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
Sorting and Searching Arrays CSC 1401: Introduction to Programming with Java Week 12 – Lectures 1 & 2 Wanda M. Kunkle.
February 17, 2015Applied Discrete Mathematics Week 3: Algorithms 1 Double Summations Table 2 in 4 th Edition: Section th Edition: Section th.
C o n f i d e n t i a l Developed By Nitendra HOME NEXT Subject Name: Data Structure Using C Unit Title: Searching Methods.
ARRAYS, RECORDS AND POINTER
1 Searching. 2 Searching Searching refers to the operation of finding an item from a list of items based on some key value. Two Searching Methods (1)
Lecture 12. Searching Algorithms and its analysis 1.
Complexity of algorithms Algorithms can be classified by the amount of time they need to complete compared to their input size. There is a wide variety:
Data Structures & Algorithms CHAPTER 4 Searching Ms. Manal Al-Asmari.
© 2011 Pearson Addison-Wesley. All rights reserved 10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
Chapter 10 A Algorithm Efficiency. © 2004 Pearson Addison-Wesley. All rights reserved 10 A-2 Determining the Efficiency of Algorithms Analysis of algorithms.
1 Lecture 5: Part 1 Searching Arrays Searching Arrays: Linear Search and Binary Search Search array for a key value Linear search  Compare each.
SEARCHING (Linear/Binary). Searching Algorithms  method of locating a specific item of information in a larger collection of data.  two popular search.
P-1 University of Washington Computer Programming I Lecture 15: Linear & Binary Search ©2000 UW CSE.
Chapter 2 ARRAYS.
Arrays.
Examples using Arrays. Summing Squares Problem: To compute the sum of the squares of N numbers N is given N values are also given These should be read.
Data Strcutures.
Advance Data Structure 1 College Of Mathematic & Computer Sciences 1 Computer Sciences Department م. م علي عبد الكريم حبيب.
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.
SEARCHING. Vocabulary List A collection of heterogeneous data (values can be different types) Dynamic in size Array A collection of homogenous data (values.
CSC 211 Data Structures Lecture 13
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.
Data Structure Introduction.
Lecture -3 on Data structures Array. Prepared by, Jesmin Akhter, Lecturer, IIT, JU Array Data structures are classified as either linear or nonlinear.
Chapter 8 Search and Sort ©Rick Mercer. Outline Understand how binary search finds elements more quickly than sequential search Sort array elements Implement.
Review 1 Arrays & Strings Array Array Elements Accessing array elements Declaring an array Initializing an array Two-dimensional Array Array of Structure.
Chapter 8 Sorting and Searching Goals: 1.Java implementation of sorting algorithms 2.Selection and Insertion Sorts 3.Recursive Sorts: Mergesort and Quicksort.
Lecture 9COMPSCI.220.FS.T Lower Bound for Sorting Complexity Each algorithm that sorts by comparing only pairs of elements must use at least 
Algorithm Analysis. What is an algorithm ? A clearly specifiable set of instructions –to solve a problem Given a problem –decide that the algorithm is.
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
Searching Topics Sequential Search Binary Search.
Sorting & Searching Geletaw S (MSC, MCITP). Objectives At the end of this session the students should be able to: – Design and implement the following.
Dr. Sajib Datta CSE 1320 Arrays, Search and Sort.
Two dimensional arrays A two dimensional m x n array A is a collection of m. n elements such that each element is specified by a pair of integers (such.
CHAPTER 51 LINKED LISTS. Introduction link list is a linear array collection of data elements called nodes, where the linear order is given by means of.
1. Traversing a linear array Here A is a linear array with lower bound LB and upper bound UB. This algorithm traverses A applying an operation PROCESS.
Arrays Department of Computer Science. C provides a derived data type known as ARRAYS that is used when large amounts of data has to be processed. “ an.
Sorting and Searching Bubble Sort Linear Search Binary Search.
CMPT 120 Topic: Searching – Part 2 and Intro to Time Complexity (Algorithm Analysis)
Chapter 16: Searching, Sorting, and the vector Type.
Code: BCA302 Data Structures with C Prof. (Dr.) Monalisa Banerjee By.
Applied Discrete Mathematics Week 2: Functions and Sequences
Data Structures I (CPCS-204)
Prepared by, Jesmin Akhter, Lecturer, IIT, JU
Lecture – 2 on Data structures
Enough Mathematical Appetizers!
Linear and Binary Search
Applied Discrete Mathematics Week 6: Computation
ARRAYS, RECORDS AND POINTER
Programming and Data Structure
24 Searching and Sorting.
Principles of Computing – UFCFA3-30-1
Shell Sort and Merge Sort
Data Structures: Searching
Principles of Computing – UFCFA3-30-1
ICS103: Programming in C Searching, Sorting, 2D Arrays
Presentation transcript:

Lecture 4 on Data Structure Array

Prepared by, Jesmin Akhter, Lecturer, IIT, JU Searching : Linear search Searching refers to the operation of finding the location LOC of ITEM in DATA, or printing some message that ITEM does not appear there. DATA is a linear array with n elements. The most intuitive way to search for a given ITEM in DATA is to compare ITEM with each element of DATA one by one. That is first we test whether DATA[1 ]=ITEM, and then we test whether DATA[2 ]=ITEM, and so on. This method, which traverses DATA sequentially to locate ITEM, is called linear search or sequential search. Algorithm 4.5 : A linear array DATA with N elements and a specific ITEM of information are given. This algorithm finds the location LOC of ITEM in the array DATA or sets LOC = 0. 1.Set DATA[N+1]:=ITEM. 2.Set LOC:=1. 3.Repeat while DATA[LOC]! =ITEM : Set LOC := LOC +1. [End of loop] 4.If LOC = N+1, then : Write : ITEM is not in the array DATA. Else : Write : LOC is the location of ITEM. 5.Exit.

Prepared by, Jesmin Akhter, Lecturer, IIT, JU Complexity of Linear search Measured by the number f(n) of comparisons required to find ITEM in the DATA array. Two important case: –Average case: Suppose p k is the probability that ITEM appears in DATA[k], and q is the probability that ITEM does not appears in DATA. –Then p 1 + p 2 + p 3 + p 4 + … p n + q = 1 (Total probability) Average number of comparisons can be calculated by- –f(n) = 1. p p p 1 + ……………….+ n. p n + (n+1). q –Let, q is very small q  0 and item appears in equal probability then p i = 1/n –Worse case: when the search occurs through the entire array, DATA. i.e. When the ITEM does not appar in the array DATA It requires f(n)= n+1 In this case, the running time is proportional to n

Prepared by, Jesmin Akhter, Lecturer, IIT, JU Binary Search Algorithm BINARY(DATA, LB, UB, ITEM, LOC) 1.Set BEG=LB; END=UB; and MID=INT((BEG+END)/2). 2.Repeat step 3 and 4 while BEG ≤ END and DATA[MID] ≠ ITEM 3.If ITEM < DATA[MID] then Set END= MID + 1 Else: Set BEG= MID-1 [end of if structure] 4.Set MID= INT((BEG+END)/2) [End of step 2 loop] 5.If ITEM = DATA[MID] then Set LOC= MID Else: Set LOC= NULL [end of if structure] 6.Exit.

Prepared by, Jesmin Akhter, Lecturer, IIT, JU Binary Search example (Seek for 123)

Prepared by, Jesmin Akhter, Lecturer, IIT, JU Binary Search - Complexity Often not interested in best case. Worst case: –Loop executes until BEG <= END –Size halved in each iteration –N, N/2, N/4, …N/2 K …. 1 –How many steps ?

Prepared by, Jesmin Akhter, Lecturer, IIT, JU Binary Search - Complexity Worst case: –N/2 K = 1 i.e. 2 K = N –Which gives K=log 2 N steps, which is O(log 2 (N)) –This is considered very fast when compared to linear

Prepared by, Jesmin Akhter, Lecturer, IIT, JU Binary Search - Complexity Average case: –1st iteration: 1 possible value –2 nd iteration: 2 possible values (left or right half) –3 rd iteration: 4 possible values (left of left, right of left, right of right, right of left) –i th iteration: 2 i-1 possible values

Prepared by, Jesmin Akhter, Lecturer, IIT, JU Binary Search - Complexity Average Case: … (upto log N steps)  1 element can be found with 1 comparison  2 elements  2  4 elements  3  Above Sum = sum over all possibilities =  i=0 to log N (i*2 i-1 ) = O (log N)

Prepared by, Jesmin Akhter, Lecturer, IIT, JU Multidimensional arrays Two dimensional, three dimensional arrays and ….. Where elements are referenced respectively by two, three and ……subscripts. Two – dimensional Arrays A Two – dimensional Arrays m x n array A is a collection of m. n data elements such that each element is specified by a pair of integers (such as J, K), called subscripts. The element of A with first subscript J and second subscript K will be denoted by A[J, K] A[3, 1]A[3, 2]A[3, 3] A[1, 1]A[1, 2]A[1, 3] A[2, 2]A[2, 1]A[2, 3] Rows Columns Fig: Two dimensional 3 x 3 array A

Prepared by, Jesmin Akhter, Lecturer, IIT, JU Matrix Multiplication Algorithm 4.7: MATMUL(A, B, C, M, P, N) Let A be an MXP matrix array, and let B be a PXN matrix array. This algorithm stores the product of A and B in an MXN matrix array. 1.Repeat steps 2 to 4 for I =1 to M: 2.Repeat steps 3 to 4 for J =1 to N: 3.Set C[I, J]:=0 4.Repeat for K =1 to P: 5.C[I, J]:= C[I, J]:+A[I, K]*B[K, J] 6.Exit See solved problem 4.12.

Prepared by, Jesmin Akhter, Lecturer, IIT, JU Solved Problem 4.2, 4.3, 4.4, 4.5, 4.6, 4.7, 4.8, 4.9, 4.10,4.12 Supplementary problems: 4.25