Prepeared By:- Ambika Prasad Mohanty
What is Arrays ?
Advantages / Uses of Arrays
Types of Arrays:
One-D Array
A type of array in which all elements are arranged in the form of a list is known as 1-D array or single dimensional array or linear list.
Declaring 1-D Array:
data_type identifier[length]; e.g: int marks[5];
0
1
Data type of values to be stored in the array. Name of the array.
Number of elements.
4
2 3
int marks
| | | | |
One-D array Intialization
The process of assigning values to array elements at the time of array declaration is called array initialization.
Syntax:
data_type identifier[length]={ List of values }; e.g:
{49,64,96,49};
Data type of values to be stored in the array.
Name of the array. Number of elements
o List of values: Values to initialize the array. Initializing values must be constant
Accessing element of array
Array_name[index];
int marks[5];
for(int i=0;i<=4;i++) marks[i]=i;
Searching In Array
Searching is a process of finding the required data in the array. Searching becomes more important when the length of the array is very large.
There are two techniques to searching elements in array as follows:
Sequential search Binary search
Sequential Search
Sequential search is also known as linear or serial search. It follows the following step to search a value in array.
Binary Search
Binary search is a quicker method of searching for value in the array. Binary search is very quick but it can only search an sorted array. It cannot be applied on an unsorted array.
Otherwise it searches the second half of the array. The process continues until the required number is found or loop completes without successful search.
Sorting Arrays
Sorting is a process of arranging the value of array in a particular order. An array can be sorted in two order.
12 | 25 | 33 | 37 | 48 |
48 | 37 | 33 | 25 | 12 |
Techniques Of Sorting Array
There are two techniques of sorting array:
Selection Sort
Selection sort is a technique that sort an array. It selects an element in the array and moves it to its proper position. Selection sort works as follows:
Bubble Sort
Bubble Sort is also known as exchange sort. It repeatedly visits the array and compares two items at a time. It works as follows:
pairs to compare.
Two-D Arrays
Two-D array can be considered as table that consists of rows and columns. Each element in 2-D array is refered with the help of two indexes. One index indicates row and second indicates the column.
Declaring 2-D Array:
Data_type Identifier[row][column]; e.g: int arr[4][3];
Data type of values to be stored in the
Name of the array.
# of Rows in the table of array.
# of Columns in the table of arr
a0,r0ra | y.0,1 | 0,2 |
1,0 | 1,1 | 1,2 |
2,0 | 2,1 | 2,2 |
a3y,.0 | 3,1 | 3,2 |
Two-D array Intialization
The two-D array can also be initialized at the time of declaration. Initialization is performed by assigning the initial values in braces seperated by commas.
Some important points :
Two-D array Intialization
Syntax:
int arr[4][3]={ {12,5,22},
12 | 5 | 22 |
95 | 3 | 41 |
77 | 6 | 53 |
84 | 59 | 62 |
Column
indexe{s95,3,41},
{77,6,53},
Row indexes
0
{84,59,62} }1
2
3
1
0
2