Delhi Public School, R.K.
Puram Computer Science
Sorting - Arranging content in Ascending/Descending order
Bubble Sort
I A[0] A[1] A[2] A[3] A[4]
0 45 90 23 23 90 34 34 90 56 56 90
1 45 23 23 45 34 34 45 56
2 23 34 45
3 23 34
C++ Function to arrange elements of an array in C++ Function to arrange elements of an array in
Ascending order using Bubble Sort Descending order using Bubble Sort
void BubbleA(int A[],int n) void BubbleD(int A[],int n)
{ {
for (int I=0;I<N-1;I++) for (int I=0;I<N-1;I++)
{ {
for (int J=0;J<N-I-1;J++) for (int J=0;J<N-I-1;J++)
if (A[J]>A[J+1]) if (A[J]<A[J+1])
{ {
int T=A[J]; int T=A[J];
A[J]=A[J+1]; A[J]=A[J+1];
A[J+1]=T; A[J+1]=T;
} }
} }
Selection Sort
I A[0] A[1] A[2] A[3] A[4]
0 45 90 55 63 28
Small=0 4
So,we will swap
Positions 0 and 4
1 28 90 55 63 45
Small=1 2 4
So,we will swap
Positions 1 and 4
2 45 55 63 90
Small=2
No change in Small
So,no swapping of
positions needed
3 55 63 90
Small=3
No change in Small
So,no swapping of
positions needed
C++ Function to arrange elements of an array in C++ Function to arrange elements of an array in
Ascending order using Selection Sort Descending order using Selection Sort
void SelectionA(int A[],int n) void SelectionD(int A[],int n)
C++ Arrays Part 2 CScXI/2016_2017/MK/12 #1
Delhi Public School, R.K.Puram Computer Science
{ {
for (int I=0;I<N-1;I++) for (int I=0;I<N-1;I++)
{ {
int Small=I; int Big=I;
for (int J=I+1;J<N;J++) for (int J=I+1;J<N;J++)
if (A[Small]>A[J]) if (A[Big]<A[J])
Small=J; Big=J;
if (Small!=I) if (Big!=I)
{ {
int T=A[Small]; int T=A[Big];
A[Small]=A[I]; A[Big]=A[I];
A[I]=T; A[I]=T;
} }
} }
} }
Insertion Sort
I A[0] A[1] A[2] A[3] A[4]
95 90 55 63 28
1 95 90 90 95
Temp=A[1]=90
2 90 55 95 90 55 95
Temp=A[2]=55
3 55 90 63 95 90 63 95
Temp=A[3]=63
4 55 28 63 55 90 63 95 90 28 95
Temp=A[4]=28
C++ Function to arrange elements of an array in C++ Function to arrange elements of an array in
Ascending order using Insertion Sort Descending order using Insertion Sort
void InsertionA(int A[],int n) void InsertionD(int A[],int n)
{ {
for (int I=1;I<N;I++) for (int I=1;I<N;I++)
{ {
int Temp=A[I],J=I-1; int Temp=A[I],J=I-1;
while(Temp<A[J] && J>=0) while(Temp>A[J] && J>=0)
{ {
A[J+1]=A[J]; A[J+1]=A[J];
J--; J--;
} }
A[J+1]=Temp; A[J+1]=Temp;
} }
} }
Binary Search
Prerequisite - The array content should be sorted (Ascending/Descending)
Let us assume, an array A[10] is arranged in ascending order. The following are the steps to search for a
C++ Arrays Part 2 CScXI/2016_2017/MK/12 #2
Delhi Public School, R.K.Puram Computer Science
value 85 from the array using Binary Search.
A[10] Step Data to searched=85
0 23 1 LB=0;UB=9;MID=(0+9)/2=4;As (A[4]<85) LB=MID+1=5
1 45 2 LB=5;UB=9;MID=(5+9)/2=7;As (A[7]>85) UB=MID-1=6
2 67 3 LB=5;UB=6;MID=(5+6)/2=5;As (A[5]<85) LB=MID+1=6
3 69 4 LB=6;UB=6;MID=(6+6)/2=6;As (A[6]==85) Data Found
4 73
5 81
6 85
7 91
8 95
9 99
Let us assume, an array A[10] is arranged in ascending order. The following are the steps to search for a
value 79 from the array using Binary Search.
A[10] Step Data to searched=79
0 23 1 LB=0;UB=9;MID=(0+9)/2=4;As (A[4]<79) LB=MID+1=5
1 45 2 LB=5;UB=9;MID=(5+9)/2=7;As (A[7]>79) UB=MID-1=6
2 67 3 LB=5;UB=6;MID=(5+6)/2=5;As (A[5]>79) UB=MID-1=4
3 69 4 LB=5;UB=4; As LB>UB, Data Not Found
4 73
5 81
6 85
7 91
8 95
9 99
C++ Function to SEARCH for a value in an array arranged in C++ Function to SEARCH for a value in an array arranged in
Ascending order using Binary Search Descending order using Binary Search
int BinSearch(int A[],int N,int Data) int BinSearch(int A[],int N,int Data)
{ {
C++ Arrays Part 2 CScXI/2016_2017/MK/12 #3
Delhi Public School, R.K.Puram Computer Science
int LB=0,UB=N-1,MID,Found=0; int LB=0,UB=N-1,MID,Found=0;
while (LB<=UB && !Found) while (LB<=UB && !Found)
{ {
MID=(LB+UB)/2; MID=(LB+UB)/2;
if (A[MID]>Data) if (A[MID]>Data)
UB=MID-1; LB=MID+1;
else if (A[MID]<Data) else if (A[MID]<Data)
LB=MID+1; UB=MID-1;
else else
Found++; Found++;
} }
return Found; return Found;
} }
Merging
C++ Function to Input Array Input Array Output Array
MERGE two ascending order array A B C
void Merge(int A[],int B[],int C[], A[0]=14 B[0]=13 C[0]=13
int N,int M,int &L) A[1]=23 B[1]=23 C[1]=14
{ A[2]=56 B[2]=59 C[2]=23
int I=0,J=0; B[3]=65 C[3]=56
L=0; B[4]=78 C[4]=59
while (I<N && J<M) C[5]=65
if (A[I]<B[J]) C[6]=78
C[L++]=A[I++];
else if (A[I]>B[J])
C[L++]=B[J++];
else
{
C[L++]=A[I++];
J++;
}
while(I<N)
C[L++]=A[I++];
while(J<M)
C[L++]=B[J++];
}
Note: If we require to merge an Ascending order Array A and a Descending Order Array B to produce an
Ascending order Array C.
1. Initialization of Merge function will be changed to [int I=0,J=M-1;]
2. Conditions will be modified to [I<N && J>=0]
3. Step to Change in subscript of A will be same as I++ but B will become J--
Two-Dimensional Array
Declaration
int A[20][30];//Declaration of 2 D Integer Array with 20 Rows 30 Columns
C++ Arrays Part 2 CScXI/2016_2017/MK/12 #4
Delhi Public School, R.K.Puram Computer Science
float B[5][6];//Declaration of 2 D Float Array with 5 Rows 6 Columns
Initialization
int C[2][3]={{2,3,4},{1,5,7}};
//Initializes
//C[0][0] as 2,C[0][1] as 3,C[0][2] as 4
//C[1][0] as 1,C[1][1] as 5,C[1][2] as 7
float D[][2]={{2,3},{1,5},{4,7}};
//Initializes
//D[0][0] as 2,D[0][1] as 3
//D[1][0] as 1,D[1][1] as 5
//D[2][0] as 4,D[2][1] as 7
Invalid Declarations
int E[][2]; //Number of rows missing
int F[][]; //Number of rows and columns missing
int G[4][]; //Number of columns missing
int N;
cin>>N;
int H[N][N]; //Number of rows/column can’t be a variable
Invalid Initializations
int S[][]={{1,2},{6,4}}; //Number of rows/columns missing
int T[2][]={{1,2},{6,4}};//Number of columns missing
A function to allow user to A function to
Enter content in a 2D Array Display content of a 2D Array
void Enter(int A[][4],int N,int M) void Display(int A[][4],int N,int M)
{ {
for (int I=0;I<N;I++) for (int I=0;I<N;I++)
for (int J=0;J<M;J++) {
{ for (int J=0;J<M;J++)
cout<<I<<”,”<<J<<”?”; cout<<setw(5)<<A[I][J];
cin>>A[I][J]; cout<<endl;
} }
} }
A function to A function to
Add two 2D Arrays A and B, store it in C Subtract a 2D Array A from B, store it in C
void Enter(int A[][4],int B[][4], void Enter(int A[][4],int B[][4],
int C[][4],int N,int M) int C[][4],int N,int M)
{ {
for (int I=0;I<N;I++) for (int I=0;I<N;I++)
for (int J=0;J<M;J++) for (int J=0;J<M;J++)
C[I][J]=A[I][J]+B[I][J]; C[I][J]=A[I][J]-B[I][J];
} }
A function to A function to
Display sum of each row of a 2D Array Display sum of each column of a 2D Array
void SumRows(int A[][4],int N,int M) void SumCols(int A[][4],int N,int M)
{ {
C++ Arrays Part 2 CScXI/2016_2017/MK/12 #5
Delhi Public School, R.K.Puram Computer Science
for (int I=0;I<N;I++) for (int J=0;J<M;J++)
{ {
int Sr=0; int Sc=0;
for (int J=0;J<M;J++) for (int I=0;I<N;I++)
Sr+=A[I][J]; Sc+=A[I][J];
cout<<”Row[”<<I<<”]=”<<Sr<<endl; cout<<”Col[”<<J<<”]=”<<Sc<<endl;
} }
} }
A function to add Diagonal elements A function to add even values in 2D Array
A[0][0] A[0][1] A[0][2] A[0][0] A[0][1] A[0][2]
10 15 5 10 15 5
A[1][0] A[1][1] A[1][2] A[1][0] A[1][1] A[1][2]
25 20 30 25 20 30
A[2][0] A[2][1] A[2][2] A[2][0] A[2][1] A[2][2]
2 40 1 2 40 1
D1=31 D2=27 Sum=10+20+30+2+40=102
void SumDiagonal(int A[][3],int N) void SumEvens(int A[][3],int N,int M)
{ {
int D1=0,D2=0; int Sum=0;
for (int I=0;I<N;I++) for (int I=0;I<N;I++)
{ for (int J=0;J<M;J++)
D1+=A[I][I]; if (A[I][J]%2==0)
D2+=A[N-I-1][I];//OR A[I][N-I-1] Sum+=A[I][J];
} cout<<Sum<<endl;
cout<<”D1=”<<D1<<” D2=”<<D2<<endl; }
}
A function to add values above D1 A function to add values below D1
A[0][0] A[0][1] A[0][2] A[0][0] A[0][1] A[0][2]
10 15 5 10 15 5
A[1][0] A[1][1] A[1][2] A[1][0] A[1][1] A[1][2]
25 20 30 25 20 30
A[2][0] A[2][1] A[2][2] A[2][0] A[2][1] A[2][2]
2 40 1 2 40 1
AbvD1=10+15+5+20+30+1=81 BlwD1=10+25+20+2+40+1=98
void SumAbvD1(int A[][3],int N) void SumBlwD1(int A[][3],int N)
{ {
int Abv=0; int Blw=0;
for (int I=0;I<N;I++) for (int I=0;I<N;I++)
for (int J=I;J<N;J++) for (int J=0;J<=I;J++)
Abv+=A[I][J]; Blw+=A[I][J];
cout<<Abv<<endl; cout<<Blw<<endl;
} }
C++ Arrays Part 2 CScXI/2016_2017/MK/12 #6