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.

Slides:



Advertisements
Similar presentations
College of Information Technology & Design
Advertisements

CSE Lecture 3 – Algorithms I
Efficiency of Algorithms Csci 107 Lecture 6-7. Topics –Data cleanup algorithms Copy-over, shuffle-left, converging pointers –Efficiency of data cleanup.
CHAPTER 10 ARRAYS II Applications and Extensions.
ICS103: Programming in C Searching, Sorting, 2D Arrays
Arrays.
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.
1 Chapter 9 Arrays and Pointers. 2  One-dimensional arrays  The Relationship between Arrays and Pointers  Pointer Arithmetic and Element Size  Passing.
1 Lecture 9  Arrays  Declaration  Initialization  Applications  Pointers  Declaration  The & and * operators  NULL pointer  Initialization  Readings:
CS102--Object Oriented Programming Lecture 6: – The Arrays class – Multi-dimensional arrays Copyright © 2008 Xiaoyan Li.
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.
1 CS 201 Array Debzani Deb. 2 Having trouble linking math.h? Link with the following option gcc –lm –o test test.o.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 11 Sorting and Searching.
Searching Arrays Linear search Binary search small arrays
Sorting and Searching Arrays CSC 1401: Introduction to Programming with Java Week 12 – Lectures 1 & 2 Wanda M. Kunkle.
Department of Computer Science and Engineering, HKUST 1 HKUST Summer Programming Course 2008 Recursion.
CPT: Search/ Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to discuss searching: its implementation,
Array Processing Simple Program Design Third Edition A Step-by-Step Approach 7.
EENG212 Algorithms and Data Structures
Array.
1 1-d Arrays. 2 Array Many applications require multiple data items that have common characteristics  In mathematics, we often express such groups of.
Arrays in C++ UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) 1 ADNAN BABAR MT14028 CR
Iterative Algorithm Analysis & Asymptotic Notations
SEARCHING UNIT II. Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances.
 2006 Pearson Education, Inc. All rights reserved Searching and Sorting.
Lecture 4 on Data Structure Array. Prepared by, Jesmin Akhter, Lecturer, IIT, JU Searching : Linear search Searching refers to the operation of finding.
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.
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.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Searching Course Lecture Slides 28 May 2010 “ Some things Man was never.
Lecture 7 Introduction to Programming in C Arne Kutzner Hanyang University / Seoul Korea.
CSCI 171 Presentation 4. Execution of a C Program Execution starts in main( ) Top down style –sequential flow Unrealistic to expect sequence in more complicated.
CSC 211 Data Structures Lecture 13
Data Structure Introduction.
CHAPTER 8 SEARCHING CSEB324 DATA STRUCTURES & ALGORITHM.
Chapter 8 Search and Sort ©Rick Mercer. Outline Understand how binary search finds elements more quickly than sequential search Sort array elements Implement.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 3.
Review 1 Arrays & Strings Array Array Elements Accessing array elements Declaring an array Initializing an array Two-dimensional Array Array of Structure.
1 Asymptotic Notations Iterative Algorithms and their analysis Asymptotic Notations –Big O,  Notations Review of Discrete Math –Summations –Logarithms.
IN THE NAME OF ALLAH WHO IS THE MOST BENEFICENT AND MOST MERCIFUL.
Chapter 8: Arrays Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 8 Arrays.
ICS103: Programming in C Searching, Sorting, 2D Arrays Muhamed F. Mudawar.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
UNIT 10 Multidimensional Arrays.
Computer Science: A Structured Programming Approach Using C1 8-7 Two-Dimensional Arrays The arrays we have discussed so far are known as one- dimensional.
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
Computer Programming for Engineers
PREVIOUS SORTING ALGORITHMS  BUBBLE SORT –Time Complexity: O(n 2 ) For each item, make (n –1) comparisons Gives: Comparisons = (n –1) + (n – 2)
Visual C# 2005 Using Arrays. Visual C# Objectives Declare an array and assign values to array elements Initialize an array Use subscripts to access.
Dr. Sajib Datta Feb 11,  Example of declaring and initializing an array. ◦ double someData[3]; /* declare the array someData that will.
Dr. Sajib Datta CSE 1320 Arrays, Search and Sort.
Searching CS 110: Data Structures and Algorithms First Semester,
Functions Dr. Sajib Datta Functions A function is a self-contained unit of program code designed to accomplish a particular task. Some functions.
Arrays. Arrays are objects that help us organize large amounts of information.
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.
Dr. Sajib Datta Feb 14,  Ordering elements in some way  For numeric data, ascending order is the most common  Lots of techniques for.
CMPT 120 Topic: Searching – Part 2 and Intro to Time Complexity (Algorithm Analysis)
Dr. Sajib Datta Sep 10,  #include  void main()  {  int a = 25;  int b = 0;  int c = -35;  if( a || b ) ◦ printf("Test1\n");  else.
1 Agenda Arrays: Definition Memory Examples Passing arrays to functions Multi dimensional arrays.
Arrays Name, Index, Address. Arrays – Declaration and Initialization int x; y[0] y[1] y[2]
Dr. Sajib Datta  Ordering elements in some way  For numeric data, ascending order is the most common  Lots of techniques for sorting  These.
LESSON 8: INTRODUCTION TO ARRAYS. Lesson 8: Introduction To Arrays Objectives: Write programs that handle collections of similar items. Declare array.
CSE 1320 Search, Sort and Strings
Functions Dr. Sajib Datta
25 Searching and Sorting Many slides modified by Prof. L. Lilien (even many without an explicit message indicating an update). Slides added or modified.
24 Searching and Sorting.
ICS103: Programming in C Searching, Sorting, 2D Arrays
Presentation transcript:

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 one-dimensional arrays.  The basic form for declaring a 2D array is ◦ type array_name[rows][columns];  The mathematical equivalent of a two- dimensional array is a matrix.

 A 2D array can be viewed like a table.  To create a 2D array for this table of values, we could use ◦ int some_data[2][3] = { {10, 5, 13}, {2, 7, 19} };  Or ◦ int some_data[][3] = { {10, 5, 13}, {2, 7, 19} };

 As with one-dimensional arrays, 2D arrays indices begin at zero. The difference is now we have to keep track of indices in two dimensions. So, if we create the following array: ◦ int Sales[2][3] = { {1, 2, 3}, {4, 5, 6} };  The individual elements are referenced with the following combinations of array name and indices:

 Print out a specific row or column in an array  #include  int main(void)  {  int row, column, Sales[2][3] = { {1, 2, 3}, {4, 5, 6} };  printf("The values on row 0 are : \n");  for (column = 0; column < 3; column++)  printf("%d ", Sales[0][column] );  printf("\n");  printf("The values on column 0 are: \n");  for (row = 0; row < 2; row++) ◦ printf("%d\n", Sales[row][0]);  return 0;  }

 In case of normal variables: to assign value of ‘a’ to ‘b’; ◦ int a,b; ◦ a=10; ◦ b = a;  Assume we want to copy the values of array ‘a’ to array ‘b’ ◦ int a[10], b[10]; ◦ Initialize a; ◦ b = a;//wrong

 Have to iterate over ‘a’ and assign individual array elements into ‘b’  for(i=0;i<10;i++) ◦ b[i] = a[i];

 Given an array on integers and another integer ‘x’, find if ‘x’ appears in the array. Print ‘Not found’, if ‘x’ does not appear, otherwise print ‘Found’.  Strategy:  Simply iterate over the array if look for a match

 int array[100];  int i,x, found=0;  ….  for(i=0; i<100;i++)  { ◦ if( array[i] == x) ◦ {  found =1;  printf(“Found %d”,x);  break; ◦ }  }  if(!found)  { ◦ printf(“%d not found”, x);  }

 The worst case – the loop is executed n times  On the average – the loop is executed n/2 times  What if the array has a million integers (about 2 20 )

 Can make it faster.  However, unlike linear search, binary search has a precondition ◦ The sequence must be sorted

 Strategy: ◦ Compare ‘x’ with the middle element.  If equal, we found it  Else If ‘x’ < middle element, we can discard the half of the elements; all elements that are greater than the middle element and middle element itself  Now search in the remaining half of the array  Similarly, if ‘x’ > middle element, we can discard the half of the elements; all elements that are smaller than the middle element and middle element itself

 #include  #define ARRSIZE 7  int main(void)  {  int intarr[ARRSIZE], target, i, left, right, mid;  printf("Please input %d integers which are sorted in the ascending order and a target value.", ARRSIZE);  scanf("%d", &target);   for(i = 0; i<ARRSIZE; i++)  {  scanf("%d", &intarr[i]);  }  left = 0;  right = ARRSIZE-1;  while(left <= right)  {  mid = (left+right)/2;  if(intarr[mid] == target)  {  printf("The index of the target in the array is %d.\n", mid);  break;  }  else if (target > intarr[mid])  {  left = mid + 1;  }  else  {  right = mid - 1;  }  if(left > right)  printf("The target is not in the array.\n");  return 0;  }

 How many comparisons on average?  Each time ‘x’ is not found, we are taking the half of the remaining array  If the array has size ‘n’, how many times we can slice it into half?

 After 1 st half: array size n/2  After 2 nd half: array size n/4  After 2rd half: array size n/8  ….  After m-th half: array size n/(2 m )  Since, the minimum array size is 1 [there has to be at least 1 element]  n/(2 m ) = 1 => m = log 2 (n)

 So, in binary search, there are log 2 (n) comparisons.  If you are given a sorted array of a million integers (2 20 ), ◦ Linear search can make a million comparison operations ◦ Binary search will make around 20 comparisons ◦ You will learn more on performance in algorithm classes. This is very important aspect of programming.

 You are given an array containing 9 integers, ranging from 1 to 10. Each number between 1 to 10 appears exactly once in the array, but unfortunately one of them is missing. Can you find the missing number?

 Array ◦ __Array.htm  Sorting and searching ◦ Sort/Catalog0280__Search-Sort.htm  Time complexity ◦ tutorials&d2=complexity1 ◦ outs/brief-complexity.pdf ◦ _AnalysisOfAlgs.pdf (advanced!)