0% found this document useful (0 votes)
108 views53 pages

CSC 221 Intro To DSA - Arrays 23022023 111731am

This document provides an introduction to the CSC-221 Data Structures and Algorithms course. It outlines the course instructor, meeting times and resources. The course aims to emphasize the importance of data structures and efficient algorithms. Key topics include common data structures like stacks, queues, linked lists, trees and graphs. Students will learn to select appropriate data structures for problems, design abstract data types, and analyze algorithm complexities. The course objectives are to equip students with skills for real-world problem solving using data structures and algorithms.

Uploaded by

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

CSC 221 Intro To DSA - Arrays 23022023 111731am

This document provides an introduction to the CSC-221 Data Structures and Algorithms course. It outlines the course instructor, meeting times and resources. The course aims to emphasize the importance of data structures and efficient algorithms. Key topics include common data structures like stacks, queues, linked lists, trees and graphs. Students will learn to select appropriate data structures for problems, design abstract data types, and analyze algorithm complexities. The course objectives are to equip students with skills for real-world problem solving using data structures and algorithms.

Uploaded by

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

CSC-221 DATA STRUCTURES AND ALGORITHMS

Instructor: Ayesha Jamal


Lecture 01: Introduction to Data Structures
INSTRUCTOR

Ms. Ayesha Jamal


Office: Room # 4, NC Basement
Email:
[email protected]

2
COURSE INFORMATION

 Pre-requisite
 Computer Programming/Object Oriented Programming

 Course meeting times


 Lectures: 2 sessions/week (1.5hrs)
 Labs: 1 session/week (3hr)
• Programming Language: C++
• Complier: Visual Studio/ Dev C++

 Course Resources
 Lectures slides, assignments (computer/written), recommended book/s, and announcements will be uploaded on 3

LMS announcement section.


COURSE AIM
 To emphasize the importance of data structures and efficient algorithmic
design for better management of computing resources while programming.

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

At the completion of this course students are expected to be able to:


• Identify and apply appropriate data structures for a particular scenario.
• Design and implement Abstract Data Types (ADT) on demand.
• Analyze the complexity of algorithms and understand time-space tradeoff.
• Provide efficient data management in terms of insertion and retrieval.

6
REFERENCE MATERIAL

• Data Structures and Algorithm Analysis in C++, by Mark Allen Weiss, 4 th


Edition, Published by Addison-Wesley, 2013.
• Data Structures and Algorithms using C & C++, Augenstein & Tenenbaum,
2015.
• C++ Plus Data Structures, 5th Edition, Nell Dale, Jones and Bartlett Learning,
2015.
• Data Structures using C++, Varsha H. Patil, 6th Edition, Oxford University
Press, 2016.
• Data Structures and Algorithms, Aho, Hopcroft & Ullman.
• A Practical Introduction to Data Structures and Algorithm Analysis, C.A.
Shaffer
• Introduction to Data Structures in C, Ashok N. Kamthane
7
GRADING POLICY

Assignments; 20%

Finals; 50% Quizes; 10%

Mids; 20%

8
9
DATA HIERARCHY

 From smallest to largest Sally Black


Tom Blue
 Bit: (1s or 0s) Judy Green File
Iris Orange
 Byte: 8 bits (char)
Randy Red
 Field: group of characters with some meaning
 Record: group of related fields
Judy Green Record
 Database: group of related files
Judy Field

01001010 Byte (ASCII character J)

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

Abstract Data Type


Primitive Data Type
 The abstract datatype is special kind of datatype,
 Primitive Data Type is basically a type of data
whose behavior is defined by a set of values and
that can be used in different computer program.
set of operations.
 It signifies the type like integer, float etc, the
 The keyword “Abstract” is used as we can use
space like integer will take 4-bytes, character
these datatypes, we can perform different
will take 1-byte of space etc.
operations. But how those operations are working
that is totally hidden from the user.
 The ADT is made of with primitive datatypes, but
operation logics are hidden.
12
DATA STRUCTURES 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?

• Can be written in simple English


• Can be a flow chart
• Can be a pseudocode
17
DATA STRUCTURE AND ALGORITHMS - RELATION
 Algorithm: Step by step procedure
 Computer Program: Implementation of the algorithm
 Data structure: Organization of data that is manipulated by the algorithm or the computer program

Efficient Algorithm

Appropriat
Input e Data
Good Output
Structure
Computer Program

• Provides the required results


• Easy to understand, code and debug 18

• Makes efficient use of resources


EFFICIENCY OF ALGORITHM
 Some algorithms are more efficient than others.
 The complexity of an algorithm is a function describing the efficiency of
the algorithm in terms of the amount of data the algorithm must process.
 Main complexity measures of the efficiency of an algorithm:
 Time complexity is a function describing the amount of time an
algorithm takes in terms of the amount of input to the algorithm.
 Space complexity is a function describing the amount of memory
(space) an algorithm takes in terms of the amount of input to the
algorithm. 19
EFFICIENT ALGORITHM…???
 Efficiency of an algorithm depends on the amount of resources it uses.
• Space (# of variables used)
• Time (# of instructions used)

Efficiency as function of input size (n)


 Number of data items

 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)

An Abstract Data Type (ADT) specifies how a


collection of data will be stored and what
operations can be performed on the data to
access and change it.

23
 For example, we can store a list of items having the same data-type using
the array data structure.

 Array Data Structure

24
DATA STRUCTURES CATEGORIES

The data structure can be subdivided into major types:


• Linear Data Structure
• Non-linear Data Structure

25
LINEAR DATA STRUCTURES

 A data structure is said to be linear if data elements are arranged in a


linear order.
 Examples of the linear data structure
 Arrays
 Queues
 Stacks
 Linked lists
26
NON-LINEAR DATA STRUCTURES

 This structure is mostly used for representing data that contains a


hierarchical relationship among various elements.
 Examples of Non-Linear Data Structures
 Graphs
 Trees

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…….

 Extreme hard work as not an easy subject

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

 Commonly used asymptotic notations to calculate the running time


complexity of an algorithm.
 Big Oh Ο Notation
 Omega Ω Notation
 Theta θ Notation

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

 0 (zero-based indexing): The first element of the array is indexed by a


subscript of 0.
 1 (one-based indexing): The first element of the array is indexed by the
subscript of 1.
 n (n-based indexing): The base index of an array can be freely chosen.
Usually, programming languages allowing n-based indexing also allow
negative index values
42
ARRAY DECLARATION

#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

 Array declaration by specifying size:


int arr1[10];
// declare an array of user specified size
int n = 10;
int arr2[n];
 Array declaration by initializing elements:
int arr[] = { 10, 20, 30, 40 }
// Compiler creates an array of size 4.
44
 Array declaration by specifying size and initializing elements:
int arr[6] = { 10, 20, 30, 40 }
// Compiler creates an array of size 6, initializes first
// 4 elements as specified by user and rest two elements as 0

45
/C++

46
TYPES OF ARRAYS

Types of arrays are:


 One Dimensional
 Multi-Dimensional

47
1-D ARRAY

 As Studied

48
MULTIDIMENSIONAL ARRAYS

 In C/C++, we can define multidimensional arrays in simple words as


array of arrays.
 Data in multidimensional arrays are stored in tabular form (in row major
order).
 General form of declaring N-dimensional arrays:
data_type array_name[size1][size2]....[sizeN];

49
EXAMPLES

Two dimensional array:


int two_d[10][20];
Three dimensional array:
int three_d[10][20][30];

50
SIZE OF MULTIDIMENSIONAL ARRAYS

 Total number of elements that can be stored in a multidimensional array


can be calculated by multiplying the size of all the dimensions.
For example:
 The array int x[10][20] can store total (10*20) = 200 elements.
Similarly array int x[5][10][20] can store total (5*10*20) = 1000
elements.

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

You might also like