Arrays
Arrays
ARRAYS
ARRAY
1D
ARRAY
SEARCHING
ARRAY
IN AN
TRAVERSAL
ARRAY
MULTI-D
ARRAY
CONTENTS SORTING
AN ARRAY
ARRAY
• It is an ordered collection of
values of the same type.
• Elements can be accessed via
its index.
• All arrays consist of contiguous
memory locations.
elements
0 1 2 3 4
indices
ONE-DIMENSIONAL MULTI-DIMENSIONAL
ONE-DIMENSIONAL MULTI-DIMENSIONAL
METHODS
each element of an array
exactly once.
• It is necessary to create a
variable that will track the
position of the element
that is currently being
accessed.
ARRAY TRAVERSAL
Using for loop
Used for visiting,
METHODS
•
accessing, and printing
each element of an array
starts the for-
exactly once.
loop which uses int
• It is necessary to create a
ctr as an iterator
variable that will
variable initialized to 0 track the
position of the element
that is currently being
prints the value of every
accessed.
element of the array by
accessing through
the ctr variable which
runs from 0 to length
ARRAY TRAVERSAL
Using for-each loop
Used for visiting,
METHODS
•
accessing, and printing
each element of an array
starts the loop, traverses every
exactly
item of theonce.
array without using
• It is necessary tothecreateindex. a
variable that will track the
position ofthethe
prints valueelement
of ctr_1
that is currently being
accessed. the variable type is
automatically determined
using the keyword auto instead
of specifying it to be an int
ARRAY TRAVERSAL
Using range-based for loop
Used for visiting,
METHODS
•
accessing, and printing
each element of an array
exactly once.
• It is necessary to create a
variable that will track the
position of the element
that is currently being
accessed.
ARRAY TRAVERSAL
Searching an Array
Searching an Array
Searching an Array
Line Definition
16 defines an integer array named arr which is initialized to some values.
18 creates an integer variable key = 55 which holds the value it is searching for.
19 calls the function search passing arr, size, and key as the parameters which are
required to perform the search operation.
Searching an Array
Line Definition
s o r t i n g
bubble HOW IT WORKS?
15 34 28 36 12
PROCEDURE:
1. Start with the two elements, comparing them
to check which one is greater.
15 34 28 36 12
In this case, 34 is greater than 15, so it
is already in sorted locations.
s o r t i n g
bubble HOW IT WORKS?
15 34 28 36 12
We find that 28 is smaller than 34,
and these two values must be
swapped.
15 28 34 36 12
s o r t i n g
bubble HOW IT WORKS?
15 28 34 36 12
We find that both are already in sorted
positions.
15 28 34 36 12
s o r t i n g
bubble HOW IT WORKS?
15 28 34 36 12
We find that 12 is smaller than 36, hence they are
not sorted. So, we swap them.
15 28 34 12 36
s o r t i n g
bubble HOW IT WORKS?
15 28 34 12 36
To be precise, we are now showing how an array should
look like after each iteration.
After the second iteration, it should look like this:
15 28 12 34 36
s o r t i n g
bubble HOW IT WORKS?
15 28 12 34 36
15 12 28 34 36
12 15 28 34 36
Since 15 is greater than 12, then
there is no need to swap them.
12 15 28 34 36
Completely sorted array.
s o r t i n g
insertion
• Works similarly to how one sorts playing
cards in one’s hands.
• The array is virtually split into a sorted
and an unsorted part.
• It uses in-place comparisons.
• A sub-list is kept here, which is always
sorted.
• An element to be inserted in this sorted
sub-list must first find its appropriate
place and then insert it.
s o r t i n g
insertion HOW IT WORKS?
14 33 27 10 35 19 42 44
PROCEDURE:
1. Insertion sort compares the first two elements.
14 33 27 10 35 19 42 44
It finds that both 14 and 33 are already in
ascending order. For now, 14 is in the sorted sub-
list.
s o r t i n g
insertion HOW IT WORKS?
14 33 27 10 35 19 42 44
It finds that 33 is not in the correct position.
It swaps 33 with 27.
14 27 33 10 35 19 42 44
It also checks with all the elements of sorted sub-list.
Here, we see that the sorted sub-list has only one
element, 14. 27 is greater than 14. Hence, the sorted sub-
list remains sorted after swapping. By now, we have 14
and 27 in the sorted sub-list.
s o r t i n g
insertion HOW IT WORKS?
14 27 33 10 35 19 42 44
Since these values are not in
sorted order, we swap them.
14 27 10 33 35 19 42 44
However, we can see that 27 and 10 are not yet sorted.
s o r t i n g
insertion HOW IT WORKS?
14 27 10 33 35 19 42 44
However, swapping makes 27 and 10
unsorted, so we swap them too.
14 10 27 33 35 19 42 44
14 and 10 are in an unsorted order, so, we swap them again.
10 14 27 33 35 19 42 44
By the end of the third iteration, we have a sorted
sub-list of four items.
s o r t i n g
insertion HOW IT WORKS?
10 14 27 33 35 19 42 44
35 and 19 are still unsorted, so we
swap them again.
10 14 27 33 19 35 42 44
10 14 27 33 19 35 42 44
But then, swapping them makes 19 and 33
unsorted, so we swap them again.
s o r t i n g
insertion HOW IT WORKS?
10 14 27 19 33 35 42 44
However, swapping makes 27 and 19
unsorted, so we swap them too.
10 14 19 27 33 35 42 44
Now, we check 33 and 35. Since they are already
sorted, there is no need to swap them.
10 14 19 27 33 35 42 44
Then, we check 35 and 42. Since they are already
sorted, there is no need to swap them.
s o r t i n g
insertion HOW IT WORKS?
10 14 19 27 33 35 42 44
Then, we check 42 and 44. Since they are already in
sorted order, swapping is no longer needed.
10 14 19 27 33 35 42 44
We have reached the end of the
array.
10 14 19 27 33 35 42 44
Array has been completely sorted.
s o r t i n g
selection
• In each iteration of the selection
sort algorithm, the smallest
element from an unsorted list is
chosen and placed at the top of
the unsorted list.
• Based on in-place comparison and
divides the list into two parts, one
sorted and one unsorted.
s o r t i n g
selection HOW IT WORKS?
18 25 14 4 5 22 1
PROCEDURE:
1. For the first position in the sorted list, the whole
list is scanned sequentially.
• The first position where 18 is stored presently, we search
the whole list and find that 1 is the lowest value.
18 25 14 4 5 22 1
first lowest
position value
s o r t i n g
selection HOW IT WORKS?
18 25 14 4 5 22 1
1 25 14 4 5 22 18
After one iteration, 1, which happens to be
the minimum value in the list, appears in the
first position of the sorted list.
s o r t i n g
selection HOW IT WORKS?
1 25 14 4 5 22 18
1 25 14 4 5 22 18
We find that 4 is the second lowest value in the list, and it
should appear at the second location. So, we swap these
values.
1 4 14 25 5 22 18
s o r t i n g
selection HOW IT WORKS?
1 4 14 25 5 22 18
1 4 14 25 5 22 18
We find that 5 is the third lowest value in the list,
and it should appear at the third location. So, we
swap these values.
1 4 5 25 14 22 18
The same process is applied to the rest
of the items in the array.
s o r t i n g
selection HOW IT WORKS?
1 4 5 25 14 22 18
1 4 5 25 14 22 18
1 4 5 14 25 22 18
s o r t i n g
selection HOW IT WORKS?
1 4 5 14 25 22 18
1 4 5 14 25 22 18
1 4 5 14 18 22 25
s o r t i n g
selection HOW IT WORKS?
1 4 5 14 18 22 25
1 4 5 14 18 22 25
Since 22 and 25 are already sorted, there
is no need to swap these values.
1 4 5 14 18 22 25
Completely sorted array
s o r t i n g
Thank
You!
CS 131