Array
Array
Application
Programming 2
Erwin D. Marcelo
November 2023
Sorting
01. Bubble Sort 02Balloon Sort
This sorting algorithm The balloon sort compares the first element
is comparison-based with each following element of the array,
algorithm in which making any necessary swaps. When the first
each pair of adjacent pass through the array is complete, the
elements is compared balloon sort then takes the second element
and the elements are and compares it with each following element
swapped if they are of the array swapping elements that are out
not in order. of order.
51 18 10 16 9
0K 1K+1 2 3 4
18 51 10 16 9
0 1K 2K+1 3 4
18 10 51 16 9
0 1 2K 3K+1 4
18 10 16 51 9
0K 1 2 3K 4K+1
BUBBLE SORT
First Pass
18 10 16 9 51
0 1 2 3 4
18 10 16 9 51
0K 1K+1 2 3 4
10 18 16 9 51
0 1K 2K+1 3 4
10 16 18 9 51
0 1 2K 3K+1 4
BUBBLE SORT
Second Pass
10 16 9 18 51
0 1 2 3 4
10 16 9 18 51
0K 1K+1 2 3 4
10 16 9 18 51
0 1K 2K+1 3 4
BUBBLE SORT
Third Pass
10 9 16 18 51
0 1 2 3 4
10 16 9 18 51
0K 1K+1 2 3 4
BUBBLE SORT
Fourth Pass
9 10 16 18 51
0 1 2 3 4
4 9 7 6 2
0-K 1 2 3 4K+4
March 2, 2024
BALLOON SORT
First Pass
2 9 7 6 4
0 1 2 3 4
March 2, 2024
BALLOON SORT
2 9 7 6 4
0 1 Key 2 3 4
• COMPARE
• IF Greater Than, then Swap
2 9 7 6 4
0 1K 2 K+1 3 4
2 7 9 6 4
0 1-K 2 3K+2 4
2 6 9 7 4
0 1K 2 3 4K+3
March 2, 2024
BALLOON SORT
Second Pass
2 4 9 7 6
0 1 2 3 4
March 2, 2024
BALLOON SORT
2 4 9 7 6
0 1 2 Key 3 4
• COMPARE
• IF Greater Than, then Swap
2 4 9 7 6
0 1 2 K 3 K+1 4
2 4 7 9 6
0 1 2K 3 4 K+2
March 2, 2024
BALLOON SORT
Third Pass
2 4 6 9 7
0 1 2 3 4
March 2, 2024
BALLOON SORT
2 4 6 9 7
0 1 2 3 Key 4
• COMPARE
• IF Greater Than, then Swap
2 4 6 9 7
0 1 2 3 K 4 K+1
March 2, 2024
BALLOON SORT
Fourth Pass
2 4 6 7 9
0 1 2 3 4
March 2, 2024
Searching
01. Linear Search 02. Binary Search
This algorithm works by This type of searching algorithm is used to
sequentially iterating through find the position of a specific value
the whole array or list from contained in a sorted array. The binary
one end until the target search algorithm works on the principle of
element is found. If the divide and conquer and it is considered the
element is found, it returns best searching algorithm because it's faster
its index, else -1.
Search algorithms are
to run.
designed to check or
retrieve an element
from any data structure
Any algorithm which solves the search problem, namely, to retrieve where that element is
information stored within some data structure, or calculated in the search being stored. They
space of a problem domain, either with discrete or continuous values. search for a target (key)
in the search space.
LINEAR SEARCH
Approach for Linear or Sequential Search
• Start with index 0 and compare each element with the target
• If the target is found to be equal to the element, return its index
• If the target is not found, return -1
March 2, 2024
• Start with index 0 and
compare each element
Find: 7
• If the target is not found,
return -1
X
2 X
4 X
6 7 9
0 1 2 3 4
Return: 3
March 2, 2024
• Start with index 0 and
compare each element
Find: 8
• If the target is not found,
return -1
X
2 X
4 X
6 X
7 X
9
0 1 2 3 4
March 2, 2024
Return: -1
BINARY SEARCH
Approach for Binary Search
•Compare the target element with the middle element
of the array.
•If the target element is greater than the middle
element, then the search continues in the right half.
•Else if the target element is less than the middle
value, the search continues in the left half.
•This process is repeated until the middle element is
equal to the target element, or the target element is
not in the array
•If the target element is found, its index is returned,
else -1 is returned.
March 2, 2024
•Compare the target
element with the middle
element of the array.
March 2, 2024
Return: 7
•Compare the target
element with the middle
element of the array.
March 2, 2024
Return: -1
Multi-
Dimensional
Array
Programming 2
Erwin D. Marcelo
November 2023
A two-dimensional
array in C++ is the
simplest form of a multi-
dimensional array. It can
be visualized as an array of
arrays. The image below
depicts a two-dimensional
array.
A two-dimensional array is
also called a matrix. It can be
of any type like integer,
character, float, etc.
depending on the
initialization. In the next
section, we are going to
discuss how we can initialize
2D arrays.
In the above code,
•We firstly initialize a 2D array, arr[4]
[2] with certain values,
•After that, we try to print the respective
array using two for loops,
•the outer for loop iterates over the rows,
while the inner one iterates over the
columns of the 2D array,
•So, for each iteration of the outer
loop, i increases and takes us to the next
1D array. Also, the inner loop traverses
over the whole 1D array at a time,
•And accordingly, we print the individual
element arr[ i ][ j ].
33 Annual Review
Taking 2D Array Elements As User In
put
#include<iostream>
using namespace std;
main( )
{
int s[2][2];
int i, j;
cout<<"\n2D Array Input:\n";
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
cout<<"\ns["<<i<<"]["<<j<<"]= ";
cin>>s[i][j];
}
}