Arrays
Arrays
An array is a special and very powerful data structure in C language. An array is a collection of similar data
items. Array is used to store, process and print large amount of data using a single variable.
Ex 1: Set of integers, set of characters, set of students, set of pens etc. are examples of various arrays.
Ex 2: Marks of all students in a class is an array of marks The pictorial representation of an array of 5 integers, an array
of 5 floating point numbers and an array of 5 characters is shown below:
A single-dimensional array (also called one-dimensional array) is a linear list consisting of related data items of same
type. In memory, all the data items are stored in contiguous memory locations one after the other.
For example, a single dimensional array consisting of 5 integer elements is shown below:
The compiler uses this size to reserve the appropriate number of memory locations so that data can be stored,
accessed and manipulated when the program is executed.
Traversing
Visiting or accessing each item in the array is called traversing the array. Here, each element is accessed in linear
order either from left to right or from right to left.
“How to read the data from the keyboard and how to display data items stored in the array?”
We can easily read, write or process the array items using appropriate programming constructs such as for-loop,
while-loop, do-while, if-statement, switch-statement etc. Consider the declaration shown below:
int a[5];
Here, memory for 5 integers is reserved and each item in the array can be accessed by specifying the index as shown
below:
Note: In general, Using a[0] through a[n-1] we can access n data items.
So, in C language, if we want to read n data items from the keyboard, the following statement can be used:
Inserting an item into an array (based on the position)
Now, let us see “How to insert an item into an unsorted array based on the position?” Problem statement: Given an
array a consisting of n elements, it is required to insert an item at the specified position say pos. Design: An item can
be inserted into the array by considering various situations as shown below:
Step 1: Elements are present (Invalid position): This case can be pictorially represented as shown below:
Can you insert an item at 8th position onwards in the above array? No, we cannot insert since, it is invalid position.
That is, if pos is greater than 7 or if pos is less than 0, the position is invalid. The code for this case can be written as
shown below:
Step 2: Make room for the item to be inserted at the specified position: Consider the following list with 7 elements
and item 60 to be inserted at position 3.
We have to make room for the item to be inserted at position 3. This can be done by moving all the elements 30, 80,
70 and 90 from positions 6, 5, 4, 3 into new positons 7, 6, 5, 4 respectively towards right by one position as shown
below:
This is possible using the following assignment statements in the order specified:
a[7] = a[6];
a[6] = a[5];
a[5] = a[4];
a[4] = a[3]
In general, a[i+1] = a[i] for i = 6 down to 3 for i = n-1 down to pos Now, the code for above activity can be written as
shown below:
After executing, the above statement, the array contents can be pictorially represented as shown below:
Step 3: Insert item at the specified position: The code for this case can be written as shown below:
a[pos] = item;
Can you delete an item from 7th position onwards in the above array? No, we cannot delete since, it is invalid
position. That is, if pos is greater than or equal to 7 or if pos is less than 0, the position is invalid. The code for this
case can be written as shown below:
Step 2: Display the item to be deleted: Consider the following list with 7 elements and let the position pos is 3.
The item at position pos can be accessed by writing a[pos] and it can be displayed using the printf() function as as
shown below:
Step 3: Remove the item from the array: Removing an element at the given position can be illustrated using the
following figure:
As shown in above figure, move all the elements from 4th position onwards towards
left by one position using the following statements:
a[3] = a[4];
a[4] = a[5];
a[5] = a[6];
In general, a[i - 1] = a[i] for i = 4 to 6
for i = pos+1 to n-1
Now, the code for above activity can be written as shown below:
After executing the above statement, the array contents can be pictorially represented
as shown below:
Step 4: Update number of elements in above array: After deleting an item, the number of items in the array should
be decremented by 1. It can be done using the following statement: return n - 1;
Step 2: Return type: Our intention is only to sort the numbers. Hence, we are not returning any value. So,
Step 3: Designing body of the function: This is the simplest and easiest sorting technique. In this technique, the two
successive items A[i] and A[i+1] are exchanged whenever A[i] = A[i+1]. For example, consider the elements shown
below:
50, 40, 30, 20, 10
The elements can be sorted as shown below:
In the first pass 50 is compared with 40 and they are exchanged since 50 is greater than 40.
Next 50 is compared with 30 and they are exchanged since 50 is greater than 30.
If we proceed in the same manner, at the end of the first pass the largest item occupies the last position.
On each successive pass, the items with the next largest value will be moved to the bottom and thus elements
are arranged in ascending order.
Note: Observe that after each pass, the larger values sinks to the bottom of the array and hence it is called sinking
sort. The following figure below shows the output of each pass.
Now we concentrate on the designing aspects of this sorting technique. The comparisons that are performed in each
pass are shown below:
In general we can say i = 0 to n-(j+1) or i = 0 to n – j – 1.
Here, j = 1 to 4 represent pass number. In general j = 1 to n-1.
So, the partial code can be written as:
for j = 1 to n-1
for i = 0 to n-j-1
if ( A[i] > A[i +1] )
exchange ( A[i] , A[i+1] )
end if
end if
end if
Successful search: If search key is 25, it is present in the above list and we return its position 3 indicating “Successful
search”.
Unsuccessful search: If search key is 50, it is not present in the above list and we return -1 indicating “Unsuccessful
search”
2 Binary search
The general idea used in binary search is similar to the way we search for the telephone number of a person in the
telephone directory. Obviously, we do not use linear search. Instead, we open the book from the middle and the name
is compared with the element at the middle of the book. If the name is found, the corresponding telephone number
is retrieved and the searching has to be stopped. If the name to be searched is less than the middle element, search
towards left otherwise, search towards right. The procedure is repeated till key item is found or the key item is not
found.
Step 1: Identify parameters to function: We have to search for key item in an array a consisting of n elements. So,
input must be key, array a and n. So,
Step 2: Return type: We are returning position of key item if found, otherwise, we return -1. Note that the position is
integer and -1 is also integer. So,
Step 3: Designing function body: Let key is the element to be searched in the array a consisting of n elements. In the
array a, element 10 in position 0 is the first element and element 90 in position 8 is the last element as shown below:
So, initial values are: low = 0 and high = 8
=9–1
= n – 1 (general)
So, in general, initial values are:
Step 4: If low is the position of the first element and high is the position of the last element, the position of the
middle element can be obtained using the statement:
The key to be searched is compared with middle element. The pictorial representation and equivalent code can be
written as shown below:
After executing the statement, if key is same as a[mid], we return the position of key. If key is not same as a[mid], it
may be present either in the left part of the array or right part of the array and leads to following 2 cases:
Case 1: key towards left of mid: If key is less than the middle element, the left part of array has to be compared from
low to mid-1 as shown in figure below:
Note that position of low has not been changed. But, position of high is changed to mid–1. The equivalent code can
be written as:
Case 2: key towards right of mid: If key is greater than the middle element, the right part of array has to be
compared from mid + 1 to high as shown in figure below:
Note that position of high has not been changed. But, position of low is changed to mid+1. The equivalent code can
be written as:
Note that entire step 4 has to be repeatedly executed as long as long as low is less than or equal to high. The code
for this can be written as shown below:
while ( low <= high )
{
mid = ( low + high ) / 2; /* Find the mid point */
if ( key == a[mid] ) return mid; /* Item found */
if ( key < a[mid] ) high = mid – 1; /* To search left part */
if ( key > a[mid] ) low = mid + 1; /* To search right part */
}
Finally, when the value of low exceeds the value of high indicates that key is not present in the array and return -1
using the following statement:
return -1; /* Search unsuccessful */