CSC 221 Intro To DSA - Arrays 23022023 111731am
CSC 221 Intro To DSA - Arrays 23022023 111731am
2
COURSE INFORMATION
Pre-requisite
Computer Programming/Object Oriented Programming
Course Resources
Lectures slides, assignments (computer/written), recommended book/s, and announcements will be uploaded on 3
4
COURSE OBJECTIVES
• Equip students with skills to select and design data structures and algorithms that
are appropriate for real world problem solving
• Developing basic algorithms for manipulating stacks, queues, linked lists, trees,
graphs.
• Study and design recursive algorithms for applications in trees and graphs.
• Study and implement searching and sorting algorithms
• Study the computational complexities of different algorithms
5
EXPECTED STUDENT OUTCOMES
6
REFERENCE MATERIAL
Assignments; 20%
Mids; 20%
8
9
DATA HIERARCHY
1 Bit 10
DATA STRUCTURES …..WHAT?
In computer science, a data structure is a data organization, management, and storage
format that enables efficient access and modification. More precisely, a data structure is
a collection of data values, the relationships among them, and the functions or operations
that can be applied to the data.
11
PRIMITIVE VS ABSTRACT DATA TYPES
13
DATA STRUCTURES…..WHY?
Market value: Big tech companies like Microsoft etc. focus mainly on data structures.
Computer & Telecom industry
Search engines (Google, Amazon etc.)
14
TIME IT TOOK YOU TO FIND A SHIRT …
15
FIND YOUR FRIEND IN HERE……
16
WHERE DO ALGORITHMS FIT …???
An algorithm is a set of sequential instructions for solving a problem.
Algorithms process (manipulate) data.
Example: How to make a cup of tea?
Efficient Algorithm
Appropriat
Input e Data
Good Output
Structure
Computer Program
Time-Space Trade-off
Ideally, an efficient algorithm is one which uses less memory and requires less time to complete the
task at hand.
In reality, this is not always possible!!!
Time-Space trade-off is a situation where memory use can be reduced at the cost of slower program 20
execution (and conversely, the computation time can be reduced at the cost of increased memory use)
APPROPRIATE DATA STRUCTURE
• No single data structure works well for all purposes, and so it is important
to know the strengths and limitations of several of them
• Choose the most appropriate data structure for a given data/problem
21
SO AS WE DISCUSSED EARLIER….
Data Structure is way of collecting and organizing data in such a way that can
perform operations on these data in an effective way.
Data Structures are programmed to store ordered data, so various operation can
be performed easily.
DS - What?
A method/technique of organizing data.
DS - Why?
To manage huge amount of data efficiently. 22
ABSTRACT DATA TYPE (ADT)
23
For example, we can store a list of items having the same data-type using
the array data structure.
24
DATA STRUCTURES CATEGORIES
25
LINEAR DATA STRUCTURES
27
BASIC OPERATION OF DATA STRUCTURES
Traversing
Searching
Insertion
Deletion
Sorting
Merging
28
REFRESH EARLIER PROGRAMMING CONCEPTS
C++ Concepts
Pointers
Arrays
Parameters
File handling
OOP Concepts
Classes
Structures
Dynamic memory
Return values & constructors
Templates 29
WHAT WILL IT BE LIKE…….
30
ASYMPTOTIC ANALYSIS OF ALGORITHM
Asymptotic analysis refers to computing the running time of any
operation in mathematical units of computation.
Using asymptotic analysis, we can very well conclude the best case,
average case, and worst case scenario of an algorithm.
Time required by an algorithm falls under three types:
Best Case − Minimum time required for program execution.
Average Case − Average time required for program execution.
Worst Case − Maximum time required for program execution.
31
ASYMPTOTIC NOTATIONS
32
BIG OH NOTATION
The notation Ο(n) is the formal way to express the upper bound of an
algorithm's running time.
It measures the worst case time complexity or the longest amount of time
an algorithm can possibly take to complete.
33
OMEGA NOTATION
The notation Ω(n) is the formal way to express the lower bound of an
algorithm's running time.
It measures the best case time complexity or the best amount of time an
algorithm can possibly take to complete.
34
THETA NOTATION
The notation θ(n) is the formal way to express both the lower bound and
the upper bound of an algorithm's running time.
It measures the average case time complexity or the average amount of
time an algorithm can possibly take to complete.
35
ARRAYS
36
ARRAYS
An array is a collection of items stored at contiguous memory locations.
Syntax:
data_type array_name [array_size];
Essential Terminologies:
Element: Every item stored in an array is termed as an element
Index: Each memory location of an element in an array is denoted by a numerical index
which is used for identifying the element
37
The idea is to store multiple items of the same type together.
Easier to calculate the position of each element by simply adding an
offset to a base value, i.e., the memory location of the first element of the
array (generally denoted by the name of the array).
38
39
WHY DO WE NEED ARRAYS?
We can use normal variables (v1, v2, v3, ..) when we have a small number of
objects, but if we want to store a large number of instances, it becomes difficult
to manage them with normal variables.
If you are not using arrays, then the number of variables used will increase.
Using the array, the number of variables reduces, i.e., you can use a single name
for multiple values, you need to deal with its index values (starting from 0 to n).
The idea of an array is to represent many instances in one variable.
40
ARRAY’S SIZE
Array has a fixed size meaning once the size is given to it, it cannot be
changed i.e. you can’t shrink it neither can you expand it.
The reason was that for expanding, if we change the size we can’t be sure
( it’s not possible every time) that we get the next memory location to us
as free.
The shrinking will not work because the array, when declared, gets
memory statically allocated, and thus compiler is the only one can
destroy it.
41
TYPES OF INDEXING IN AN ARRAY
#include <iostream>
using namespace std;
int main()
{
int arr[10]; // Creating an integer array named arr of size 10.
arr[0] = 5; // accessing element at 0 index and setting its value to 5.
cout << arr[0];// print value at 0 index we get the output as 5.
return 0;
43
ARRAY DECLARATION
45
/C++
46
TYPES OF ARRAYS
47
1-D ARRAY
As Studied
48
MULTIDIMENSIONAL ARRAYS
49
EXAMPLES
50
SIZE OF MULTIDIMENSIONAL ARRAYS
51
TRAVERSING AN ARRAY
#include<stdio.h>
using namespace std;
int main()
{ Output:
int arr[6]={11,12,13,14,15,16}; 11 12 13 14 15 16
for(int i=0;i<6;i++)
cout<<arr[i]<<" ";
cout<<endl;
return 0; 52
}
REAL LIFE APPLICATIONS OF ARRAYS
Arrangement of leader-board of a game can be done simply through
arrays to store the score and arrange them in descending order to clearly
make out the rank of each player in the game.
A simple question Paper is an array of numbered questions with each of
them assigned to some marks.
2D or 3D arrays, commonly known as, matrix, are used in image
processing.
It is also used in speech processing, in which each speech signal is an
array. 53