SlideShare a Scribd company logo
1
ARRAYS
2
OBJECTIVE
• Introduces:
Linear array and it’s representation in main
memory.
Operations of a linear array
Multidimensional array
2-dimensional array representation in a
memory
3
CONTENTS
Introductions
Linear Array
Linear Array Representations in Memory
Traversing, Insert, and Delete Algorithms
Binary Search Algorithm
Multidimensional Array
Array
Representations in Memory
Inconsistency Matrix Algorithms
4
Introduction
• Data Structure can be classified as:
linear
non-linear
• Linear (elements arranged in sequential in memory
location) i.e. array & linear link-list
• Non-linear such as a tree and graph.
• Operations:
Traversing, Searching, Inserting, Deleting,
Sorting, Merging
• Array is used to store a fix size for data and a link-list
the data can be varies in size.
5
2.1 Introduction
• Advantages of an Array:
a) Very simple
b) Economy – if full use of memory
c) Random accessed at the same time
• Disadvantage: wasting memory if not
fully used
6
Linear Array
• Homogeneous data:
a) Elements are represented through indexes.
b) Elements are saved in sequential in memory locations.
• Number of elements, N –> length or size of an array.
If:
UB : upper bound ( the largest index)
LB : lower bound (the smallest index)
Then: N = UB – LB + 1
Length = N = UB when LB = 1
7
Linear Array
• All elements in A are written symbolically as, 1 .. n is the
subscript.
A1, A2, A3, .... , An
• i.e., FORTRAN, PL/1 and BASIC  A(1), A(2), ..., A(N)
Java  A[0], A[1], ..., A[N-1] , subscript starts from 0
LB = 0, UB = N–1
8
Representation of Array in a Memory
• The process to determine the address in a memory:
a) First address – base address.
b) Relative address to base address through index function.
Example: char X[100];
Let char uses 1 location storage.
If the base address is 1200 then the next element is in 1201.
Index Function is written as:
Loc (X[i]) = Loc(X[0]) + i , i is subscript and LB = 0
1200 1201 1202 1203
X[0] X[1] X[2]
9
Traversing Algorithm
 Traversing operation means visit every element once.
e.g. to print, etc.
 Example algorithm:
1. [Assign counter]
K=LB // LB = 0
2. Repeat step 2.1 and 2.2 while K <= UB // If LB = 0
2.1 [visit element]
do PROCESS on LA[K]
2.2 [add counter]
K=K+1
3. end repeat step 2
4. exit
10
Insertion Algorithm
 Insert item at the back is easy if there is a space. Insert
item in the middle requires the movement of all elements
to the right as in Figure 1.
0 1 2 3 4 k MAX_LIST-1
1 2 3 4 5 k+1 MAX_LIST
12 3 44 19 100 … 5 10 18 ? … ?k+1
size
Array indexes New item
ADT list positions
items
Figure 1: Shifting items for insertion at position 3
11
Insertion Algorithm
 Example algorithm:
INSERT(LA, N, K, ITEM)
//LA is a linear array with N element
//K is integer positive where K < N and LB = 0
//Insert an element, ITEM in index K
1. [Assign counter]
J = N – 1; // LB = 0
2. Repeat step 2.1 and 2.2 while J >= K
2.1 [shift to the right all elements from J]
LA[J+1] = LA[J]
2.2 [decrement counter] J = J – 1
3. [Stop repeat step 2]
4. [Insert element] LA[K] = ITEM
5. [Reset N] N = N + 1
6. Exit
12
Deletion Algorithm
 Delete item.
(a)
0 1 2 3 4 k-1 k MAX_LIST-1
1 2 3 4 5 k k+1 MAX_LIST
12 3 44 100 … 5 10 18 ? … ?k
size
Array indexes
Delete 19
ADT list positions
items
Figure 2: Deletion causes a gap
13
Deletion Algorithm
(b)
0 1 2 3 k-1 MAX_LIST-1
1 2 3 4 k MAX_LIST
12 3 44 100 … 5 10 18 ? … ?k
size
Array indexes
ADT list positions
items
Figure 3: Fill gap by shifting
14
Deletion Algorithm
 Example algorithm:
DELETE(LA, N, K, ITEM)
1. ITEM = LA[K]
2. Repeat for I = K to N–2 // If LB = 0
2.1 [Shift element ke J + 1, forward]
LA[I] = LA[I+1]
3. [end of loop]
4. [Reset N in LA]
N = N – 1
5. Exit
15
Binary Search Algorithm
• Binary search algorithm is efficient if the array is sorted.
• A binary search is used whenever the list starts to become
large.
• Consider to use binary searches whenever the list contains
more than 16 elements.
• The binary search starts by testing the data in the element at
the middle of the array to determine if the target is in the first
or second half of the list.
• If it is in the first half, we do not need to check the second
half. If it is in the second half, we do not need to test the first
half. In other words we eliminate half the list from further
consideration. We repeat this process until we find the target
or determine that it is not in the list.
16
Binary Search Algorithm
• To find the middle of the list, we need three variables, one to
identify the beginning of the list, one to identify the middle
of the list, and one to identify the end of the list.
• We analyze two cases here: the target is in the list (target
found) and the target is not in the list (target not found).
17
Binary Search Algorithm
• Target found case: Assume we want to find 22 in a sorted
list as follows:
• The three indexes are first, mid and last. Given first as 0 and
last as 11, mid is calculated as follows:
mid = (first + last) / 2
mid = (0 + 11) / 2 = 11 / 2 = 5
4 7 8 10 14 21 22 36 62 77 81 91
a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9] a[10] a[11]
18
Binary Search Algorithm
• At index location 5, the target is greater than the list value (22 > 21).
Therefore, eliminate the array locations 0 through 5 (mid is automatically
eliminated). To narrow our search, we assign mid + 1 to first and repeat
the search.
4 7 8 10 14 21 22 36 62 77 81 91
a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9] a[10] a[11]
0 5 11
first mid last
Target: 22Target: 22
22 > 21
19
Binary Search Algorithm
• The next loop calculates mid with the new value for first and
determines that the midpoint is now 8 as follows:
mid = (6 + 11) / 2 = 17 / 2 = 8
4 7 8 10 14 21 22 36 62 77 81 91
a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9] a[10] a[11]
6 8 11
first mid last
Target: 22Target: 22
22 < 62
20
Binary Search Algorithm
• When we test the target to the value at mid a second time, we discover that the
target is less than the list value (22 < 62). This time we adjust the end of the list
by setting last to mid – 1 and recalculate mid. This step effectively eliminates
elements 8 through 11 from consideration. We have now arrived at index location
6, whose value matches our target. This stops the search.
4 7 8 10 14 21 22 36 62 77 81 91
a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9] a[10] a[11]
6 6 7
first mid last
Target: 22Target: 22
22 equals 228 6 7
function terminates
first mid last
21
Binary Search Algorithm
• Target not found case: This is done by testing for first and last crossing:
that is, we are done when first becomes greater than last. Two conditions
terminate the binary search algorithm when (a) the target is found or (b)
first becomes larger than last. Assume we want to find 11 in our binary
search array.
4 7 8 10 14 21 22 36 62 77 81 91
a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9] a[10] a[11]
0 5 11
first mid last Target: 11Target: 11
11 < 21
22
Binary Search Algorithm
• The loop continues to narrow the range as we saw in the
successful search until we are examining the data at index
locations 3 and 4.
4 7 8 10 14 21 22 36 62 77 81 91
a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9] a[10] a[11]
0 2 4
first mid last
Target: 11Target: 11
11 > 8
23
Binary Search Algorithm
• These settings of first and last set the mid index to 3 as follows:
mid = (3 + 4) / 2 = 7 / 2 = 3
4 7 8 10 14 21 22 36 62 77 81 91
a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9] a[10] a[11]
3 3 4
first mid last Target: 11Target: 11
11 > 10
24
Binary Search Algorithm
• The test at index 3indicates that the target is greater than the list value, so we set first to mid
+ 1, or 4. We now test the data at location 4 and discover that 11 < 14. The mid is as
calculated as follows:
• At this point, we have discovered that the target should be between two adjacent values; in
other words, it is not in the list. We see this algorithmically because last is set to mid – 1,
which makes first greater than last, the signal that the value we are looking for is not in the
list.
4 7 8 10 14 21 22 36 62 77 81 91
a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9] a[10] a[11]
4 4 4
first mid last
Target: 11Target: 11
11 < 14 4 4 3
first mid last
Function terminates
25
Binary Search Algorithm
• Example algorithm:
DATA – sorted array
ITEM – Info
LB – lower bound
UB – upper bound
ST – start Location
MID – middle Location
LAST – last Location
26
Binary Search Algorithm
1. [Define variables]
ST = LB, LAST= UB;
MID = (ST+LAST)/2;
2. Repeat 3 and 4 DO ST <= LAST & DATA[MID] != ITEM
3. If ITEM < DATA[MID] then
LAST = MID-1
If not
ST = MID+1
4. Set MID = INT((ST + LAST)/2)
[LAST repeat to 2]
5. If DATA[MID] = ITEM then
LOK = MID
If not,
LOK = NULL
6. Stop
27
2.3 MULTIDIMENSIONAL ARRAY
• Two or more subscripts.
28
2-D ARRAY
• A 2-D array, A with m X n elements.
• In math application it is called matrix.
• In business application – table.
• Example:
Assume 25 students had taken 4 tests.
The marks are stored in 25 X 4 array locations:
U0 U1 U2 U3
Stud 0 88 78 66 89
Stud 1 60 70 88 90
Stud 2 62 45 78 88
.. .. .. .. ..
.. .. .. .. ..
Stud 24 78 88 98 67
n
m
29
2-D ARRAY
• Multidimensional array declaration in Java:-
int [][] StudentMarks = new int [25][4];
StudentMarks[0][0] = 88;
StudentMarks[0][1] = 78;…..
OR
int [][] StudentMarks = {{88, 78, 66, 89},
{60, 70, 88, 90},…}
30
2-D ARRAY
• In Java the 2-D array is visualized as follows:
…
[0]
[1]
[2]
[3]
[4]
[5]
[6]
[24]
StudentMarks
88 78 66 89
60 70 88 90
62 45 78 88
[0] [1] [2] [3]
31
Exercises
• Find where the indicated elements of an
array a are stored, if the base address
of a is 200* and LB = 0
a) double a[10]; a[3]?
b) int a[26]; a[2]?
*(assume that int(s) are stored in 4 bytes
and double(s) in 8 bytes).

More Related Content

What's hot

Shell sorting
Shell sortingShell sorting
Shell sorting
TUC
 
linked lists in data structures
linked lists in data structureslinked lists in data structures
linked lists in data structures
DurgaDeviCbit
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
Lovely Professional University
 
Different Sorting tecniques in Data Structure
Different Sorting tecniques in Data StructureDifferent Sorting tecniques in Data Structure
Different Sorting tecniques in Data Structure
Tushar Gonawala
 
Trees, Binary Search Tree, AVL Tree in Data Structures
Trees, Binary Search Tree, AVL Tree in Data Structures Trees, Binary Search Tree, AVL Tree in Data Structures
Trees, Binary Search Tree, AVL Tree in Data Structures
Gurukul Kangri Vishwavidyalaya - Faculty of Engineering and Technology
 
Unit I-Data structures stack & Queue
Unit I-Data structures stack & QueueUnit I-Data structures stack & Queue
Unit I-Data structures stack & Queue
DrkhanchanaR
 
Doubly linked list (animated)
Doubly linked list (animated)Doubly linked list (animated)
Doubly linked list (animated)
DivyeshKumar Jagatiya
 
Lecture 3 data structures and algorithms
Lecture 3 data structures and algorithmsLecture 3 data structures and algorithms
Lecture 3 data structures and algorithmsAakash deep Singhal
 
Data structure and its types
Data structure and its typesData structure and its types
Data structure and its typesNavtar Sidhu Brar
 
linked list in Data Structure, Simple and Easy Tutorial
linked list in Data Structure, Simple and Easy Tutoriallinked list in Data Structure, Simple and Easy Tutorial
linked list in Data Structure, Simple and Easy Tutorial
Afzal Badshah
 
UNIT II LINEAR DATA STRUCTURES – STACKS, QUEUES
UNIT II 	LINEAR DATA STRUCTURES – STACKS, QUEUES	UNIT II 	LINEAR DATA STRUCTURES – STACKS, QUEUES
UNIT II LINEAR DATA STRUCTURES – STACKS, QUEUES
Kathirvel Ayyaswamy
 
Counting Sort
Counting SortCounting Sort
Counting Sort
Faiza Saleem
 
Quick sort
Quick sortQuick sort
Quick sort
Dhruv Sabalpara
 
Queue
QueueQueue
Introduction to data structure
Introduction to data structureIntroduction to data structure
Introduction to data structure
Zaid Shabbir
 
Queue data structure
Queue data structureQueue data structure
Queue data structure
anooppjoseph
 
Queues in C++
Queues in C++Queues in C++
Queues in C++
Vineeta Garg
 
Circular linked list
Circular linked listCircular linked list
Circular linked listdchuynh
 
Queue in Data Structure
Queue in Data Structure Queue in Data Structure
Queue in Data Structure
Janki Shah
 
Data structure using c++
Data structure using c++Data structure using c++
Data structure using c++
Prof. Dr. K. Adisesha
 

What's hot (20)

Shell sorting
Shell sortingShell sorting
Shell sorting
 
linked lists in data structures
linked lists in data structureslinked lists in data structures
linked lists in data structures
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
 
Different Sorting tecniques in Data Structure
Different Sorting tecniques in Data StructureDifferent Sorting tecniques in Data Structure
Different Sorting tecniques in Data Structure
 
Trees, Binary Search Tree, AVL Tree in Data Structures
Trees, Binary Search Tree, AVL Tree in Data Structures Trees, Binary Search Tree, AVL Tree in Data Structures
Trees, Binary Search Tree, AVL Tree in Data Structures
 
Unit I-Data structures stack & Queue
Unit I-Data structures stack & QueueUnit I-Data structures stack & Queue
Unit I-Data structures stack & Queue
 
Doubly linked list (animated)
Doubly linked list (animated)Doubly linked list (animated)
Doubly linked list (animated)
 
Lecture 3 data structures and algorithms
Lecture 3 data structures and algorithmsLecture 3 data structures and algorithms
Lecture 3 data structures and algorithms
 
Data structure and its types
Data structure and its typesData structure and its types
Data structure and its types
 
linked list in Data Structure, Simple and Easy Tutorial
linked list in Data Structure, Simple and Easy Tutoriallinked list in Data Structure, Simple and Easy Tutorial
linked list in Data Structure, Simple and Easy Tutorial
 
UNIT II LINEAR DATA STRUCTURES – STACKS, QUEUES
UNIT II 	LINEAR DATA STRUCTURES – STACKS, QUEUES	UNIT II 	LINEAR DATA STRUCTURES – STACKS, QUEUES
UNIT II LINEAR DATA STRUCTURES – STACKS, QUEUES
 
Counting Sort
Counting SortCounting Sort
Counting Sort
 
Quick sort
Quick sortQuick sort
Quick sort
 
Queue
QueueQueue
Queue
 
Introduction to data structure
Introduction to data structureIntroduction to data structure
Introduction to data structure
 
Queue data structure
Queue data structureQueue data structure
Queue data structure
 
Queues in C++
Queues in C++Queues in C++
Queues in C++
 
Circular linked list
Circular linked listCircular linked list
Circular linked list
 
Queue in Data Structure
Queue in Data Structure Queue in Data Structure
Queue in Data Structure
 
Data structure using c++
Data structure using c++Data structure using c++
Data structure using c++
 

Viewers also liked

Ifc modul 2 (array)
Ifc   modul 2 (array)Ifc   modul 2 (array)
Ifc modul 2 (array)
James Montolalu
 
RAO MUHAMMAD TANVEER HUSSAIN PIYA: +92 300 8852066: +92 345 8852066: +92 313 ...
RAO MUHAMMAD TANVEER HUSSAIN PIYA: +92 300 8852066: +92 345 8852066: +92 313 ...RAO MUHAMMAD TANVEER HUSSAIN PIYA: +92 300 8852066: +92 345 8852066: +92 313 ...
RAO MUHAMMAD TANVEER HUSSAIN PIYA: +92 300 8852066: +92 345 8852066: +92 313 ...
PPS
 
Linked list
Linked listLinked list
Linked listVONI
 
Materi : Struktur Data (2 Array)
Materi : Struktur Data (2 Array)Materi : Struktur Data (2 Array)
Materi : Struktur Data (2 Array)
eka pandu cynthia
 
Array Dimensi banyak struktur data pertemuan ke 3
Array Dimensi banyak struktur data pertemuan ke 3Array Dimensi banyak struktur data pertemuan ke 3
Array Dimensi banyak struktur data pertemuan ke 3
said zulhelmi
 
Pertemuan 6 Struktur Data, Algoritma dan Pemrograman
Pertemuan 6 Struktur Data, Algoritma dan PemrogramanPertemuan 6 Struktur Data, Algoritma dan Pemrograman
Pertemuan 6 Struktur Data, Algoritma dan Pemrograman
Prasetyo Adi
 
Array data structure
Array data structureArray data structure
Array data structure
maamir farooq
 
Data structure and algorithm.(dsa)
Data structure and algorithm.(dsa)Data structure and algorithm.(dsa)
Data structure and algorithm.(dsa)
mailmerk
 
Arrays
ArraysArrays
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithmsmultimedia9
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
Mohammed Hussein
 
Data Structures - Searching & sorting
Data Structures - Searching & sortingData Structures - Searching & sorting
Data Structures - Searching & sorting
Kaushal Shah
 
9. Searching & Sorting - Data Structures using C++ by Varsha Patil
9. Searching & Sorting - Data Structures using C++ by Varsha Patil9. Searching & Sorting - Data Structures using C++ by Varsha Patil
9. Searching & Sorting - Data Structures using C++ by Varsha Patil
widespreadpromotion
 

Viewers also liked (15)

Ifc modul 2 (array)
Ifc   modul 2 (array)Ifc   modul 2 (array)
Ifc modul 2 (array)
 
RAO MUHAMMAD TANVEER HUSSAIN PIYA: +92 300 8852066: +92 345 8852066: +92 313 ...
RAO MUHAMMAD TANVEER HUSSAIN PIYA: +92 300 8852066: +92 345 8852066: +92 313 ...RAO MUHAMMAD TANVEER HUSSAIN PIYA: +92 300 8852066: +92 345 8852066: +92 313 ...
RAO MUHAMMAD TANVEER HUSSAIN PIYA: +92 300 8852066: +92 345 8852066: +92 313 ...
 
Makalah Random Generator
Makalah Random GeneratorMakalah Random Generator
Makalah Random Generator
 
Linked list
Linked listLinked list
Linked list
 
Materi : Struktur Data (2 Array)
Materi : Struktur Data (2 Array)Materi : Struktur Data (2 Array)
Materi : Struktur Data (2 Array)
 
Array Dimensi banyak struktur data pertemuan ke 3
Array Dimensi banyak struktur data pertemuan ke 3Array Dimensi banyak struktur data pertemuan ke 3
Array Dimensi banyak struktur data pertemuan ke 3
 
Pertemuan 6 Struktur Data, Algoritma dan Pemrograman
Pertemuan 6 Struktur Data, Algoritma dan PemrogramanPertemuan 6 Struktur Data, Algoritma dan Pemrograman
Pertemuan 6 Struktur Data, Algoritma dan Pemrograman
 
Array data structure
Array data structureArray data structure
Array data structure
 
Data structure and algorithm.(dsa)
Data structure and algorithm.(dsa)Data structure and algorithm.(dsa)
Data structure and algorithm.(dsa)
 
Sorting
SortingSorting
Sorting
 
Arrays
ArraysArrays
Arrays
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
 
Data Structures - Searching & sorting
Data Structures - Searching & sortingData Structures - Searching & sorting
Data Structures - Searching & sorting
 
9. Searching & Sorting - Data Structures using C++ by Varsha Patil
9. Searching & Sorting - Data Structures using C++ by Varsha Patil9. Searching & Sorting - Data Structures using C++ by Varsha Patil
9. Searching & Sorting - Data Structures using C++ by Varsha Patil
 

Similar to Array 2

Data Structure and Algorithms Arrays
Data Structure and Algorithms ArraysData Structure and Algorithms Arrays
Data Structure and Algorithms Arrays
ManishPrajapati78
 
ds 2Arrays.ppt
ds 2Arrays.pptds 2Arrays.ppt
ds 2Arrays.ppt
AlliVinay1
 
Array operations
Array operationsArray operations
Array operations
Razzaaa
 
Sorting and hashing concepts
Sorting and hashing conceptsSorting and hashing concepts
Sorting and hashing concepts
LJ Projects
 
Sorting and hashing concepts
Sorting and hashing conceptsSorting and hashing concepts
Sorting and hashing concepts
LJ Projects
 
2. Array in Data Structure
2. Array in Data Structure2. Array in Data Structure
2. Array in Data Structure
Mandeep Singh
 
Unit 8 searching and hashing
Unit   8 searching and hashingUnit   8 searching and hashing
Unit 8 searching and hashing
Dabbal Singh Mahara
 
DS Unit 1.pptx
DS Unit 1.pptxDS Unit 1.pptx
DS Unit 1.pptx
chin463670
 
PPT Lecture 2.2.1 onn c++ data structures
PPT Lecture 2.2.1 onn c++ data structuresPPT Lecture 2.2.1 onn c++ data structures
PPT Lecture 2.2.1 onn c++ data structures
midtushar
 
Unit III Version I.pptx
Unit III Version I.pptxUnit III Version I.pptx
Unit III Version I.pptx
ssuserd602fd
 
ARRAY in python and c with examples .pptx
ARRAY  in python and c with examples .pptxARRAY  in python and c with examples .pptx
ARRAY in python and c with examples .pptx
abhishekmaurya102515
 
data structure and algorithm Array.pptx btech 2nd year
data structure and algorithm  Array.pptx btech 2nd yeardata structure and algorithm  Array.pptx btech 2nd year
data structure and algorithm Array.pptx btech 2nd year
palhimanshi999
 
Array ADT(Abstract Data Type)|Data Structure
Array ADT(Abstract Data Type)|Data StructureArray ADT(Abstract Data Type)|Data Structure
Array ADT(Abstract Data Type)|Data Structure
Akash Gaur
 
BCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHI
BCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHIBCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHI
BCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHI
Sowmya Jyothi
 
Arrays in C.pptx
Arrays in C.pptxArrays in C.pptx
Arrays in C.pptx
HarsimranKaur362773
 
Unit 7 sorting
Unit 7   sortingUnit 7   sorting
Unit 7 sorting
kalyanineve
 
Ocw chp6 2searchbinary
Ocw chp6 2searchbinaryOcw chp6 2searchbinary
Ocw chp6 2searchbinary
Prashant Rai
 
Updated Lab3.docx
Updated Lab3.docxUpdated Lab3.docx
Updated Lab3.docx
AleezaAnjum
 
DATA STRUCTURES USING C -ENGGDIGEST
DATA STRUCTURES USING C -ENGGDIGESTDATA STRUCTURES USING C -ENGGDIGEST
DATA STRUCTURES USING C -ENGGDIGEST
Swapnil Mishra
 
Chapter 10.ppt
Chapter 10.pptChapter 10.ppt
Chapter 10.ppt
MithuBose3
 

Similar to Array 2 (20)

Data Structure and Algorithms Arrays
Data Structure and Algorithms ArraysData Structure and Algorithms Arrays
Data Structure and Algorithms Arrays
 
ds 2Arrays.ppt
ds 2Arrays.pptds 2Arrays.ppt
ds 2Arrays.ppt
 
Array operations
Array operationsArray operations
Array operations
 
Sorting and hashing concepts
Sorting and hashing conceptsSorting and hashing concepts
Sorting and hashing concepts
 
Sorting and hashing concepts
Sorting and hashing conceptsSorting and hashing concepts
Sorting and hashing concepts
 
2. Array in Data Structure
2. Array in Data Structure2. Array in Data Structure
2. Array in Data Structure
 
Unit 8 searching and hashing
Unit   8 searching and hashingUnit   8 searching and hashing
Unit 8 searching and hashing
 
DS Unit 1.pptx
DS Unit 1.pptxDS Unit 1.pptx
DS Unit 1.pptx
 
PPT Lecture 2.2.1 onn c++ data structures
PPT Lecture 2.2.1 onn c++ data structuresPPT Lecture 2.2.1 onn c++ data structures
PPT Lecture 2.2.1 onn c++ data structures
 
Unit III Version I.pptx
Unit III Version I.pptxUnit III Version I.pptx
Unit III Version I.pptx
 
ARRAY in python and c with examples .pptx
ARRAY  in python and c with examples .pptxARRAY  in python and c with examples .pptx
ARRAY in python and c with examples .pptx
 
data structure and algorithm Array.pptx btech 2nd year
data structure and algorithm  Array.pptx btech 2nd yeardata structure and algorithm  Array.pptx btech 2nd year
data structure and algorithm Array.pptx btech 2nd year
 
Array ADT(Abstract Data Type)|Data Structure
Array ADT(Abstract Data Type)|Data StructureArray ADT(Abstract Data Type)|Data Structure
Array ADT(Abstract Data Type)|Data Structure
 
BCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHI
BCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHIBCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHI
BCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHI
 
Arrays in C.pptx
Arrays in C.pptxArrays in C.pptx
Arrays in C.pptx
 
Unit 7 sorting
Unit 7   sortingUnit 7   sorting
Unit 7 sorting
 
Ocw chp6 2searchbinary
Ocw chp6 2searchbinaryOcw chp6 2searchbinary
Ocw chp6 2searchbinary
 
Updated Lab3.docx
Updated Lab3.docxUpdated Lab3.docx
Updated Lab3.docx
 
DATA STRUCTURES USING C -ENGGDIGEST
DATA STRUCTURES USING C -ENGGDIGESTDATA STRUCTURES USING C -ENGGDIGEST
DATA STRUCTURES USING C -ENGGDIGEST
 
Chapter 10.ppt
Chapter 10.pptChapter 10.ppt
Chapter 10.ppt
 

More from Abbott

Chap007 MIS (Management Information System)
Chap007 MIS (Management Information System)Chap007 MIS (Management Information System)
Chap007 MIS (Management Information System)
Abbott
 
Chap006 MIS (Management Information System)
Chap006 MIS (Management Information System)Chap006 MIS (Management Information System)
Chap006 MIS (Management Information System)
Abbott
 
Chap005 MIS (Management Information System)
Chap005 MIS (Management Information System)Chap005 MIS (Management Information System)
Chap005 MIS (Management Information System)
Abbott
 
Chap004 MIS (Management Information System)
Chap004 MIS (Management Information System)Chap004 MIS (Management Information System)
Chap004 MIS (Management Information System)
Abbott
 
Chap003 MIS (Management Information System)
Chap003 MIS (Management Information System)Chap003 MIS (Management Information System)
Chap003 MIS (Management Information System)
Abbott
 
Chap002 (Management Information System)
Chap002 (Management Information System)Chap002 (Management Information System)
Chap002 (Management Information System)
Abbott
 
Chap001 MIS (Management Information System)
Chap001 MIS (Management Information System)Chap001 MIS (Management Information System)
Chap001 MIS (Management Information System)
Abbott
 
Stacks and queues
Stacks and queuesStacks and queues
Stacks and queuesAbbott
 
Practice programs
Practice programsPractice programs
Practice programsAbbott
 
linked list
linked listlinked list
linked list
Abbott
 
Data structure lecture 2
Data structure lecture 2Data structure lecture 2
Data structure lecture 2
Abbott
 
Data structure lecture 2 (pdf)
Data structure lecture 2 (pdf)Data structure lecture 2 (pdf)
Data structure lecture 2 (pdf)
Abbott
 
Ch17
Ch17Ch17
Ch17
Abbott
 

More from Abbott (13)

Chap007 MIS (Management Information System)
Chap007 MIS (Management Information System)Chap007 MIS (Management Information System)
Chap007 MIS (Management Information System)
 
Chap006 MIS (Management Information System)
Chap006 MIS (Management Information System)Chap006 MIS (Management Information System)
Chap006 MIS (Management Information System)
 
Chap005 MIS (Management Information System)
Chap005 MIS (Management Information System)Chap005 MIS (Management Information System)
Chap005 MIS (Management Information System)
 
Chap004 MIS (Management Information System)
Chap004 MIS (Management Information System)Chap004 MIS (Management Information System)
Chap004 MIS (Management Information System)
 
Chap003 MIS (Management Information System)
Chap003 MIS (Management Information System)Chap003 MIS (Management Information System)
Chap003 MIS (Management Information System)
 
Chap002 (Management Information System)
Chap002 (Management Information System)Chap002 (Management Information System)
Chap002 (Management Information System)
 
Chap001 MIS (Management Information System)
Chap001 MIS (Management Information System)Chap001 MIS (Management Information System)
Chap001 MIS (Management Information System)
 
Stacks and queues
Stacks and queuesStacks and queues
Stacks and queues
 
Practice programs
Practice programsPractice programs
Practice programs
 
linked list
linked listlinked list
linked list
 
Data structure lecture 2
Data structure lecture 2Data structure lecture 2
Data structure lecture 2
 
Data structure lecture 2 (pdf)
Data structure lecture 2 (pdf)Data structure lecture 2 (pdf)
Data structure lecture 2 (pdf)
 
Ch17
Ch17Ch17
Ch17
 

Recently uploaded

Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
Safe Software
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Albert Hoitingh
 
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered QualitySoftware Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Inflectra
 
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Product School
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
Product School
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
KatiaHIMEUR1
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
Paul Groth
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
Laura Byrne
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
Product School
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Thierry Lestable
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
DianaGray10
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
Alison B. Lowndes
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
UiPathCommunity
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
ControlCase
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
91mobiles
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
OnBoard
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
Ana-Maria Mihalceanu
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Jeffrey Haguewood
 

Recently uploaded (20)

Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
 
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered QualitySoftware Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
 
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
 

Array 2

  • 2. 2 OBJECTIVE • Introduces: Linear array and it’s representation in main memory. Operations of a linear array Multidimensional array 2-dimensional array representation in a memory
  • 3. 3 CONTENTS Introductions Linear Array Linear Array Representations in Memory Traversing, Insert, and Delete Algorithms Binary Search Algorithm Multidimensional Array Array Representations in Memory Inconsistency Matrix Algorithms
  • 4. 4 Introduction • Data Structure can be classified as: linear non-linear • Linear (elements arranged in sequential in memory location) i.e. array & linear link-list • Non-linear such as a tree and graph. • Operations: Traversing, Searching, Inserting, Deleting, Sorting, Merging • Array is used to store a fix size for data and a link-list the data can be varies in size.
  • 5. 5 2.1 Introduction • Advantages of an Array: a) Very simple b) Economy – if full use of memory c) Random accessed at the same time • Disadvantage: wasting memory if not fully used
  • 6. 6 Linear Array • Homogeneous data: a) Elements are represented through indexes. b) Elements are saved in sequential in memory locations. • Number of elements, N –> length or size of an array. If: UB : upper bound ( the largest index) LB : lower bound (the smallest index) Then: N = UB – LB + 1 Length = N = UB when LB = 1
  • 7. 7 Linear Array • All elements in A are written symbolically as, 1 .. n is the subscript. A1, A2, A3, .... , An • i.e., FORTRAN, PL/1 and BASIC  A(1), A(2), ..., A(N) Java  A[0], A[1], ..., A[N-1] , subscript starts from 0 LB = 0, UB = N–1
  • 8. 8 Representation of Array in a Memory • The process to determine the address in a memory: a) First address – base address. b) Relative address to base address through index function. Example: char X[100]; Let char uses 1 location storage. If the base address is 1200 then the next element is in 1201. Index Function is written as: Loc (X[i]) = Loc(X[0]) + i , i is subscript and LB = 0 1200 1201 1202 1203 X[0] X[1] X[2]
  • 9. 9 Traversing Algorithm  Traversing operation means visit every element once. e.g. to print, etc.  Example algorithm: 1. [Assign counter] K=LB // LB = 0 2. Repeat step 2.1 and 2.2 while K <= UB // If LB = 0 2.1 [visit element] do PROCESS on LA[K] 2.2 [add counter] K=K+1 3. end repeat step 2 4. exit
  • 10. 10 Insertion Algorithm  Insert item at the back is easy if there is a space. Insert item in the middle requires the movement of all elements to the right as in Figure 1. 0 1 2 3 4 k MAX_LIST-1 1 2 3 4 5 k+1 MAX_LIST 12 3 44 19 100 … 5 10 18 ? … ?k+1 size Array indexes New item ADT list positions items Figure 1: Shifting items for insertion at position 3
  • 11. 11 Insertion Algorithm  Example algorithm: INSERT(LA, N, K, ITEM) //LA is a linear array with N element //K is integer positive where K < N and LB = 0 //Insert an element, ITEM in index K 1. [Assign counter] J = N – 1; // LB = 0 2. Repeat step 2.1 and 2.2 while J >= K 2.1 [shift to the right all elements from J] LA[J+1] = LA[J] 2.2 [decrement counter] J = J – 1 3. [Stop repeat step 2] 4. [Insert element] LA[K] = ITEM 5. [Reset N] N = N + 1 6. Exit
  • 12. 12 Deletion Algorithm  Delete item. (a) 0 1 2 3 4 k-1 k MAX_LIST-1 1 2 3 4 5 k k+1 MAX_LIST 12 3 44 100 … 5 10 18 ? … ?k size Array indexes Delete 19 ADT list positions items Figure 2: Deletion causes a gap
  • 13. 13 Deletion Algorithm (b) 0 1 2 3 k-1 MAX_LIST-1 1 2 3 4 k MAX_LIST 12 3 44 100 … 5 10 18 ? … ?k size Array indexes ADT list positions items Figure 3: Fill gap by shifting
  • 14. 14 Deletion Algorithm  Example algorithm: DELETE(LA, N, K, ITEM) 1. ITEM = LA[K] 2. Repeat for I = K to N–2 // If LB = 0 2.1 [Shift element ke J + 1, forward] LA[I] = LA[I+1] 3. [end of loop] 4. [Reset N in LA] N = N – 1 5. Exit
  • 15. 15 Binary Search Algorithm • Binary search algorithm is efficient if the array is sorted. • A binary search is used whenever the list starts to become large. • Consider to use binary searches whenever the list contains more than 16 elements. • The binary search starts by testing the data in the element at the middle of the array to determine if the target is in the first or second half of the list. • If it is in the first half, we do not need to check the second half. If it is in the second half, we do not need to test the first half. In other words we eliminate half the list from further consideration. We repeat this process until we find the target or determine that it is not in the list.
  • 16. 16 Binary Search Algorithm • To find the middle of the list, we need three variables, one to identify the beginning of the list, one to identify the middle of the list, and one to identify the end of the list. • We analyze two cases here: the target is in the list (target found) and the target is not in the list (target not found).
  • 17. 17 Binary Search Algorithm • Target found case: Assume we want to find 22 in a sorted list as follows: • The three indexes are first, mid and last. Given first as 0 and last as 11, mid is calculated as follows: mid = (first + last) / 2 mid = (0 + 11) / 2 = 11 / 2 = 5 4 7 8 10 14 21 22 36 62 77 81 91 a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9] a[10] a[11]
  • 18. 18 Binary Search Algorithm • At index location 5, the target is greater than the list value (22 > 21). Therefore, eliminate the array locations 0 through 5 (mid is automatically eliminated). To narrow our search, we assign mid + 1 to first and repeat the search. 4 7 8 10 14 21 22 36 62 77 81 91 a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9] a[10] a[11] 0 5 11 first mid last Target: 22Target: 22 22 > 21
  • 19. 19 Binary Search Algorithm • The next loop calculates mid with the new value for first and determines that the midpoint is now 8 as follows: mid = (6 + 11) / 2 = 17 / 2 = 8 4 7 8 10 14 21 22 36 62 77 81 91 a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9] a[10] a[11] 6 8 11 first mid last Target: 22Target: 22 22 < 62
  • 20. 20 Binary Search Algorithm • When we test the target to the value at mid a second time, we discover that the target is less than the list value (22 < 62). This time we adjust the end of the list by setting last to mid – 1 and recalculate mid. This step effectively eliminates elements 8 through 11 from consideration. We have now arrived at index location 6, whose value matches our target. This stops the search. 4 7 8 10 14 21 22 36 62 77 81 91 a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9] a[10] a[11] 6 6 7 first mid last Target: 22Target: 22 22 equals 228 6 7 function terminates first mid last
  • 21. 21 Binary Search Algorithm • Target not found case: This is done by testing for first and last crossing: that is, we are done when first becomes greater than last. Two conditions terminate the binary search algorithm when (a) the target is found or (b) first becomes larger than last. Assume we want to find 11 in our binary search array. 4 7 8 10 14 21 22 36 62 77 81 91 a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9] a[10] a[11] 0 5 11 first mid last Target: 11Target: 11 11 < 21
  • 22. 22 Binary Search Algorithm • The loop continues to narrow the range as we saw in the successful search until we are examining the data at index locations 3 and 4. 4 7 8 10 14 21 22 36 62 77 81 91 a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9] a[10] a[11] 0 2 4 first mid last Target: 11Target: 11 11 > 8
  • 23. 23 Binary Search Algorithm • These settings of first and last set the mid index to 3 as follows: mid = (3 + 4) / 2 = 7 / 2 = 3 4 7 8 10 14 21 22 36 62 77 81 91 a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9] a[10] a[11] 3 3 4 first mid last Target: 11Target: 11 11 > 10
  • 24. 24 Binary Search Algorithm • The test at index 3indicates that the target is greater than the list value, so we set first to mid + 1, or 4. We now test the data at location 4 and discover that 11 < 14. The mid is as calculated as follows: • At this point, we have discovered that the target should be between two adjacent values; in other words, it is not in the list. We see this algorithmically because last is set to mid – 1, which makes first greater than last, the signal that the value we are looking for is not in the list. 4 7 8 10 14 21 22 36 62 77 81 91 a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9] a[10] a[11] 4 4 4 first mid last Target: 11Target: 11 11 < 14 4 4 3 first mid last Function terminates
  • 25. 25 Binary Search Algorithm • Example algorithm: DATA – sorted array ITEM – Info LB – lower bound UB – upper bound ST – start Location MID – middle Location LAST – last Location
  • 26. 26 Binary Search Algorithm 1. [Define variables] ST = LB, LAST= UB; MID = (ST+LAST)/2; 2. Repeat 3 and 4 DO ST <= LAST & DATA[MID] != ITEM 3. If ITEM < DATA[MID] then LAST = MID-1 If not ST = MID+1 4. Set MID = INT((ST + LAST)/2) [LAST repeat to 2] 5. If DATA[MID] = ITEM then LOK = MID If not, LOK = NULL 6. Stop
  • 27. 27 2.3 MULTIDIMENSIONAL ARRAY • Two or more subscripts.
  • 28. 28 2-D ARRAY • A 2-D array, A with m X n elements. • In math application it is called matrix. • In business application – table. • Example: Assume 25 students had taken 4 tests. The marks are stored in 25 X 4 array locations: U0 U1 U2 U3 Stud 0 88 78 66 89 Stud 1 60 70 88 90 Stud 2 62 45 78 88 .. .. .. .. .. .. .. .. .. .. Stud 24 78 88 98 67 n m
  • 29. 29 2-D ARRAY • Multidimensional array declaration in Java:- int [][] StudentMarks = new int [25][4]; StudentMarks[0][0] = 88; StudentMarks[0][1] = 78;….. OR int [][] StudentMarks = {{88, 78, 66, 89}, {60, 70, 88, 90},…}
  • 30. 30 2-D ARRAY • In Java the 2-D array is visualized as follows: … [0] [1] [2] [3] [4] [5] [6] [24] StudentMarks 88 78 66 89 60 70 88 90 62 45 78 88 [0] [1] [2] [3]
  • 31. 31 Exercises • Find where the indicated elements of an array a are stored, if the base address of a is 200* and LB = 0 a) double a[10]; a[3]? b) int a[26]; a[2]? *(assume that int(s) are stored in 4 bytes and double(s) in 8 bytes).