9 Arrays
9 Arrays
PRAVEEN M JIGAJINNI
PGT (Computer Science)
MTech[IT],MPhil (Comp.Sci), MCA, MSc[IT], PGDCA, ADCA,
Dc. Sc. & Engg.
email: [email protected]
Introduction to Arrays
One Dimensional Array.
Two Dimensional Array.
Inserting Elements in Array.
Reading Elements from an Array.
Searching in Array.
Sorting of an Array.
Merging of 2 Arrays.
What is an Array?
An array is a sequenced collection of
elements that share the same data type.
a[12];
Arrays and Loops
Since we can refer to individual array elements
using numbered indexes, it is very common for
programmers to use for loops when processing
arrays.
Example : scores Array
Array Types in C++
C++ supports two types of arrays:
arrayLength = 9;
scores[5] = 42;
scores[3] = 5 + 13;
scores[8] = x + y;
scores[0] = pow(7, 2);
Copying Entire Arrays
AnyFunction(myArray[4]);
…
void AnyFunction(int anyValue)
{
…
}
Passing Elements to Functions
We may also pass the address of an element:
anyFunction(&myArray[4]);
…
void anyFunction(int* anyValue)
{
*anyValue = 15;
}
Arrays are Passed by Reference
Unlike individual variables or elements, which
we pass by value to functions, C++ passes
arrays to functions by reference.
77 42 35 12 101 5
(UNSORTED ARRAY)
1 2 3 4 5 6
5 12 35 42 77 101
(SORTED ARRAY)
Sorting
Sorting is the “process through which data are
arranged according to their values.”
2 2 2 2 2 2 2
6 3 3 3 3 3 3
13 13 6 6 6 6 6
22 22 22 13 13 13 13
3 6 13 22 15 15 15
52 52 52 52 52 22 22
15 15 15 15 22 52 52
Observations about
Advantages:
Selection sort
1. Easy to use.
2. The efficiency DOES NOT depend on initial arrangement
of data
3. Could be a good choice over other methods when data
moves are costly but comparisons are not.
Disadvantages:
1. Performs more transfers and comparison compared to
bubble sort.
2. The memory requirement is more compared to insertion
& bubble sort.
Bubble Sort
Basic Idea:-
Divide list into sorted the unsorted sublist is
“bubbled” up into the sorted sublist.
Repeat until done:
1.Compare adjacent pairs of records/nodes.
2. If the pair of nodes are out of order,
exchange them, and continue with the next pair
of records/nodes.
3. If the pair is in order, ignore and unsorted
sublists.
Smallest element in them and continue with the
next pair of nodes.
Bubble Sort Program
void BubbleSort(int a[],int N)
{
int i , j , tmp;
for (i=0;i<N; i++)
{
for(j=0 ; j < (N-1)-i ; j++)
{
if (a[j]>a[j+1]) //swap the values if previous is greater
{ // than ext one
tmp=a[j];
a[j]=a[j+1];
a[j+1]=tmp;
}
} }
Bubble sort example
Initial array elements
9 7 4 6 1
Array after pass-1
7 9 4 6 1 Array after pass-3
(No swap)
7 4 9 6 1 4 6 1 7 9
7 4 6 9 1
4 1 6 7 9
7 4 6 1 9
Array after pass-4
Array after pass-2 1 4 6 7 9
4 7 6 1 9
Final sorted array
4 6 7 1 9
1 4 6 7 9
4 6 1 7 9
Observations about Bubble Sort
Advantages:
1. Easy to understand & implement.
2. Requires both comparison and swapping.
3. Lesser memory is required compared to
other sorts.
Disadvantages:
1. As the list grows larger the performance of
bubble sort get reduced dramatically.
2. The bubble sort has a higher probability of
high movement of data.
Insertion sort
9 4 3 2 34 1
Pass1
Pass 2 4 9 3 2 34 1
Pass 3 3 4 9 2 34 1
Pass 4 2 3 4 9 34 1
Pass 5 2 3 4 9 34 1
1 2 3 4 9 34
Pass 6
1 2 3 4 9 34
Final Sorted array
Insertion sort program
void insertionSort(int a[ ], int N)
{
int temp,i, j;
a[0]=INT_MIN;
for(i=1;i<N ; i++)
{
temp=a[i]; j=i-1;
while(temp < AR[j])
{ a[j+1]=a[j];
j--;
}
a[j+1]=temp;
}
}
Observations about insertion sort
Advantages:
1.Simplicity of the insertion sort makes it a
reasonable approach.
2.The programming effort in this techniques is trivial.
3.With lesser memory available insertion sort proves
useful.
Disadvantage:
1. The sorting does depend upon the distribution of
element values.
2. For large arrays -- it can be extremely inefficient!
Merging of two sorted arrays into third
array in sorted order
Algorithm to merge arrays a[m](sorted in ascending
order) and b[n](sorted in descending order) into third
array C [ n + m ] in ascending order.
c[k++]=a[i++];
c[k++]=b[j--];
} // end of Function.
Merging of two sorted arrays into third
array in sorted order
void main()
{
int a[5]={2,4,5,6,7},m=5; //a is in ascending order
int b[6]={15,12,4,3,2,1},n=6; //b is in descending order
int c[11];
merge(a, m, b, n, c);
cout<<”The merged array is :\n”;
for(int i=0; i<m+n; i++)
cout<<c[i]<”, “;
}
Output is
The merged array is:
1, 2, 2, 3, 4, 4, 5, 6, 7, 12, 15
CBSE Questions on Sorting
1.Consider the following array of integers
42,29,74,11,65,58, use insertion sort to sort them in
ascending order and indicate the sequence of steps
required.
2. The following array of integers is to arranged in ascending
order using the bubble sort technique : 26,21,20,23,29,17.
Give the contents of array after each iteration.
3. Write a C++ function to arrange the following Employee
structure in descending order of their salary using bubble
sort. The array and its size is required to be passed as
parameter to the function. Definition of Employee structure
struct Employee { int Eno;
char Name[20];
float salary; };
Revision
1.In selection sort, algorithm continuous goes
on finding the smallest element and swap
that element with appropriate position
element.
2.In bubble sort, adjacent elements checked
and put in correct order if unsorted.
3. In insertion sort, the exact position of the
element is searched and put that element
at its particular position.
2D Array Definition
To define a fixed-length two-dimensional array:
int table[5][4];
2-D Array a[m][n];m Rows and n columns - int a[3][4];
Implementation of 2-D Array in memory
The elements of 2-D array can be stored in memory by two-
Linearization method :
1. Row Major
2. Column Major
‘a’ ‘b’ ‘c’
‘a’
‘a’ ‘b’ ‘c’ ‘d’
100
100
101
101
102
102
103
103
Therefore,the memory address
104 What is the address of ‘c’
Ans - 104
calculation
102 will be different in 104
in column
row major
major
? ?
105 both the methods.
105 55
Implementation of 2-D Array in memory
1. Row Major Order for a[m][n] or a[0…m-1][0…n-1]
X = B+W*[ I*C+J]
2.Column Major Order formula
X = B + W * [I + J * R]
Memory Address Calculation
An array S[10][15] is stored in the memory with each
element requiring 4 bytes of storage. If the base address of
S is 1000, determine the location of S[8][9] when the array
is S stored by CBSE 1998 OUTISDE DELHI 3
(i) Row major (ii) Column major.
ANSWER
Let us assume that the Base index number is
[0][0].
Number of Rows = R = 10
Number of Columns = C = 15 Size of data = W = 4
• Base address = B = S[0][0] = 1000 Location of
S[8][9] = X
Memory Address Calculation
(i) When S is stored by Row Major
X = B + W * [ 8 * C + 9]
= 1000 + 4 * [8 * 15 + 9]
= 1000 + 4 * [120 + 9]
= 1000 + 4 * 129
= 1516
(ii) When S is stored by Column Major
X = B + W * [8 + 9 * R]
= 1000 + 4 * [8 + 9 * 10]
= 1000 + 4 * [8 + 90]
= 1000 + 4 * 98
= 1000 + 392
= 1392
Programs on 2D Arrays
1. sum of rows a[3][2]
for(row=0;row<3;row++)
{ sum=0;
for(col=0;col<2;col++)
sum+=a[row][col];
cout<<”sum is “<<sum; }
2. sum of columns a[2][3]
for(col=0;col<3;col++)
{sum=0;
for(row=0;row<2;row++)
sum+=a[row][col];
cout<<”column sum is”<<sum;}
Programs on 2D Arrays
3. sum of left diagonals a[3][3]
sum=0;
for(row=0;row<3;row++)
sum+=a[row][row]
4. sum of right diagonals a[3][3]
sum=0;
for(row=0,col=2;row<3;row++,col--)
sum+=a[row][col]
5. Transpose of a matrix a[3][2] stored in b[2][3]
for(row=0;row<3;row++)
for(col=0;col<2;col++)
b[col][row]=a[row][col];
Programs on 2D Arrays
6. Display the lower half a[3][3]
for(row=0;row<3;row++)
for(col=0;col<3;col++)
if(row<col)
cout<<a[row][col];
7. Display the Upper Half a[3][3]
for(row=0;row<3;row++)
for(col=0;col<3;col++)
if(row>col)
cout<<a[row][col];
Programs on 2D Arrays
8. ADD/Subtract matrix
a[2][3]+/-b[2][3]=c[2][3]
for(row=0;row<2;row++)
for(col=0;col<3;col++)
c[row][col]=a[row][col]+/- b[row][col]
9. Multiplication a[2][3]*b[3][4]=c[2][4]
for(row=0;row<2;row++)
for(col=0;col<4;col++)
{c[row][col]=0;
for(k=0;k<3;k++)
c[row][col]+=a[row][k]*b[k][col] }
Programs on 1D Arrays
1. Reversing an array
j=N-1;
for(i=0;i<N/2;i++)
{ temp=a[i];
a[i]=a[j];
a[j]=temp; j--; }
2.Exchanging first half with second half (array size is even)
for(i=0,j=N/2;i<N/2;i++,j++)
{ temp=a[i];
a[i]=a[j];
a[j]=temp; }
Programs on 1D Arrays
3. Interchanging alternate locations
(size of the array will be even)
for(i=0;i<N;i=i+2)
{ temp= a[i]; //swapping the alternate
a[i]=a[i+1]; //locations using third
a[i+1]=temp;} //variable
?
Any Questions Please
Thank You
Very Much