Data Structures and Algorithms - Lecture 1 - Arrays
Data Structures and Algorithms - Lecture 1 - Arrays
Data Structures and Algorithms - Lecture 1 - Arrays
Algorithms
CSC7202
Computer programmers
decide which data
structures to use based on
the nature of the data
and the processes that Binary Tree
need to be performed on
that data.
Classification of Data Structure
Data Structures
Files and
Primitives Non-Primitives
Databases
numbers 5 2 6 9 3
Initializing Arrays
• Using a loop:
for (int i = 0; i < myList.length; i++)
myList[i] = i;
arrayVariable.length
For example,
myList.length returns 10
Multidimensional Array Illustration
0 1 2 3 4 0 1 2 3 4 0 1 2
0 0 0 1 2 3
1 1 1 4 5 6
2 2 7 2 7 8 9
3 3 3 10 11 12
4 4 int[][] array = {
{1, 2, 3},
matrix = new int[5][5]; matrix[2][1] = 7; {4, 5, 6},
{7, 8, 9},
{10, 11, 12}
};
Using Arrays in Sorting
2, 9, 5, 4, 8, 1, 6
Using Arrays in Sorting
int[] myList = {2, 9, 5, 4, 8, 1, 6}; // Unsorted
Find the largest element in myList and swap it with the last element in
myList.
2, 9, 5, 4, 8, 1, 6 => 2, 6, 5, 4, 8, 1, 9 (size = 7)
2, 6, 5, 4, 8, 1 => 2, 6, 5, 4, 1, 8 (size = 6)
2, 6, 5, 4, 1 => 2, 1, 5, 4, 6 (size = 5)
2, 1, 5, 4 => 2, 1, 4, 5
2, 1, 4 => 2, 1, 4,
2, 1 => 1, 2
Sort it to produce 1, 2, 4, 5, 6, 8, 9
Bubble Sort
int[] myList = {2, 9, 5, 4, 8, 1, 6}; // Unsorted
Pass 1: 2, 5, 4, 8, 1, 6, 9
Pass 2: 2, 4, 5, 1, 6, 8, 9
Pass 3: 2, 4, 1, 5, 6, 8, 9
Pass 4: 2, 1, 4, 5, 6, 8, 9
Pass 5: 1, 2, 4, 5, 6, 8, 9
Pass 6: 1, 2, 4, 5, 6, 8, 9
Searching Arrays
Searching is the process of looking for a
specific element in an array; for example,
discovering whether a certain score is
included in a list of scores. Searching,
like sorting, is a common task in computer
programming. There are many algorithms and
data structures devoted to searching. In
this section, two commonly used approaches
are discussed, linear search and binary
search.
Linear Search
The linear search approach compares the
key element, key, with each element in the
array list[]. The method continues to do
so until the key matches an element in the
list or the list is exhausted without a
match being found. If a match is made, the
linear search returns the index of the
element in the array that matches the key.
If no match is found, the search returns -
1.
Binary Search
For binary search to work, the elements in
the array must already be ordered. Without
loss of generality, assume that the array
is in ascending order.
e.g. 2 4 7 10 11 45 50 59 60 66 69 70 79
The binary search first compares the key
with the element in the middle of the
array. Consider the following three cases:
Binary Search, cont.
If the key is less than the middle
element, you only need to search the key
in the first half of the array.
If the key is equal to the middle
element, the search ends with a match.
If the key is greater than the middle
element, you only need to search the key
in the second half of the array.
Binary Search, cont.
key = 11
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12]
list 2 4 7 10 11 45 50 59 60 66 69 70 79
key = 11 mid
Google Classroom Code: h3fdf2
End
Implementation of
Arrays
• Creation
• Insertion
• Traversing
• Searching
• Reverse
𝟏 𝑵
• Standard deviation = σ 𝒙𝒊 − 𝝁 𝟐
𝑵 𝒊=𝟏