Data Structures and Algorithms - Lecture 1 - Arrays

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 25
At a glance
Powered by AI
The key takeaways are that data structures are schemes for organizing data in memory and some common ones discussed are arrays, linked lists, stacks, queues, trees and graphs. The document also discusses sorting, searching and other algorithms.

Some common data structures discussed in the document include arrays, linked lists, stacks, queues, trees and graphs.

The main operations that can be performed on data structures are traversing, searching, insertion, deletion, sorting and merging.

Data Structure and

Algorithms
CSC7202

Ahmad Abba Datti


Course Contents
• Overview of Data structures and Algorithms
• Arrays
• Sorting Algorithms (Bubble Sort and Insertion Sort)
• Linked Lists
• Stacks
• Queues
• Quicksort
• Trees
• Graphs
• Searching Algorithms
Data Structures
A data structure is a
scheme for organizing
data in the memory of a
computer.

Some of the more


commonly used data
structures include lists,
arrays, stacks, queues,
heaps, trees, and graphs. Binary Tree
Data Structures
The way in which the
data is organized affects
the performance of a
program for different tasks.

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

Integer Real Character Boolean Linear Non-Linear

Simple Compound Tree Graph

Arrays Linked-Lists Stack Queue


Basic Operations
• The particular data structure chosen largely depends on
the frequency of the operation that needs to be
performed on the data structure.
• The data in the data structures are processed by certain
operations.
– Traversing
– Searching
– Insertion
– Deletion
– Sorting
– Merging
Introducing Arrays
Array is a data structure that represents a collection of the
same types of data. double[] myList = new double[10];

myList reference myList[0]


myList[1]
myList[2]
myList[3]
An Array of 10 myList[4]
Elements myList[5]
of type double myList[6]
myList[7]
myList[8]
myList[9]

An array is an indexed set of variables, such as dancer[1],


dancer[2], dancer[3],… It is like a set of boxes that hold things.
Arrays
• An array is a group of related data items that all have
the same name and the same data type.
• Arrays can be of any data type we choose.
• Arrays are static in that they remain the same size
throughout program execution.
• An array’s data items are stored contiguously in
memory.
• Each of the data items is known as an element of the
array. Each element can be accessed individually.
Array Declaration and Initialization
int numbers[ 5 ] ;

• The name of this array is “numbers”.


• This declaration sets aside a chunk of memory that is big enough
to hold 5 integers.
• It does not initialize those memory locations to 0 or any other
value. They contain garbage.
• Initializing an array may be done with an array initialization, as in
:
int numbers[ 5 ] = { 5, 2, 6, 9, 3 } ;

numbers 5 2 6 9 3
Initializing Arrays
• Using a loop:
for (int i = 0; i < myList.length; i++)
myList[i] = i;

• Declaring, creating, initializing in one step:


double[] myList = {1.9, 2.9, 3.4, 3.5};
This shorthand syntax must be in one statement.
Filling Large Arrays
• Since many arrays are quite large, using an array
initialization can be impractical.
• Large arrays are often filled using a for loop.
for ( i = 0; i < 100; i++ )
{
values [ i ] = 0 ;
}
would set every element of the 100 element array
“values” to 0.
Accessing Array Elements
• Each element in an array has a subscript (index) associated
with it.
• Subscripts are integers and always begin at zero.
• Values of individual elements can be accessed by indexing
into the array. For example,
print(“The third element = ”, numbers[ 2 ] ) ;

would give the output


The third element = 6. numbers 5 2 6 9 3
0 1 2 3 4
Modifying Elements
• Individual elements of an array can also be
modified using subscripts.
numbers[ 4 ] = 20 ; /*changes the value of
the element found at
subscript 4 to 20 */
• Initial values may be stored in an array using
indexing, rather than using an array initialization.
numbers[ 0 ] = 5 ;
numbers[ 1 ] = 2 ;
numbers[ 2 ] = 6 ;
numbers[ 3 ] = 9 ;
numbers[ 4 ] = 3 ;
The Length of Arrays
• Once an array is created, its size is fixed. It cannot
be changed. You can find its size using

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

• Objective: Use the selectionSort method to write a program


that will sort a list of double floating-point numbers.

int[] myList = {2, 9, 5, 4, 8, 1, 6}; // Unsorted


Sort it to produce 1, 2, 4, 5, 6, 8, 9

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 < 50 mid


[0] [1] [2] [3] [4] [5]
2 4 7 10 11 45

key > 7 mid

[3] [4] [5]


10 11 45

key = 11 mid
Google Classroom Code: h3fdf2

End
Implementation of
Arrays
• Creation
• Insertion
• Traversing
• Searching

• Reverse
𝟏 𝑵
• Standard deviation = σ 𝒙𝒊 − 𝝁 𝟐
𝑵 𝒊=𝟏

You might also like