SHS DataStruct Module ICT-12-Reviewer
SHS DataStruct Module ICT-12-Reviewer
MODULE CONTENT:
Module 1-2: REFRESHER ON ARRAYS / ARRAY ALGORITHMS
Array Summary - main points
Purpose To save large numbers of data elements, either primitives or objects.
Universal Almost all programming languages have arrays.
Array Declaration
Same type All elements in an array must be the same type, either primitive or object.
Declaration Precede variable with element type and square brackets. Eg,
int[] scores;
Allocation Memory to store array elements must be allocated with new. Eg,
int[] scores; // Declares that scores is an array of integers.
scores = new int[12]; // Allocate memory to hold up to 12 score values.
Combined It's very common to combine the declaration with the allocation.
int[] scores = new int[12];//Declare and allocate memory in one statement.
Fixed size When memory is allocated for an array, it is a fixed size block. It does not expand.
Names The most common naming advice is to use plural names or collective nouns.
References Array variables are references to array objects. Two variables can reference the same array.
Array initial values
zero/null If no initial values are specified for array elements (see below), array elements are initialized to zero for
numbers, null for object references. This is the same as for all object fields.
Initialization A curly brace surrounded list of values can be specified on the declaration. This allocates exactly enough
space to hold the array. The following are equivalent.
String[] names = {"Juan", "Mario", "Julia"}; String[] names = new
String[3];
names[0] = "Juan";
names[1] = "Mario";
names[2] = "Julia";
For loops for loops are the most common way to iterate over array elements. The normal for loop is good for
going forwards, backwards, sub-ranges, comparing two elements, or on every nth element. If you are
just going forward over the entire array, the enhanced for loop is more readable.
//... Using standard for loop.
int[] scores = new int[12];
. . . Set values in the scores array.
int total = 0;
for (int i = 0; i < scores.length; i++) {
total += scores[i];
}
Two-dimensional arrays are usually visualized as a matrix, with rows and columns.
a[0][0] a[0][1] a[0][2] a[0][3] This diagram shows the array a with its corresponding subscripts.
a[1][0] a[1][1] a[1][2] a[1][3]
Many row and column indexes have a meaning, and aren't just simply arbitrary numbers. The following example
might be used to represent the number of accidents on the day of the month and the hour of the day. See
programming problems below for some examples using this array.
static final int DAYS = 31;
static final int HOURS = 24;
. . .
int[][] accidents = new int[DAYS][HOURS];
Initial values
You can assign initial values to an array in a manner very similar to one-dimensional arrays, but with an extra level
of braces. The dimension sizes are computed by the compiler from the number of values. This would allocate a 3x3
board
int[][] board = new int[][] {{1,0,0},{0,1,0},{1,2,1}};
The ArrayList class implements a dynamic array. A dynamic array varies in size during run time and is used
in applications where the size of an array may need to grow or shrink. An ArrayList object shifts elements
up one position when a new element is added at a specific index. Elements added to the end of the
ArrayList do not move existing elements. Removing an element from an ArrayList also shifts elements as
necessary to close the gap left by the removed element.
When using an ArrayList object, it is important to understand that only objects, not primitive types, can
be stored. Because the indexOf() method compares its object parameter to each element of the array,
it is important that the object’s class has an appropriately overridden equals() method.
Module 5: Bubble Sort Algorithm
Bubble sort is a simple algorithm which compares neighboring elements and swaps them if they are not
in the order.
public int[] bubbleSort(int[] data){
int lenD = data.length;
int tmp = 0;
for(int i = 0;i<lenD;i++){
for(int j = (lenD-1);j>=(i+1);j--){
if(data[j]<data[j-1]){
tmp = data[j];
data[j]=data[j-1];
data[j-1]=tmp;
}
}
}
return data;
}
Module 7: Recursion
In Java, a method that calls itself is known as a recursive method. And, this process is known as recursion.
A physical world example would be to place two parallel mirrors facing each other. Any object in between
them would be reflected recursively.
How Recursion works?
In the above example, we have called the recurse() method from inside the main method (normal
method call). And, inside the recurse() method, we are again calling the same recurse method. This
is a recursive call.
In order to stop the recursive call, we need to provide some conditions inside the method. Otherwise, the
method will be called infinitely.
Hence, we use the if...else statement (or similar approach) to terminate the recursive call inside the
method.
Module 8: Stack Data Structure
The stack is a linear data structure that is used to store the collection of objects. It is based on the Last
In-First-Out (LIFO) algorithm. Java collection framework provides many interfaces and classes to store
the collection of objects. One of them is the Stack class that provides different operations such as push,
pop, peek, etc.
There are many real-life examples of a stack. Consider the simple example
of plates stacked over one another in a canteen. The plate which is at the
top is the first one to be removed, i.e. the plate which has been placed at
the bottommost position remains in the stack for the longest period of
time. So, it can be simply seen to follow LIFO order.