SlideShare a Scribd company logo
Data Structures I (CPCS-204) 
Week # 3: Arrays & Searching Algorithms
Data Structures 
 Data structure 
 A particular way of storing and organising data in a 
computer so that it can be used efficiently 
 Types of data structures 
 Based on memory allocation 
o Static (or fixed sized) data structures (Arrays) 
o Dynamic data structures (Linked lists) 
 Based on representation 
o Linear (Arrays/linked lists) 
o Non-linear (Trees/graphs)
Array: motivation 
 You want to store 5 numbers in a computer 
 Define 5 variables, e.g. num1, num2, ..., num5 
 What, if you want to store 1000 numbers? 
 Defining 1000 variables is a pity! 
 Requires much programming effort 
 Any better solution? 
 Yes, some structured data type 
o Array is one of the most common structured data types 
o Saves a lot of programming effort (cf. 1000 variable names)
What is an Array? 
 A collection of data elements in which 
 all elements are of the same data type, hence 
homogeneous data 
o An array of students’ marks 
o An array of students’ names 
o An array of objects (OOP perspective!) 
 elements (or their references) are stored at 
contiguous/ consecutive memory locations 
 Array is a static data structure 
 An array cannot grow or shrink during program 
execution – its size is fixed
Basic concepts 
 Array name (data) 
 Index/subscript (0...9) 
 The slots are numbered sequentially 
starting at zero (Java, C++) 
 If there are N slots in an array, the 
index will be 0 through N-1 
 Array length = N = 10 
 Array size = N x Size of an element = 40 
 Direct access to an element
Homogeneity 
 All elements in the array must have the same 
data type 
Index: 
0 1 2 3 4 5 6 7 8 9 
Value: 5 10 18 30 45 50 60 65 70 80 
Index: 
0 1 2 3 4 
Value: 5.5 10.2 18.5 45.6 60.5 
Index: 
0 1 2 3 4 
Value: ‘A’ 10.2 55 ‘X’ 60.5 
Not an array
Contiguous Memory 
 Array elements are stored at contiguous memory 
locations 
Index: 
0 1 2 3 4 5 6 7 8 9 
Value: 5 10 18 30 45 50 60 65 70 80 
 No empty segment in between values (3 & 5 are 
empty – not allowed) 
Index: 
0 1 2 3 4 5 6 7 8 9 
Value: 5 10 18 45 60 65 70 80
Using Arrays 
 Array_name[index] 
 For example, in Java 
 System.out.println(data[4]) will display 
0 
 data[3] = 99 will replace -3 with 99
Some more concepts 
data[ -1 ] always illegal 
data[ 10 ] illegal (10 > upper bound) 
data[ 1.5 ] always illegal 
data[ 0 ] always OK 
data[ 9 ] OK 
Q. What will be the output of? 
1.data[5] + 10 
2.data[3] = data[3] + 10
Array’s Dimensionality 
 One dimensional (just a linear list) 
 e.g., 
 Only one subscript is required to access an individual 
element 
 Two dimensional (matrix/table) 
5 10 18 30 45 50 60 65 70 80 
 e.g., 2 x 4 matrix (2 rows, 4 columns) 
Col 0 Col 1 Col 2 Col 3 
Row 0 20 25 60 40 
Row 1 30 15 70 90
Two dimensional Arrays 
 Let, the name of the two dimensional array is M 
20 25 60 40 
30 15 70 90 
 Two indices/subscripts are required (row, 
column) 
 First element is at row 0, column 0 
 M0,0 or M(0, 0) or M[0][0] (more common) 
 What is: M[1][2]? M[3][4]?
Array Operations 
 Indexing: inspect or update an element using its index. 
Performance is very fast O(1) 
randomNumber = numbers[5]; 
numbers[20000] = 100; 
 Insertion: add an element at certain index 
– Start: very slow O(n) because of shift 
– End : very fast O(1) because no need to shift 
 Removal: remove an element at certain index 
– Start: very slow O(n) because of shift 
– End : very fast O(1) because no need to shift 
 Search: performance depends on algorithm 
1) Linear: slow O(n) 2) binary : O(log n) 
 Sort: performance depends on algorithm 
1) Bubble: slow O(n2) 2) Selection: slow O(n2) 
3) Insertion: slow O(n2) 4)Merge : O (n log n)
One Dimensional Arrays in Java 
To declare an array follow the type with (empty) []s 
int[] grade; //or 
int grade[]; //both declare an int array 
In Java arrays are objects so must be created with the new 
keyword 
 To create an array of ten integers: 
int[] grade = new int[10]; 
Note that the array size has to be specified, although it can be 
specified with a variable at run-time
Arrays in Java 
 When the array is created memory is reserved for its contents 
 Initialization lists can be used to specify the initial values of an 
array, in which case the new operator is not used 
int[] grade = {87, 93, 35}; //array of 3 ints 
To find the length of an array use its .length property 
int numGrades = grade.length; //note: not .length()!!
Searching Algorithms 
 Search for a target (key) in the search space 
 Search space examples are: 
 All students in the class 
 All numbers in a given list 
 One of the two possible outcomes 
 Target is found (success) 
 Target is not found (failure)
Searching Algorithms 
Index: 0 1 2 3 4 
Value: 
20 40 10 30 60 
Target = 30 (success or failure?) 
Target = 45 (success or failure?) 
Search strategy? 
List Size = N = 5 
Min index = 0 
Max index = 4 (N - 1)
Sequential Search 
 Search in a sequential order 
 Termination condition 
 Target is found (success) 
 List of elements is exhausted (failure)
Sequential Search 
Index: 0 1 2 3 4 
Value: 
20 40 10 30 60 
Target = 30 
Step 1: Compare 30 with value at index 0 
Step 2: Compare 30 with value at index 1 
Step 3: Compare 30 with value at index 2 
Step 4: Compare 30 with value at index 3 (success)
Sequential Search 
Index: 0 1 2 3 4 
Value: 
20 40 10 30 60 
Target = 45 
Step 1: Compare 45 with value at index 0 
Step 2: Compare 45 with value at index 1 
Step 3: Compare 45 with value at index 2 
Step 4: Compare 45 with value at index 3 
Step 5: Compare 45 with value at index 4 
Failure
Sequential Search Algorithm 
Given: A list of N elements, and the target 
1. index  0 
2. Repeat steps 3 to 5 
3. Compare target with list[index] 
4. if target = list[index] then 
return index // success 
else if index >= N - 1 
return -1 // failure 
5. index  index + 1
Binary Search 
 Search through a sorted list of items 
 Sorted list is a pre-condition for Binary Search! 
 Repeatedly divides the search space (list) into 
two 
 Divide-and-conquer approach
Binary Search: An Example (Key Î List) 
Index: 
0 1 2 3 4 5 6 7 8 9 
Value: 5 10 18 30 45 50 60 65 70 80 
Target (Key) = 30 
First iteration: whole list (search space), compare with mid value 
Low Index (LI) = 0; High Index (HI) = 9 
Choose element with index (0 + 9) / 2 = 4 
Compare value at index 4 (45) with the key (30) 
30 is less than 45, so the target must be in the lower half of the 
list
Binary Search: An Example (Key Î List) 
Second Iteration: Lookup in the reduced search space 
Index: 
0 1 2 3 4 5 6 7 8 9 
Value: 5 10 18 30 45 50 60 65 70 80 
Low Index (LI) = 0; High Index (HI) = (4 - 1) = 3 
Choose element with index (0 + 3) / 2 = 1 
Compare value at index 1 (10) with the key (30) 
30 is greater than 10, so the target must be in the higher 
half of the (reduced) list
Binary Search: An Example (Key Î List) 
Third Iteration: Lookup in the further reduced search space 
Index: 
0 1 2 3 4 5 6 7 8 9 
Value: 5 10 18 30 45 50 60 65 70 80 
Low Index (LI) = 1 + 1 = 2; High Index (HI) = 3 
Choose element with index (2 + 3) / 2 = 2 
Compare value at index 2 (18) with the key (30) 
30 is greater than 18, so the target must be in the higher 
half of the (reduced) list
Binary Search: An Example (Key Î List) 
Fourth Iteration: Lookup in the further reduced search space 
Index: 
0 1 2 3 4 5 6 7 8 9 
Value: 5 10 18 30 45 50 60 65 70 80 
Low Index (LI) = 2 + 1 = 3; High Index (HI) = 3 
Choose element with index (3 + 3) / 2 = 3 
Compare value at index 3 (30) with the key (30) 
Key is found at index 3
Binary Search: An Example (Key Ï List) 
Index: 
0 1 2 3 4 5 6 7 8 9 
Value: 5 10 18 30 45 50 60 65 70 80 
Target (Key) = 40 
First iteration: Lookup in the whole list (search space) 
Low Index (LI) = 0; High Index (HI) = 9 
Choose element with index (0 + 9) / 2 = 4 
Compare value at index 4 (45) with the key (40) 
40 is less than 45, so the target must be in the lower half of 
the list
Binary Search: An Example (Key Ï List) 
Second Iteration: Lookup in the reduced search space 
Index: 
0 1 2 3 4 5 6 7 8 9 
Value: 5 10 18 30 45 50 60 65 70 80 
Low Index (LI) = 0; High Index (HI) = (4 - 1) = 3 
Choose element with index (0 + 3) / 2 = 1 
Compare value at index 1 (10) with the key (40) 
40 is greater than 10, so the target must be in the higher 
half of the (reduced) list
Binary Search: An Example (Key Ï List) 
Third Iteration: Lookup in the further reduced search space 
Index: 
0 1 2 3 4 5 6 7 8 9 
Value: 5 10 18 30 45 50 60 65 70 80 
Low Index (LI) = 1 + 1 = 2; High Index (HI) = 3 
Choose element with index (2 + 3) / 2 = 2 
Compare value at index 2 (18) with the key (40) 
40 is greater than 18, so the target must be in the higher 
half of the (reduced) list
Binary Search: An Example (Key Ï List) 
Fourth Iteration: Lookup in the further reduced search space 
Index: 
0 1 2 3 4 5 6 7 8 9 
Value: 5 10 18 30 45 50 60 65 70 80 
Low Index (LI) = 2 + 1 = 3; High Index (HI) = 3 
Choose element with index (3 + 3) / 2 = 3 
Compare value at index 3 (30) with the key (40) 
40 is greater than 30, so the target must be in the higher 
half of the (reduced) list
Binary Search: An Example (Key Ï List) 
Fifth Iteration: Lookup in the further reduced search space 
Index: 
0 1 2 3 4 5 6 7 8 9 
Value: 5 10 18 30 45 50 60 65 70 80 
Low Index (LI) = 3 + 1 = 4; High Index (HI) = 3 
Since LI > HI, Key does not exist in the list 
Stop; Key is not found
Binary Search Algorithm: Informal 
 Middle  (LI + HI) / 2 
 One of the three possibilities 
 Key is equal to List[Middle] 
o success and stop 
 Key is less than List[Middle] 
o Key should be in the left half of List, or it does not exist 
 Key is greater than List[Middle] 
o Key should be in the right half of List, or it does not exist 
 Termination Condition 
 List[Middle] is equal to Key (success) OR LI > HI 
(Failure)
Binary Search Algorithm 
 Input: Key, List 
 Initialisation: LI  0, HI  SizeOf(List) – 1 
 Repeat steps 1 and 2 until LI > HI 
1. Mid  (LI + HI) / 2 
2. If List[Mid] = Key then 
Return Mid // success 
Else If Key < List[Mid] then 
HI  Mid – 1 
Else 
LI  Mid + 1 
Return -1 // failure
Search Algorithms:Time Complexity 
 Time complexity of Sequential Search algorithm: 
 Best-case : O(1) comparison 
o target is found immediately at the first location 
 Worst-case: O(n) comparisons 
o Target is not found 
 Average-case: O(n) comparisons 
o Target is found somewhere in the middle 
 Time complexity of Binary Search algorithm: 
O(log(n))  This is worst-case
Outlook 
Next week, we’ll discuss basic sorting algorithms

More Related Content

What's hot (20)

PPTX
Arrays In C++
Awais Alam
 
PPT
C++ Arrays
أحمد محمد
 
PPT
Array Presentation (EngineerBaBu.com)
EngineerBabu
 
PPTX
Sorting and searching arrays binary search algorithm
David Burks-Courses
 
PPTX
Data structure using c module 1
smruti sarangi
 
PPT
Array Presentation
Deep Prajapati Microplacer
 
PDF
Sorting Algorithms
Mohammed Hussein
 
PPT
02 Arrays And Memory Mapping
Qundeel
 
PPT
Fundamentals of data structures
Niraj Agarwal
 
PPTX
Search algorithms master
Hossam Hassan
 
PPTX
Binary search
Raghu nath
 
PDF
Array data structure
maamir farooq
 
PPTX
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
Umesh Kumar
 
PPTX
Data structures in c#
SivaSankar Gorantla
 
PPT
Arrays and structures
Mohd Arif
 
PPTX
ARRAY
ayush raj
 
PPT
Chapter 5 ds
Hanif Durad
 
PPSX
Lecture 3 data structures & algorithms - sorting techniques - http://techiem...
Dharmendra Prasad
 
PDF
Chapter 14 Searching and Sorting
MuhammadBakri13
 
PPTX
Arrays in C++
Kashif Nawab
 
Arrays In C++
Awais Alam
 
C++ Arrays
أحمد محمد
 
Array Presentation (EngineerBaBu.com)
EngineerBabu
 
Sorting and searching arrays binary search algorithm
David Burks-Courses
 
Data structure using c module 1
smruti sarangi
 
Array Presentation
Deep Prajapati Microplacer
 
Sorting Algorithms
Mohammed Hussein
 
02 Arrays And Memory Mapping
Qundeel
 
Fundamentals of data structures
Niraj Agarwal
 
Search algorithms master
Hossam Hassan
 
Binary search
Raghu nath
 
Array data structure
maamir farooq
 
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
Umesh Kumar
 
Data structures in c#
SivaSankar Gorantla
 
Arrays and structures
Mohd Arif
 
ARRAY
ayush raj
 
Chapter 5 ds
Hanif Durad
 
Lecture 3 data structures & algorithms - sorting techniques - http://techiem...
Dharmendra Prasad
 
Chapter 14 Searching and Sorting
MuhammadBakri13
 
Arrays in C++
Kashif Nawab
 

Viewers also liked (20)

PPT
Arrays Data Structure
student
 
PPT
Linear Search & Binary Search
Reem Alattas
 
PPT
Data Structures- Part4 basic sorting algorithms
Abdullah Al-hazmy
 
PDF
Data Structures and Algorithms
Pierre Vigneras
 
PDF
intorduction to Arrays in java
Muthukumaran Subramanian
 
PPTX
Lecture 3 data structures and algorithms
Aakash deep Singhal
 
PDF
Trends and future of C++: Evolving a systems language for performance - by Bj...
devstonez
 
PPTX
Arrays C#
Raghuveer Guthikonda
 
PPTX
Data structures
Sneha Chopra
 
PPT
Data Structures- Part8 stacks and queues
Abdullah Al-hazmy
 
PPT
Data Structures- Part2 analysis tools
Abdullah Al-hazmy
 
PPT
Data Structures- Part9 trees simplified
Abdullah Al-hazmy
 
PPTX
3 searching algorithms in Java
Mahmoud Alfarra
 
PDF
C++11
ppd1961
 
PPT
Lecture 2a arrays
Victor Palmar
 
PPT
Arrays Basics
Nikhil Pandit
 
PPT
Data Structures- Part5 recursion
Abdullah Al-hazmy
 
PPTX
History of Google Local from 2004-2011
Mike Blumenthal
 
PPT
Tri Merge Sorting Algorithm
Ashim Sikder
 
KEY
One Long Argument
John Lynch
 
Arrays Data Structure
student
 
Linear Search & Binary Search
Reem Alattas
 
Data Structures- Part4 basic sorting algorithms
Abdullah Al-hazmy
 
Data Structures and Algorithms
Pierre Vigneras
 
intorduction to Arrays in java
Muthukumaran Subramanian
 
Lecture 3 data structures and algorithms
Aakash deep Singhal
 
Trends and future of C++: Evolving a systems language for performance - by Bj...
devstonez
 
Data structures
Sneha Chopra
 
Data Structures- Part8 stacks and queues
Abdullah Al-hazmy
 
Data Structures- Part2 analysis tools
Abdullah Al-hazmy
 
Data Structures- Part9 trees simplified
Abdullah Al-hazmy
 
3 searching algorithms in Java
Mahmoud Alfarra
 
C++11
ppd1961
 
Lecture 2a arrays
Victor Palmar
 
Arrays Basics
Nikhil Pandit
 
Data Structures- Part5 recursion
Abdullah Al-hazmy
 
History of Google Local from 2004-2011
Mike Blumenthal
 
Tri Merge Sorting Algorithm
Ashim Sikder
 
One Long Argument
John Lynch
 
Ad

Similar to Data Structures- Part3 arrays and searching algorithms (20)

DOCX
MODULE 5-Searching and-sorting
nikshaikh786
 
PDF
advanced searching and sorting.pdf
haramaya university
 
PPT
Array 2
Abbott
 
PPTX
Arrays.pptx
Epsiba1
 
PDF
03 Linear Arrays Memory Representations .pdf
KkSingh64
 
PDF
Data Structure & Algorithms - Operations
babuk110
 
PPT
Computer notes - Binary Search
ecomputernotes
 
PPTX
Different type of Arrays and multidimensional arrays
prathwinidevadiga1
 
PPTX
Introduction to Searching Algorithm for beginners
JoshuaEddyson1
 
PPT
ds 2-Arrays and its types and operations
kavita20193
 
PPTX
Array 1D.................................pptx
Hassan Kamal
 
PPT
computer notes - Data Structures - 32
ecomputernotes
 
PPT
ds 2Arrays.ppt
AlliVinay1
 
DOCX
PPS 5.5.BASIC ALGORITHMS SEARCHING (LINEAR SEARCH, BINARY SEARCH ETC.), BASI...
Sitamarhi Institute of Technology
 
PDF
Data-Structure-using-C-Rajesh-Pandey.pdf
mohanaprakasht
 
PPTX
Chapter #3 (Searchinmmmg ALgorithm).pptx
hekmatyarzahir44
 
PPTX
Data structure Unit - II Searching and Sorting.pptx
gavanisanjana
 
PPTX
Rahat &amp; juhith
Rj Juhith
 
PPTX
ARRAY PPT.pptx for mca finals placement
utkarshKatiyar32
 
PPT
arrays1.ppt python programme arrays insertion
bushraashraf639
 
MODULE 5-Searching and-sorting
nikshaikh786
 
advanced searching and sorting.pdf
haramaya university
 
Array 2
Abbott
 
Arrays.pptx
Epsiba1
 
03 Linear Arrays Memory Representations .pdf
KkSingh64
 
Data Structure & Algorithms - Operations
babuk110
 
Computer notes - Binary Search
ecomputernotes
 
Different type of Arrays and multidimensional arrays
prathwinidevadiga1
 
Introduction to Searching Algorithm for beginners
JoshuaEddyson1
 
ds 2-Arrays and its types and operations
kavita20193
 
Array 1D.................................pptx
Hassan Kamal
 
computer notes - Data Structures - 32
ecomputernotes
 
ds 2Arrays.ppt
AlliVinay1
 
PPS 5.5.BASIC ALGORITHMS SEARCHING (LINEAR SEARCH, BINARY SEARCH ETC.), BASI...
Sitamarhi Institute of Technology
 
Data-Structure-using-C-Rajesh-Pandey.pdf
mohanaprakasht
 
Chapter #3 (Searchinmmmg ALgorithm).pptx
hekmatyarzahir44
 
Data structure Unit - II Searching and Sorting.pptx
gavanisanjana
 
Rahat &amp; juhith
Rj Juhith
 
ARRAY PPT.pptx for mca finals placement
utkarshKatiyar32
 
arrays1.ppt python programme arrays insertion
bushraashraf639
 
Ad

Recently uploaded (20)

PDF
ARAL-Orientation_Morning-Session_Day-11.pdf
JoelVilloso1
 
PPTX
Post Dated Cheque(PDC) Management in Odoo 18
Celine George
 
PPTX
MENINGITIS: NURSING MANAGEMENT, BACTERIAL MENINGITIS, VIRAL MENINGITIS.pptx
PRADEEP ABOTHU
 
PPTX
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
PPTX
How to Create Odoo JS Dialog_Popup in Odoo 18
Celine George
 
PPTX
Identifying elements in the story. Arrange the events in the story
geraldineamahido2
 
PPTX
GRADE-3-PPT-EVE-2025-ENG-Q1-LESSON-1.pptx
EveOdrapngimapNarido
 
PPTX
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
PPTX
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
PDF
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
PDF
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
PPTX
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
PPTX
ASRB NET 2023 PREVIOUS YEAR QUESTION PAPER GENETICS AND PLANT BREEDING BY SAT...
Krashi Coaching
 
PPTX
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 
PDF
Geographical Diversity of India 100 Mcq.pdf/ 7th class new ncert /Social/Samy...
Sandeep Swamy
 
PDF
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
PPTX
QUARTER 1 WEEK 2 PLOT, POV AND CONFLICTS
KynaParas
 
PPTX
PPT-Q1-WEEK-3-SCIENCE-ERevised Matatag Grade 3.pptx
reijhongidayawan02
 
PDF
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
PDF
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
ARAL-Orientation_Morning-Session_Day-11.pdf
JoelVilloso1
 
Post Dated Cheque(PDC) Management in Odoo 18
Celine George
 
MENINGITIS: NURSING MANAGEMENT, BACTERIAL MENINGITIS, VIRAL MENINGITIS.pptx
PRADEEP ABOTHU
 
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
How to Create Odoo JS Dialog_Popup in Odoo 18
Celine George
 
Identifying elements in the story. Arrange the events in the story
geraldineamahido2
 
GRADE-3-PPT-EVE-2025-ENG-Q1-LESSON-1.pptx
EveOdrapngimapNarido
 
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
ASRB NET 2023 PREVIOUS YEAR QUESTION PAPER GENETICS AND PLANT BREEDING BY SAT...
Krashi Coaching
 
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 
Geographical Diversity of India 100 Mcq.pdf/ 7th class new ncert /Social/Samy...
Sandeep Swamy
 
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
QUARTER 1 WEEK 2 PLOT, POV AND CONFLICTS
KynaParas
 
PPT-Q1-WEEK-3-SCIENCE-ERevised Matatag Grade 3.pptx
reijhongidayawan02
 
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 

Data Structures- Part3 arrays and searching algorithms

  • 1. Data Structures I (CPCS-204) Week # 3: Arrays & Searching Algorithms
  • 2. Data Structures  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently  Types of data structures  Based on memory allocation o Static (or fixed sized) data structures (Arrays) o Dynamic data structures (Linked lists)  Based on representation o Linear (Arrays/linked lists) o Non-linear (Trees/graphs)
  • 3. Array: motivation  You want to store 5 numbers in a computer  Define 5 variables, e.g. num1, num2, ..., num5  What, if you want to store 1000 numbers?  Defining 1000 variables is a pity!  Requires much programming effort  Any better solution?  Yes, some structured data type o Array is one of the most common structured data types o Saves a lot of programming effort (cf. 1000 variable names)
  • 4. What is an Array?  A collection of data elements in which  all elements are of the same data type, hence homogeneous data o An array of students’ marks o An array of students’ names o An array of objects (OOP perspective!)  elements (or their references) are stored at contiguous/ consecutive memory locations  Array is a static data structure  An array cannot grow or shrink during program execution – its size is fixed
  • 5. Basic concepts  Array name (data)  Index/subscript (0...9)  The slots are numbered sequentially starting at zero (Java, C++)  If there are N slots in an array, the index will be 0 through N-1  Array length = N = 10  Array size = N x Size of an element = 40  Direct access to an element
  • 6. Homogeneity  All elements in the array must have the same data type Index: 0 1 2 3 4 5 6 7 8 9 Value: 5 10 18 30 45 50 60 65 70 80 Index: 0 1 2 3 4 Value: 5.5 10.2 18.5 45.6 60.5 Index: 0 1 2 3 4 Value: ‘A’ 10.2 55 ‘X’ 60.5 Not an array
  • 7. Contiguous Memory  Array elements are stored at contiguous memory locations Index: 0 1 2 3 4 5 6 7 8 9 Value: 5 10 18 30 45 50 60 65 70 80  No empty segment in between values (3 & 5 are empty – not allowed) Index: 0 1 2 3 4 5 6 7 8 9 Value: 5 10 18 45 60 65 70 80
  • 8. Using Arrays  Array_name[index]  For example, in Java  System.out.println(data[4]) will display 0  data[3] = 99 will replace -3 with 99
  • 9. Some more concepts data[ -1 ] always illegal data[ 10 ] illegal (10 > upper bound) data[ 1.5 ] always illegal data[ 0 ] always OK data[ 9 ] OK Q. What will be the output of? 1.data[5] + 10 2.data[3] = data[3] + 10
  • 10. Array’s Dimensionality  One dimensional (just a linear list)  e.g.,  Only one subscript is required to access an individual element  Two dimensional (matrix/table) 5 10 18 30 45 50 60 65 70 80  e.g., 2 x 4 matrix (2 rows, 4 columns) Col 0 Col 1 Col 2 Col 3 Row 0 20 25 60 40 Row 1 30 15 70 90
  • 11. Two dimensional Arrays  Let, the name of the two dimensional array is M 20 25 60 40 30 15 70 90  Two indices/subscripts are required (row, column)  First element is at row 0, column 0  M0,0 or M(0, 0) or M[0][0] (more common)  What is: M[1][2]? M[3][4]?
  • 12. Array Operations  Indexing: inspect or update an element using its index. Performance is very fast O(1) randomNumber = numbers[5]; numbers[20000] = 100;  Insertion: add an element at certain index – Start: very slow O(n) because of shift – End : very fast O(1) because no need to shift  Removal: remove an element at certain index – Start: very slow O(n) because of shift – End : very fast O(1) because no need to shift  Search: performance depends on algorithm 1) Linear: slow O(n) 2) binary : O(log n)  Sort: performance depends on algorithm 1) Bubble: slow O(n2) 2) Selection: slow O(n2) 3) Insertion: slow O(n2) 4)Merge : O (n log n)
  • 13. One Dimensional Arrays in Java To declare an array follow the type with (empty) []s int[] grade; //or int grade[]; //both declare an int array In Java arrays are objects so must be created with the new keyword  To create an array of ten integers: int[] grade = new int[10]; Note that the array size has to be specified, although it can be specified with a variable at run-time
  • 14. Arrays in Java  When the array is created memory is reserved for its contents  Initialization lists can be used to specify the initial values of an array, in which case the new operator is not used int[] grade = {87, 93, 35}; //array of 3 ints To find the length of an array use its .length property int numGrades = grade.length; //note: not .length()!!
  • 15. Searching Algorithms  Search for a target (key) in the search space  Search space examples are:  All students in the class  All numbers in a given list  One of the two possible outcomes  Target is found (success)  Target is not found (failure)
  • 16. Searching Algorithms Index: 0 1 2 3 4 Value: 20 40 10 30 60 Target = 30 (success or failure?) Target = 45 (success or failure?) Search strategy? List Size = N = 5 Min index = 0 Max index = 4 (N - 1)
  • 17. Sequential Search  Search in a sequential order  Termination condition  Target is found (success)  List of elements is exhausted (failure)
  • 18. Sequential Search Index: 0 1 2 3 4 Value: 20 40 10 30 60 Target = 30 Step 1: Compare 30 with value at index 0 Step 2: Compare 30 with value at index 1 Step 3: Compare 30 with value at index 2 Step 4: Compare 30 with value at index 3 (success)
  • 19. Sequential Search Index: 0 1 2 3 4 Value: 20 40 10 30 60 Target = 45 Step 1: Compare 45 with value at index 0 Step 2: Compare 45 with value at index 1 Step 3: Compare 45 with value at index 2 Step 4: Compare 45 with value at index 3 Step 5: Compare 45 with value at index 4 Failure
  • 20. Sequential Search Algorithm Given: A list of N elements, and the target 1. index  0 2. Repeat steps 3 to 5 3. Compare target with list[index] 4. if target = list[index] then return index // success else if index >= N - 1 return -1 // failure 5. index  index + 1
  • 21. Binary Search  Search through a sorted list of items  Sorted list is a pre-condition for Binary Search!  Repeatedly divides the search space (list) into two  Divide-and-conquer approach
  • 22. Binary Search: An Example (Key Î List) Index: 0 1 2 3 4 5 6 7 8 9 Value: 5 10 18 30 45 50 60 65 70 80 Target (Key) = 30 First iteration: whole list (search space), compare with mid value Low Index (LI) = 0; High Index (HI) = 9 Choose element with index (0 + 9) / 2 = 4 Compare value at index 4 (45) with the key (30) 30 is less than 45, so the target must be in the lower half of the list
  • 23. Binary Search: An Example (Key Î List) Second Iteration: Lookup in the reduced search space Index: 0 1 2 3 4 5 6 7 8 9 Value: 5 10 18 30 45 50 60 65 70 80 Low Index (LI) = 0; High Index (HI) = (4 - 1) = 3 Choose element with index (0 + 3) / 2 = 1 Compare value at index 1 (10) with the key (30) 30 is greater than 10, so the target must be in the higher half of the (reduced) list
  • 24. Binary Search: An Example (Key Î List) Third Iteration: Lookup in the further reduced search space Index: 0 1 2 3 4 5 6 7 8 9 Value: 5 10 18 30 45 50 60 65 70 80 Low Index (LI) = 1 + 1 = 2; High Index (HI) = 3 Choose element with index (2 + 3) / 2 = 2 Compare value at index 2 (18) with the key (30) 30 is greater than 18, so the target must be in the higher half of the (reduced) list
  • 25. Binary Search: An Example (Key Î List) Fourth Iteration: Lookup in the further reduced search space Index: 0 1 2 3 4 5 6 7 8 9 Value: 5 10 18 30 45 50 60 65 70 80 Low Index (LI) = 2 + 1 = 3; High Index (HI) = 3 Choose element with index (3 + 3) / 2 = 3 Compare value at index 3 (30) with the key (30) Key is found at index 3
  • 26. Binary Search: An Example (Key Ï List) Index: 0 1 2 3 4 5 6 7 8 9 Value: 5 10 18 30 45 50 60 65 70 80 Target (Key) = 40 First iteration: Lookup in the whole list (search space) Low Index (LI) = 0; High Index (HI) = 9 Choose element with index (0 + 9) / 2 = 4 Compare value at index 4 (45) with the key (40) 40 is less than 45, so the target must be in the lower half of the list
  • 27. Binary Search: An Example (Key Ï List) Second Iteration: Lookup in the reduced search space Index: 0 1 2 3 4 5 6 7 8 9 Value: 5 10 18 30 45 50 60 65 70 80 Low Index (LI) = 0; High Index (HI) = (4 - 1) = 3 Choose element with index (0 + 3) / 2 = 1 Compare value at index 1 (10) with the key (40) 40 is greater than 10, so the target must be in the higher half of the (reduced) list
  • 28. Binary Search: An Example (Key Ï List) Third Iteration: Lookup in the further reduced search space Index: 0 1 2 3 4 5 6 7 8 9 Value: 5 10 18 30 45 50 60 65 70 80 Low Index (LI) = 1 + 1 = 2; High Index (HI) = 3 Choose element with index (2 + 3) / 2 = 2 Compare value at index 2 (18) with the key (40) 40 is greater than 18, so the target must be in the higher half of the (reduced) list
  • 29. Binary Search: An Example (Key Ï List) Fourth Iteration: Lookup in the further reduced search space Index: 0 1 2 3 4 5 6 7 8 9 Value: 5 10 18 30 45 50 60 65 70 80 Low Index (LI) = 2 + 1 = 3; High Index (HI) = 3 Choose element with index (3 + 3) / 2 = 3 Compare value at index 3 (30) with the key (40) 40 is greater than 30, so the target must be in the higher half of the (reduced) list
  • 30. Binary Search: An Example (Key Ï List) Fifth Iteration: Lookup in the further reduced search space Index: 0 1 2 3 4 5 6 7 8 9 Value: 5 10 18 30 45 50 60 65 70 80 Low Index (LI) = 3 + 1 = 4; High Index (HI) = 3 Since LI > HI, Key does not exist in the list Stop; Key is not found
  • 31. Binary Search Algorithm: Informal  Middle  (LI + HI) / 2  One of the three possibilities  Key is equal to List[Middle] o success and stop  Key is less than List[Middle] o Key should be in the left half of List, or it does not exist  Key is greater than List[Middle] o Key should be in the right half of List, or it does not exist  Termination Condition  List[Middle] is equal to Key (success) OR LI > HI (Failure)
  • 32. Binary Search Algorithm  Input: Key, List  Initialisation: LI  0, HI  SizeOf(List) – 1  Repeat steps 1 and 2 until LI > HI 1. Mid  (LI + HI) / 2 2. If List[Mid] = Key then Return Mid // success Else If Key < List[Mid] then HI  Mid – 1 Else LI  Mid + 1 Return -1 // failure
  • 33. Search Algorithms:Time Complexity  Time complexity of Sequential Search algorithm:  Best-case : O(1) comparison o target is found immediately at the first location  Worst-case: O(n) comparisons o Target is not found  Average-case: O(n) comparisons o Target is found somewhere in the middle  Time complexity of Binary Search algorithm: O(log(n))  This is worst-case
  • 34. Outlook Next week, we’ll discuss basic sorting algorithms