0% found this document useful (0 votes)
2 views

Arrays

The document provides an overview of arrays, detailing one-dimensional and multi-dimensional arrays, including their initialization, traversal methods, and searching techniques. It also explains various sorting algorithms such as bubble sort, insertion sort, and selection sort, illustrating their procedures and how they operate. Additionally, it emphasizes the importance of arrays in data management and operations.

Uploaded by

Althea Rhien
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Arrays

The document provides an overview of arrays, detailing one-dimensional and multi-dimensional arrays, including their initialization, traversal methods, and searching techniques. It also explains various sorting algorithms such as bubble sort, insertion sort, and selection sort, illustrating their procedures and how they operate. Additionally, it emphasizes the importance of arrays in data management and operations.

Uploaded by

Althea Rhien
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 51

CS 131

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

It is made up of one row or one


column of array members with
the same name but different
index values

The 2D array is the most basic


type of multi-dimensional
array.
ONE-DIMENSIONAL ARRAY

we have created a group of int values named


numarray with a max capacity of 10 elements.

Assigns 50.00 in the 5th index of the array


balance.
ONE-DIMENSIONAL ARRAY

Using for loop to access all elements of an array


ONE-DIMENSIONAL ARRAY

Arrays could also be an ordered group of


characters, commonly known as a string, with
data type char.

String arrays could also be initialized by simply


assigning it to a string of text enclosed with double
quotes.
MULTI-DIMENSIONAL ARRAY

Initialized an integer array named


numarray, with 3 rows and 4
columns.

Initialized an integer array


named numarray, with 3
rows and 4 columns.
MULTI-DIMENSIONAL ARRAY

Initialized an integer array named


numarray, with 3 rows and 4
columns.

Assigning 45 to row 1 column 2


of numarray.
MULTI-DIMENSIONAL ARRAY

Accessing 2D Array Elements using for-loop


MULTI-DIMENSIONAL ARRAY

Accessing 3D Array Elements using for-loop


MULTI-DIMENSIONAL ARRAY

Element 1 { {3, 4, 2, 3}, {0, -3, 9, 11}, {23, 12, 23, 2} }


Element 2 { {13, 4, 56, 3}, {5, 9, 3, 5}, {5, 1, 4, 9} }

The second dimension has the value 3. Notice that


each of the elements of the first dimension has three
elements each. There are four int numbers inside
each of the elements of the second dimension.
Maintaining a large chunk of data
using variable names.

To easily sort elements.

ARRAY For matrix operations.

USE CPU Scheduling

For implementing other data structures


such as: stacks, queue, heaps, etc.
ARRAY
TRAVERSAL
• Used for visiting,
accessing, and printing

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

4-12 Defines the search function


where it traverses through
the array arr based on the
length size.
Sorting
an array
bubble
• Most straightforward sorting method
• Repeatedly switches nearby
components if they are in the wrong
order.
• The name “bubble sort” comes from
each item in the list “bubbles” up to
where it should go, similar to water
bubbles.

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?

2. Next, we compare 34 by 28.

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?

3. Next, we compare 34 and 36.

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?

4. Then, we move to the next values, 36 and 12.

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?

5. We find that we have reached the end of the


array. After one iteration, the array should look
like this:

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?

Notice that after each iteration, at least one value moves


at the end.

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?

2. Insertion sort moves ahead and compares 33 with 27.

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?

3. Next, it compares 33 with 10.

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?

2. So, we replace 18 with 1.

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?

3. For the second position, where 25 is residing, we


start scanning the rest of the list in a linear manner.

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?

4. After two iterations, two least values are positioned


at the beginning in sorted manner.

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?

The same process is applied to the rest of


the items in the array.

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?

The same process is applied to the rest of


the items in the array.

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?

The same process is applied to the rest of


the items in the array.

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

You might also like