You are on page 1of 26

Arrays

Topics
• Implementation of One-Dimensional Arrays
• Multidimensional Arrays
• Applications of Arrays
• Address Calculation
• Searching: Linear search, Binary Search
• Matrix Operations
• Dense and Sparse Data in Arrays
Data Types
• Simple data types are int, float and char.
• Simple variables can hold only one value at any
time during program execution, although that
value may change.
• A data structure is a data type that can hold
multiple values at the same time.
• Known as complex data type, composite
data type.
• The array is one kind of data structure.
Arrays
• An array is a group of related data items that all
have the same name and the same data type.
• Arrays can be of any data type we choose.
• Arrays are static, remain same size throughout
program execution.
• An array’s data items are stored contiguously in
physical memory.
• Each of the data items is known as an element
of the array. Each element can be accessed
individually (directly, randomly).
Array Declaration and Initialization
int data[ 5 ] ;
• The name of this array is “data”.
• This declaration sets aside a chunk of memory that is
big enough to hold 5 integers.
• It does not initialize those memory locations to 0 or
any other value. They contain garbage value.
• Initializing an array may be done with an array
initialization, as in :
 int data[ 5 ] = { 5, 2, 6, 9, 3 } ;

data 5 2 6 9 3
Accessing Array Elements
• Each element in an array has a subscript (index)
associated with it.
data 5 2 6 9 3
0 1 2 3 4
• Subscripts are integers and always begin at zero.
• Values of individual elements can be accessed by
indexing into the array. For example,
printf(“The third element = %d.\n”, data[ 2 ] ) ;
would give the output

The third element = 6.


Accessing Array Elements (con’t)
• A subscript can also be an expression that
evaluates to an integer.
data[ (a + b) * 2 ] ;
• It is a logical error when a subscript
evaluates to a value that is out of range
for the particular array.
• Some systems will handle an out-of-
range error gracefully and some will not
(in C).
Modifying Elements
• Individual elements of an array can also be modified
using subscripts.
data[ 4 ] = 20 ;
/*changes the value of the element found at subscript 4 to
20 */
• Initial values may be stored in an array using
indexing, rather than using an array initialization.
data[ 0 ] = 5 ;
data[ 1 ] = 2 ;
data[ 2 ] = 6 ;
data[ 3 ] = 9 ;
data[ 4 ] = 3 ;
Filling Large Arrays
• Since many arrays are quite large, using an
array initialization can be impractical.
• Large arrays are often filled using a for loop.
 for ( i = 0; i < 100; i++ )
 values [ i ] = 0 ;

 would set every element of the 100 element
array “values” to 0.
More Declarations
 int score [ 39 ] , gradeCount [ 5 ];

• Declares two arrays of type int.


• Neither array has been initialized.
• “score” contains 39 elements (one for each
student in a class).
• “gradeCount” contains 5 elements (one for
each possible grade, A - F).
Applications of One-Dimensional Arrays

• Polynomial Evaluation
• Polynomial Addition
• Polynomial Multiplication
• Linear Search
• Binary Search
Polynomial Evaluation
• P(x) = 3x5+2x4+7x3+8x2+2x+4
• t1 = (3*x*x*x*x*x)
• t2 = t1 + (2*x*x*x*x)
• t3 = t2 + (7*x*x*x)
• t4 = t3 + (8*x*x)
• P = t4 +(2*x) + 4
• 15 Multiplications, 5 Additions
Polynomial Evaluation
• P(x) = 3x5+2x4+7x3+8x2+2x+4
• t1 := 4; xp := x;
• t2 := (2*xp) + t1; xp := xp * x;
• t3 := (8*xp) + t2; xp := xp * x;
• t4 := (7*xp) + t3; xp := xp * x;
• t5 := (2*xp) + t4; xp := xp * x;
• P := (3*xp) + t5;
• 9 Multiplications, 5 Additions
Horner’s Rule

• P(x) = 3x5+2x4+7x3+8x2+2x+4
• t1 = (3*x) + 2
• t2 = (t1*x) + 7
• t3 = (t2*x) + 8
• t4 = (t3*x) + 2
• P = (t4*x) + 4
• 5 Multiplications, 5 Additions
Polynomial Addition
Polynomial Addition

• If m=n, for k=0:m ck=ak+bk


• If m<n, for k=0:m ck=ak+bk
and for k=m+1:n ck=bk
• If m>n, for k=0:n ck=ak+bk
and for k=n+1:m ck=ak
Polynomial Multiplication
Polynomial Multiplication

• for k=m+1:m+n ak=0


• for k=n+1:m+n bk = 0

The value of k is from 0 to m+n


Linear Search: A Simple
• A search traverses Search
the collection until
o The desired element is found
o Or the collection is exhausted

• If the collection is ordered, we might not have to


look at all elements
o We can stop looking when we know the
element cannot be in the collection.
Un-Ordered Iterative Array Search
void main()
{
int LSearch(int a[], int, int);
int i, arr[10], n, x;
printf(“\nEnter the number of Elements “);
scanf(“%d”, &n);
printf(“\nEnter the Elements “);
for(i=0,i<n;i++) scanf(“%d”, &arr[i]);
printf(“\nEnter the element to be searched”);
Scanf(“%d”, &x);
if(LSearch(arr,n,x)<n)
printf(“\nElement Found at Location %d” LSearch(arr,n,x)+1);
else
pirintf(“\nElement not Found”);
int LSearch(int Array[], int n,int Num){
int i;
for (i=0;i<n, i++)
if(Array[i]==Num) return i;
return n;}
Complexity – Linear Search

• Best Case – Only one comparison – О (1)


• Worst Case – n comparisons – О (n)
• Average Case
 To locate 1st element – 1 comparison
 To locate 2nd element – 2 comparisons
 ......
 To locate k-th element – k comparisons
 ....
 To locate n-th element – n comparisons
• Total comparisons = 1+2+3+ ...+ n= n(n+1)/2
• Average number of comparisons =n(n+1)/2/n =
(n+1)/2
• Complexity = О (n)
A Better Search Algorithm
• Of course we could use our simpler search and
traverse the array

• But we can use the fact that the array is sorted to


our advantage

• This will allow us to reduce the number of


comparisons
Binary Search
• Requires a sorted array or a binary search
tree.

• Cuts the “search space” in half each time.

• Keeps cutting the search space in half until


the target is found or has exhausted the all
possible locations.
Binary Search Algorithm

look at “middle” element


if no match then
look left (if need smaller)
or
right (if need larger)

1 7 9 12 33 42 59 76 81 84 91 92 93 99

Look for 42
Binary Search – Recursive Version
int BSearch(int Array[], int low, int high, int x)
{
int mid;
if(low<=high)
{
mid= (low+high)/2;
if (Array[mid]== x) return mid;
else
if(Array[mid]>x) BSearch (Array, low, mid-1,x);
else BSearch(Array, mid+1, high,x);
}
return -999;
}
Binary Search – Iterative Version
int BSearch(int Array[], int low, int high, int x)
{
int mid;
while(low<=high)
{
mid= (low+high)/2;
if (Array[mid]== x) return mid;
else
if(Array[mid]>x) high = mid-1;
else low = mid+1;
}
return -999;
}
Complexity – Binary Search
• Best Case – Searching of Middle element – О(1)
• Average Case

• Worst Case – One more than Average Case

You might also like