Professional Documents
Culture Documents
ARRAY
• Array is a fixed size sequenced collection of (subscripted) data storage locations (variables), each
having the same data type and the same name.
• An array is an indexed data structure to represent several variables having the same data type:
int y[100];
• An array is a group of consecutive memory locations with same name and data type.
• While the complete set of values is referred to as an array, an individual value is called element.
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.
1 | Programming
OUTPUT:
Syntax:
data_type array_name[array_size];
where: Examples:
int numcollect[5]; Declares an array
• data_type –declares the base type of the
named numcollect with
array, which is the type of each element in char word[10];
the array 5 elements with no
• array_name- the name of the array long telnos[15]; values.
• array_size- a positive integer or expression int studentAge[4];
which defines the array size or the number studentAge[0] = 14;
of elements the array will hold. studentAge[1] = 13;
studentAge[2] = 15;
studentAge[3] = 16;
Syntax:
where: Examples:
int numcollect[5] = {12,15,36,18,9};
• List of values - Values to initialize the array
Elements of an array
2 | Programming
Array elements may be used in programs just like any other C simple variables.
• Examples of executable statements that makes use of array elements:
a = number[0] + 10;
number[4] =number[0] + number[2];
number[2] = x[5] +y[10];
value[6] = number[i]*3;
Types of arrays
};
3 | Programming
Initialization of arrays can either be at:
• Compile time
• Run time
Compile time initialization
• Array elements are initialized in the source code before compilation.
Run time initialization
• Array elements are initialized after the source code is compiled and the program is ran.
Run time
initialization
OUTPUT:
Compile time
initialization
OUTPUT:
4 | Programming
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 and 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 a 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.
1. 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:
2. 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:
o Compare adjacent element. If the first is greater than the second, swap them.
o Repeat this for each pair of adjacent element, starting with the first two and ending with the last
two. (at this point last element should be greatest).
o Repeat the step for all elements except the last one.
o Keep repeating for one fewer element each time until there are no pairs to compare.
5 | Programming
Run time
initialization
OUTPUT:
Sequential
Search
OUTPUT:
6 | Programming