CPCS204!02!03-Arrays - Overview - OperationsAnalysing
CPCS204!02!03-Arrays - Overview - OperationsAnalysing
CP
CS
20
4
CP
CS
20
4
ARRAYS
Introduction
CP Motivation
CS
• You want to store 5 numbers in a program
20 o No problem. You define five int variables:
4 int num1, num2, num3, num4, num5;
o Easy enough, right?
o But what if you want to store 1000 numbers?
• Are you really going to make 1000 separate variables?
int num1, num2,..., num998, num999, num1000;
• That would be CRAZY!
• So what is the solution?
o A data structure! Specifically, an array!
• An array is one of the most common data structures.
3 Dr. Jonathan (Yahya) Cazalas Dr. Muhammad Umair Ramzan
Data Structures - I
“No Bonus” marks for this course anymore
CP Basic Concepts
CS
• Array name (data)
20
• Index/subscript (0...9)
4
• 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
4 Dr. Jonathan (Yahya) Cazalas Dr. Muhammad Umair Ramzan
Data Structures - I
“No Bonus” marks for this course anymore
CP Using Arrays
CS
• Array_name[index]
20
4 • For example, in Java
o System.out.println(data[4]);
• will display 0
o data[3] = 99;
• Will replace -3 with 99
CP Using Arrays
CS
• data[ -1 ] What will be the output of?
20 o illegal data[5] + 10
data[3] = data[3] + 10
4 • data[ 10 ]
o illegal (10 > upper bound)
• data[ 1.5 ]
o illegal
• data[ 0 ]
o OK
• data[ 9 ]
o OK
6 Dr. Jonathan (Yahya) Cazalas Dr. Muhammad Umair Ramzan
Data Structures - I
“No Bonus” marks for this course anymore
CP Array Dimensionality
CS
• One dimensional (just a linear list)
20
4 5 10 18 30 45 50 60 65 70 80
o Only one subscript is required to access an
individual element
• Two dimensional (matrix or table)
Col 0 Col 1 Col 2 Col 3
Row 0 20 25 60 40
Row 1 30 15 70 90
7 Dr. Jonathan (Yahya) Cazalas Dr. Muhammad Umair Ramzan
Data Structures - I
“No Bonus” marks for this course anymore
CP 2D Arrays
CS
• Given the following array (whose name is
20
‘M’) 20 25 60 40
4
30 15 70 90
o Two indices/subscripts are now required to
reference a given cell
• M[0][0] ?
• M[1][2] ?
• M[3][4] ?
8 Dr. Jonathan (Yahya) Cazalas Dr. Muhammad Umair Ramzan
Data Structures - I
“No Bonus” marks for this course anymore
CP Array Declaration
CS
• You declare an array as follows:
20
int[] grades;
4
• This simply makes a an array variable (grades)
o But it does NOT specify where that variable refers to
• We can also declare the following:
int[] grades = new int[10];
o Now the array variable grades refers to an array of
ten integers
• By default, numeric elements are initialized to zero
9 Dr. Jonathan (Yahya) Cazalas Dr. Muhammad Umair Ramzan
Data Structures - I
“No Bonus” marks for this course anymore
CP
CS
20
4
ARRAY
As a Data Structure
CP What is an Array?
CS
• An array is a data structure
20
o It is a collection of multiple values of same
4 type
o Examples:
o An array of student grades
o An array of student names
o An array of objects (OOP perspective!)
CP Array Characteristics
CS
• Homogeneity
20 o All elements in an array must have the same data type
4 • Contiguous Memory
o Array elements (or their references) are stored in
contiguous/consecutive memory locations
• Direct access to an element
o Index reference is used to access it directly
• Static data structure
o An array cannot grow or shrink during program
execution…the size is fixed
16 Dr. Jonathan (Yahya) Cazalas Dr. Muhammad Umair Ramzan
Data Structures - I
“No Bonus” marks for this course anymore
CP
CS
20
4
Operations on ARRAY
As a Data Structure
CP Array Operations
CS • Accessing/indexing an element using its index
20 o Performance is very fast
4 o We can access an index immediately without searching
o myArray [1250] = 55;
o we immediately access array spot 1250 of myArray
public static void access(double[] a, int index) {
CP Array Operations
CS • Traversing an array
20 o Display all contents of an array
4 • All elements will be displayed
• Every elements will be displayed exactly once
System.out.println (a[i]);
}
CP Array Operations
CS
• Insertion: add an element at a certain
20
index
4 o What if we want to add an element at the
beginning?
• This would be a very slow operation! Why?
o Because we would have to shift ALL other elements over
one position
• What if we add an element at the end?
o It would be FAST. Why? No need to shift.
Array - Insertion
CS a[i] = a[i-1];
a[i] = value;
20 }
else{
4 int j = 0;
while ( j < a . length -1){
if (a[j+1] > value)
break;
j++;
}
for (int k = a.length-1; k > j+1; k--)
a[k] = a[k-1];
if ( j == a.length-1)
System. out. println(“Array Full”);
else
a[k] = value;
}
}
CP Array Operations
CS
• Deletion: remove an element at a certain
20
index
4 o Remove an element at the beginning of the
array
• Performance is again very slow.
o Because ALL elements need to shift one position
backwards
o Remove an element at the end of an array
• Very fast because of no shifting needed
Array - Deletion
a[i-1] = a[i];
a[i-1] = + ∞;// maximum value of domain range
20 }
else{
4 int j = 0;
while ( j < a . Length-1){
if (a[j+1] == value){
for (int k = j+1; k < a . Length-1; k++)
a[k] = a[k+1];
a[k] = + ∞;
break;
}
j++;
}
if ( j == a . Length – 1)
System.out.println (“Value is not found”);
}
CP Array Operations
CS
• Merging: combining the data of two or
20
more arrays into one
4 o Implementation is left for student exercise
public static void merge(int[] a, int[] b) {
CP Array Operations
CS
• Sorting: Arranging the values in ascending
20
or descending order
4 o Insertion Sort
o Selection Sort
o Quick Sort etc
CP
CS
20
4
ARRAYS
Searching
CP Array Operations
CS
• Searching through the array:
20 • Depends on the algorithm
4 • Some algorithms are faster than others
o More detail coming soon!
o Linear Search
o Binary Search
CP
CS
20
4
Operations on ARRAY
Linear Search
CP Linear Search
CS public static boolean search(int[] a, int value) {
for (int i=0; i<a.length; i++) {
20 if (a[i] == value) {
return true;
4 }
}
return false;
}
CP Linear Search
CS public static int search(int[] a, int value) {
for (int i=0; i<a.length; i++) {
20 if (a[i] == value) {
return i;
4 }
}
return -1;
}
CP
CS
20
4
Operations on ARRAY
Linear Search
Analysis
31 Dr. Jonathan (Yahya) Cazalas Dr. Muhammad Umair Ramzan
Data Structures - I
“No Bonus” marks for this course anymore
CP
CS
20
4
Operations on ARRAY
Binary Search
• In this case:
o The lowest index is 0
o The highest index is 8
o So the middle index is 4
37 Dr. Jonathan (Yahya) Cazalas Dr. Muhammad Umair Ramzan
Data Structures - I
“No Bonus” marks for this course anymore
20 value 2 6 19 27 33 37 38 41 118
4 • We would ask, “is the number I am searching for, 19, greater or less
than the number stored in index 4?
o Index 4 stores 33
• The answer would be “less than”
• So we would modify our search range to in between index 0 and
index 3
o Note that index 4 is no longer in the search space
• We then continue this process
o The second index we’d look at is index 1, since (0+3)/2=1
o Then we’d finally get to index 2, since (2+3)/2 = 2
o And at index 2, we would find the value, 19, in the array
38 Dr. Jonathan (Yahya) Cazalas Dr. Muhammad Umair Ramzan
Data Structures - I
“No Bonus” marks for this course anymore
CP
CS
20
4
Operations on ARRAY
Binary Search
Analysis
41 Dr. Jonathan (Yahya) Cazalas Dr. Muhammad Umair Ramzan
Data Structures - I
“No Bonus” marks for this course anymore
CP
CS
20
4
List Matching
Problem
if (lst1[i].compareTo(lst2[j])==0) {
System.out.println(lst1[i]);
break;
}
}
}
}
50 Dr. Jonathan (Yahya) Cazalas Dr. Muhammad Umair Ramzan
Data Structures - I
“No Bonus” marks for this course anymore
if (binSearch(list2, list1[i]))
System.out.println(list1[i]);
}
}
CP
CS
20
4
Allah (Alone) is
Sufficient for us, and He
is the Best Disposer of
affairs (for us).(Quran 3 : 173)
56 Dr. Jonathan (Yahya) Cazalas Dr. Muhammad Umair Ramzan