0% found this document useful (0 votes)
3 views6 pages

Module Arrays

This module covers arrays, searching, and sorting in computer programming, including topics such as declaring arrays, accessing elements, and implementing searching and sorting algorithms. Students will learn to define arrays, create multidimensional arrays, and use linear and binary search as well as bubble and selection sort techniques. The module concludes with a laboratory exam requiring students to write a Java program that incorporates these concepts.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views6 pages

Module Arrays

This module covers arrays, searching, and sorting in computer programming, including topics such as declaring arrays, accessing elements, and implementing searching and sorting algorithms. Students will learn to define arrays, create multidimensional arrays, and use linear and binary search as well as bubble and selection sort techniques. The module concludes with a laboratory exam requiring students to write a Java program that incorporates these concepts.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

MODULE: ARRAYS, SEARCHING, AND SORTING

Computer Programming2

Topic Coverage:

• Introduction to Arrays
• Declaring Arrays
• Accessing Array Elements
• Array Length
• Multidimensional Arrays
• Searching Algorithms
• Sorting Algorithms

I. LEARNING OUTCOMES
At the end of this module, students should be able to:

➢ Define and explain arrays.


➢ Declare and initialize arrays properly.
➢ Access and manipulate array elements.
➢ Use the length property correctly.
➢ Create and use multidimensional arrays.
➢ Implement basic searching algorithms.
➢ Implement basic sorting algorithms.

II. ARRAYS

A. Introduction to Arrays
An array is a data structure that stores multiple values of the same data type in a single
variable. Arrays use index numbers starting from 0.

Instead of declaring: We can use:

int num1 = 10; int[] numbers = {10, 20, 30};


int num2 = 20;
int num3 = 30;

Key Characteristics of Arrays:

• Fixed size (cannot change after declaration)


• Stores elements of the same data type
• Uses index numbers
• Index starts at 0
B. Declaring Arrays
Syntax:

dataType[] arrayName = new dataType[size];

Example:

int[] scores = new int[5];


int[] scores = {85, 90, 78, 92, 88};

C. Accessing Array Elements


Use index: arrayName[index];

Example: scores[0]; //output: 85

Modification Example: scores[1] = 95; //element at index 1 will be


changed to 95

When using new, default values are assigned:

Data Type Default Value


int 0
double 0.0
boolean false
String null

Example: int[] nums = new int[3];


[Link](nums[0]); // output is 0

Looping Through Arrays

Using for loop: for(int i = 0; i < [Link]; i++) {


[Link](scores[i]);
}

Using enhanced for-loop: for(int score : scores) {


[Link](score);
}

D. Array Length
Arrays use .length to determine size.

Use the length property: [Link]

Example: int[] scores = {85, 90, 78};

[Link]([Link]); // output: 3

Note: It is length, not length().


Practical Example: Finding the Sum and Average

int[] scores = {85, 90, 78};

int sum = 0;

for(int i = 0; i < [Link]; i++) {

sum += scores[i];

double average = (double) sum / [Link];

[Link]("Sum: " + sum);

[Link]("Average: " + average);

E. Multidimensional Arrays
What is a Multidimensional Array?
A multidimensional array is an array of arrays.
Most common type: 2D Array (table-like structure).

Declaring a 2D Array: dataType[][] arrayName = new dataType[rows][columns];


Example: int[][] matrix = new int[2][3];
This creates: Col0 Col1 Col2
Row0 0 0 0
Row1 0 0 0

Initializing a 2D Array int[][] matrix = {


{1, 2, 3},
{4, 5, 6}
};

Accessing 2D Array Elements [Link](matrix[0][1]); // 2


[Link](matrix[1][2]); // 6

Looping Through 2D Arrays for(int i = 0; i < [Link]; i++) {


for(int j = 0; j < matrix[i].length; j++) {
[Link](matrix[i][j] + " ");
}
[Link]();
}

Output: 123
456
III. ADVANCED MODULE: SEARCHING ALGORITHMS

A. Linear Search
Linear Search checks each element one by one until the value is found.

Example Code:

public static int linearSearch(int[] arr, int key) {


for(int i = 0; i < [Link]; i++) {
if(arr[i] == key) {
return i;
}
}
return -1;
}

B. Binary Search
Binary Search works only on sorted arrays. It divides the array into halves.

Example Code:

public static int binarySearch(int[] arr, int key) {


int left = 0, right = [Link] - 1;
while(left <= right) {
int mid = (left + right) / 2;
if(arr[mid] == key)
return mid;
else if(arr[mid] < key)
left = mid + 1;
else
right = mid - 1;
}
return -1;
}

IV. ADVANCED MODULE: SORTING ALGORITHMS

A. Bubble Sort
Bubble Sort repeatedly swaps adjacent elements if they are in wrong order.

Example Code:

public static void bubbleSort(int[] arr) {


for(int i = 0; i < [Link] - 1; i++) {
for(int j = 0; j < [Link] - i - 1; j++) {
if(arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}

B. Selection Sort
Selection Sort finds the minimum element and places it at the beginning.

Example Code:

public static void selectionSort(int[] arr) {


for(int i = 0; i < [Link] - 1; i++) {
int minIndex = i;
for(int j = i + 1; j < [Link]; j++) {
if(arr[j] < arr[minIndex])
minIndex = j;
}
int temp = arr[minIndex];
arr[minIndex] = arr[i];
arr[i] = temp;
}
}

V. LABORATORY EXAM
Laboratory Exam: Arrays, Searching, and Sorting

Instructions: Write a Java program that performs the following tasks:

1. Ask the user to input 10 integers and store them in an array.

2. Display all elements.

3. Compute and display the sum and average.

4. Sort the array using Bubble Sort.

5. Allow the user to search for a number using Linear Search.

6. Display the index if found, otherwise display 'Not Found'.

Grading Rubric
Correct Declaration and Input - 20 points

Correct Computation (Sum & Average) - 20 points


Correct Sorting Implementation - 20 points

Correct Searching Implementation - 20 points

Program Output and Clean Code - 20 points

TOTAL: 100 points

You might also like