Arrays
Arrays
Arrays are very useful when multiple elements of the same data type need to be stored and
processed.
An array is given a name, and its elements are referred to by their indices.
An array definition specifies a variable type, a name, and its size to indicate how many
elements it will contain.
The data type of an array element is known as the base type of the array.
An array must be declared and created before it can be used. This is a two-step process:
java
CopyEdit
int[] numbers;
For example, double[] x; is used to create a reference to a double-type array. Then, x = new
double[5]; creates an array of five double values and stores its reference in x.
Accessing Array Elements
An array element is accessed using the array name followed by an integer subscript (also
called an index) inside square brackets.
In Java arrays, the element at position 1 has an index of 0, the element at position 2 has an
index of 1, and so on.
Array Storage
Single-dimensional arrays are essentially lists of information of the same type, and their elements
are stored in contiguous memory locations in index order.
o A group of contiguous memory locations that all share the same name and data
type.
o A reference variable that stores the starting memory address of the array
elements.
o A separate instance variable storing the total number of elements in the array.
java
CopyEdit
double[] x;
x = new double[5];
// Accessing an element
x[0] = 3.14;
java
CopyEdit
Out-of-Bounds Access
Subscripts other than 0 to n-1 (both inclusive) for an array with n elements are called out-
of-bound subscripts.
Two-Dimensional Arrays
A two-dimensional array is an array where each element is itself an array. It consists of rows and
columns:
java
CopyEdit
dataType[][] arrayName;
int[][] matrix = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
mathematica
CopyEdit
1. Linear Search
Linear search is a technique where each element of an array is compared with the search item one
by one until:
Worst-case scenario: If the search item is one of the last elements, many comparisons will be
needed, making the process time-consuming.
2. Binary Search
Binary search is an efficient searching technique that works only for sorted arrays.
Steps:
1. Compare the search item with the middle element of the array.
3. If the search item is less than the middle element (in ascending order), search in the first
half of the array.
Key Differences:
Binary search works only for sorted arrays and is faster than linear search.
Sorting in Arrays
Sorting means arranging array elements in ascending or descending order. Common sorting
techniques include:
Bubble Sort
Selection Sort
Insertion Sort
Merge Sort
Quick Sort
java
CopyEdit
class LinearSearch {
if (arr[i] == key) {
CopyEdit
import java.util.Arrays;
class BinarySearch {
if (arr[mid] == key)
return mid;
left = mid + 1;
else
right = mid - 1;
Selection Sort
The basic idea of selection sort is to repeatedly select the smallest key in the remaining unsorted
part of the array. Selection sort works by:
3. Repeating the process for the next smallest (or largest) element.
After sorting:
Bubble Sort
The basic idea of bubble sort is to compare adjacent elements and swap them if they are not in the
correct order.
How it works:
The largest element moves to the highest index position after each pass.
This is achieved by repeatedly comparing two adjacent elements and swapping them if
needed.
Example: Sorting an array in ascending order using bubble sort moves the largest values to the
rightmost positions first.
Important Consequences:
1. The == operator compares references (memory addresses), not the actual data stored in
arrays.
2. The .equals() method, when used with arrays, also compares references and not the data
inside the arrays.
Example in Java:
java
CopyEdit
java
CopyEdit
class SelectionSort {
int n = arr.length;
int minIndex = i;
minIndex = j;
arr[minIndex] = arr[i];
arr[i] = temp;
sort(arr);
java
CopyEdit
class BubbleSort {
int n = arr.length;
// Swap
arr[j + 1] = temp;
sort(arr);
Advantages of Arrays
3. Not Suitable for Dynamic Needs – Cannot adjust memory size at runtime.
In Java, arrays are reference types, meaning they behave like objects. As a result, when you use an
assignment statement such as array1 = array2;, it copies the reference (memory address) from
array2 to array1. This means both array1 and array2 will refer to the same array object.
java
CopyEdit
array2[4] = 13;
Explanation:
The assignment array1 = array2; makes array1 and array2 refer to the same object.
Modifying array2[4] to 13 also modifies array1[4] since both reference the same array.