Chapter-
Array
Definition of an Array :-
"An array is a collection of variables of the same data type that are referenced by a common
name."
Types of an array - 2 type
1. Single dimensional array :- The simplest form of an array is the single dimensional array. The
array is given a name and its elements are referred to by their subscripts or indices. In C
language, the array's index numbering starts with zero. The index of the first element is known as
the lower bound and the index number of the last element is known as the upper bound.
Declaration of single dimensional array -
data_type array_name [size];
Where the data type declares the base type of array which is the type of each element in the
array, the array name is specified the name with which the array will be referenced and size
defines how many elements the array will hold. The size must be an integer value or integer
constant without any sign.
For eg,
int marks[10];
Initialisation of a single dimensional array :-
data_type array_name []={element -1, element -2, .... element -n};
For eg,
int marks[5]={15,25,89,65,55};
OR
float price[3]={25.50,15.25,35.55};
OR
char grade[5]={'A','B',',C','D','E'};
Accessing an element at a particular index for one dimensional arrays :-
Individual elements of an array can be accessed using the following syntax-
array_name [index or subscript];
For eg,
To assign a value to the second location of the array, we give the following statement :
marks[1]=90;
Similarly for reading the value of the 4th element in array_name marks, we give the following
statement :
scanf("%d", & marks[3]);
For writing (printing) the value of the second element in array_name marks, we give the
following statement :
printf("%d",marks[1]);
Note:-
Arrays can always be read or written through a loop. If we read one dimensional array,
requires one loop for reading and another for writing (printing) the array.
For example,
a) for reading the array :
For reading the marks of 10 students,
for{I=0;I<10;I++)
{
scanf("%d",&marks[I]);
}
b) for writing(print) the array :
for(I=0;I<10;I++)
{
printf("%d",marks[I]);
}
2. Two dimensional array :- Like single or one dimensional array has only one subscript or index
number, in the same way- two dimensional arrays have two subscript or index numbers. This is
also called a matrix. As in a matrix, every element is pointed by its row as well as column, in the
same way 4 pointing the element to subscript are used in which the first subscript is called row
while the second sab script is called column.
Declaration of two dimensional array -
data_type array_name[row_number][column_number];
For eg,
int sum[2][3];
Initialisation of a two dimensional array :-
data_type array_name[row][column]={{values of row1}{values of row2}{values of row
n}};
OR
data_type array_name[row][column]={{values of row,}values of row2,...,values of row
n}};
For eg,
int sum[2][3]={5,6,7,8,9,10,11,12,13};
OR
int sum[2][3]={{5,6,7}{8,9,10}{11,12,13}};
Linear Search (Linear / Sequential Search)
Linear search is the simplest method where you check every element of the array one by
one from the starting index to the end until the element is found.
Condition: The array elements can be in any order (sorted or unsorted).
Logic: A loop runs from index 0 to n-1 to check if Array[index] == target.
Efficiency: Slow for large datasets.
Example of Linear Search
Let's search for the target value 55 in an unsorted array:
Index 0 1 2 3 4
2
Value 10 55 8 90
3
Step 1: Compare 55 with index 0 (10). Not a match.
Step 2: Compare 55 with index 1 (23). Not a match.
Step 3: Compare 55 with index 2 (55). Match found! Stop searching and return index 2.
Binary Search (Binary / Half-Interval Search)
Binary search is an efficient algorithm that works on the principle of Divide and Conquer by
splitting the array into halves.
Strict Condition: The array must be sorted either in ascending or descending order.
Logic: Find the middle element. If the target is smaller than the middle, discard the right half. If
greater, discard the left half. Repeat.
Variables Used: low (start index), high (end index), and mid (middle index calculated as (low +
high) / 2). Example of Binary Search
Let's search for the target value 70 in a sorted array:
Index 0 1 2 3 4 5 6
Valu 2 6
10 30 40 50 70
e 0 0
Step 1: Set low = 0, high = 6.
o Calculate mid = (0 + 6) / 2 = 3.
o Value at index 3 is 40.
o Since our target 70 > 40, discard the left half. Set low = mid + 1 = 4.
Step 2: Now low = 4, high = 6.
o Calculate mid = (4 + 6) / 2 = 5.
o Value at index 5 is 60.
Since our target 70 > 60, discard the left half again. Set low = mid + 1 = 6.
Step 3: Now low = 6, high = 6.
o Calculate mid = (6 + 6) / 2 = 6.
o Value at index 6 is 70. Match found! Return index 6.
Key Differences
Feature Linear Search Binary Search
Array Requirement Sorted or Unsorted Must be Sorted
Working Principle Sequential checks Divide and Conquer)
Speed / Process Slow process Very fast process
Complexity Simple implementation Complex implementation
Sorting-
Sorting is the process of arranging data or elements in a specific logical order, which can
be either ascending order (increasing order, e.g., 1 to 10) or descending order (decreasing order,
e.g., 10 to 1)
Bubble sort, selection sort, and insertion sort are fundamental sorting algorithms. They
rearrange an array or list of elements in a specific order (like ascending or descending).
1. Bubble Sort
In bubble sort, adjacent elements (elements next to each other) are compared, and they are
swapped if they are in the wrong order. With each pass, the largest unsorted element "bubbles"
to its correct position at the end of the list.
2. Selection Sort
Selection sort repeatedly finds the minimum (smallest) element in the unsorted part of the list
and swaps it with the first element of that unsorted part. This places the smallest elements at the
beginning, one by one.
3. Insertion Sort
Insertion sort is similar to how you organize a hand of playing cards. It divides the list into a
sorted part and an unsorted part. It takes the first element from the unsorted part and inserts it
into its correct position in the sorted part.
Character array and string