Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
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
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
{ {
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
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
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
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)
{ {
Merging
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
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 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)
{ {