4 Arrayppt
4 Arrayppt
What is Arrays ?
• An array is a group of consective memory locations with same name and data
type.
• Simple variable is a single memory location with unique name and a type. But
an Array is collection of different adjacent memory locations. All these
memory locations have one collective name and type.
• The memory locations in the array are known as elements of array. The total
number of elements in the array is called length.
• The elements of array is accessed with reference to its position in array, that is
call index or subscript.
Advantages / Uses of Arrays
Arrays can store a large number of value with single name.
Arrays are used to process many value easily and quickly.
The values stored in an array can be sorted easily.
The search process can be applied on arrays easily.
Types of Arrays:
One-Dimensional Array
Two-Dimensional Array
Multi-Dimensional Array
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.
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 }; 70 marks[5]={70,54,82,96,49};
e.g: int 54 82 96 49
o Data _type: Data type of values to be stored in the array.
o Identifier: Name of the array.
o Length: Number of elements
o List of values: Values to initialize the array. Initializing values must be constant
Accessing element of array
Individual Element:
Array_name[index];
Using Loop:
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.
Visit the first element of array and compare its value with required value.
If the value of array matches with the desired value, the search is complete.
If the value of array does not match, move to next element an repeat same
process.
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.
o It locates the middle element of array and compare with desired number.
o If they are equal, search is successful and the index of middle element is
returned.
o If they are not equal, it reduces the search to half of the array.
o If the search number is less than the middle element, it searches the first half of
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.
o Ascending Order
o Descending Order 12 25 33 37 48
48 37 33 25 12
Techniques Of Sorting Array
There are two techniques of sorting array:
o Selection Sort
o Bubble Sort
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:
3 84 59 62
* Prepared by .. .
1. Kaushal Mehta … .. twitter@kaushalmehta96
2. Jigar Patel … .. twitter@jigarpatel_jp