Week4,5
Week4,5
Representation
Primitive Data Types
• Primitive data types are the most basic types provided by a
programming language
• They are typically atomic, meaning they do not consist of other
types.
• Integer Data Type Used for Example
Integer Whole numbers 7, 12, 999
• Floating Point Float (floating point) / Number with a decimal point 3.15, 9.06, 00.13
Double
• Character Character Encoding text numerically a, b, x etc.
Boolean Representing logical values TRUE, FALSE
• Boolean
• Void
2
Composite Data Types
• Composite data types are formed by combining multiple primitive data
types and/or other composite types.
• They provide more complex structures to represent real-world entities.
Few types of composite datatype are:
• Array
• Structure
• Pointer etc.
3
• Array:
o A collection of elements of the same type, stored in contiguous memory locations.
o Size is fixed at the time of creation.
o Example: int numbers[5] = {1, 2, 3, 4, 5};
• Structure (struct):
o Groups variables of different types into a single type.
o Members can be accessed individually using the dot operator.
o Example:
struct Person {
char name[50];
int age;
float height; };
7
8
Extended ASCII
▸ ASCII was originally developed for basic computers and uses
a 7-bit code
▸ As more computers began to work with 8-bit groups of data,
ASCII was written as 8 bits (Extended ASCII)
▸ Using eight bits instead of seven bits allows Extended ASCII
to provide codes for 256 characters
▸ Includes such characters as math symbols and Greek letters
▸ ASCII’s 256 characters, however, are not enough to handle
such languages as Chinese and Japanese, with their
thousands of characters
9
Unicode
▸ It was developed in the early 1990s
▸ Unicode uses 2 bytes for each character, hence it can handle 65,536 character
combinations
▸ Hence it allows almost all the written languages of the world to be represented using a
single character set
10
Text Document
▸ Plain, unformatted text is sometimes called ASCII text and is stored in a text
file with a name ending in .txt.
▸ In Windows, these files are labeled as “Text Document” while on Apple devices
these files are labeled “Plain Text.”
▸ ASCII text files contain no formatting
▸ To create documents with styles and formats, formatting codes have to be
embedded in the text
11
Formatted Text Document
▸ Microsoft Word produces formatted text and creates documents in DOCX
format
▸ Apple Pages produces documents in PAGES format
▸ Adobe Acrobat produces documents in PDF format
▸ HTML markup language used for Web pages produces documents in HTML
format
12
Data Representation: Image
• Bitmap Images (Raster Graphics):
A bitmap is an array of bits that specify the color of each pixel in a rectangular array of
pixels. The number of bits used per pixel determines the color range.
• Vector Images:
Made up of lines and shapes that are mathematically defined. This means that they can
be scaled up or down without losing any quality. They are often used for logos and
illustrations. The most common vector file types are SVG, AI, EPS, PDF, and CRR.
• Image Formats:
• BMP: Uncompressed bitmap format.
• JPEG: Compressed format using lossy compression.
• PNG: Compressed format using lossless compression. Etc.
15
Data Representation: Audio
Audio data is represented digitally by sampling sound waves and encoding the samples
in binary.
• Audio Formats:
• WAV: Uncompressed audio format.
• MP3: Compressed format using lossy compression.
• FLAC: Compressed format using lossless compression.
Data Representation: Video
Video is a sequence of images (frames) displayed in quick succession, typically
accompanied by audio.
• Frames per Second (FPS):
• The number of frames displayed per second.
• Resolution:
• The number of pixels in each dimension that the video frame contains.
Example Formats: MP4, AVI, MKV.
Introduction to Data
Structures and Algorithm
Data Structures
● Data structure is a representation of data and the operations allowed on that data.
● A data structure is a way to organize and store data in order to facilitate the access and
modifications.
● Data structure is the implementation that we provide or specify like set of rules etc. that
defines how the data will be accessed, modified, or stored etc.
21
Data Structures
● Any data structure is designed to organize data to suit a specific purpose so that it can be
accessed and worked with in appropriate ways
● In computer programming, a data structure may be selected or designed to store data for
the purpose of working on it with various algorithms
22
Real World Example
Dictionary is an example of how data structure work in real life.
There are more than a thousand words in a dictionary. Suppose you want to search the
definition of the word “Work”. You know that first you will look in the “W” section then in the
“Wo” so on to find the word “Work”. This process sounds easy because the dictionary is
arranged in this particular order. It would be impossible to search for a word in a dictionary
without any order. This is exactly how data structures work.
Data structure organizes, and sorts your data as per pre-defined rule or order.
23
Data Structure Classification
Generally classified into:
24
Primitive Data Structures
• Fundamental data types which are supported by a programming
language
• Basic data types such as integer, real, character and Boolean are
known as Primitive Data Structures
• These data types consists of characters that cannot be divided and
hence they also called simple data types
25
Non-Primitive Data Structures
• Non-primitive data structures are those data structures which are
created using primitive data structures
• Examples of non-primitive data structures is the processing of
complex numbers
• Based on the structure and arrangement of data, further classified
into
1. Linear Data Structure, and
2. Non – Linear Data Structure
26
Types of Data Structures
There are two types of data structures:
1. Linear (data is stored in a sequence)
2. Non Linear (data is not stored in a sequence)
27
Linear Data Structure
• A data structure is said to be linear if its elements form a sequence or
a linear list.
• The common examples of linear data structure are Arrays, Linked Lists
, Queues, Stacks
28
Non Linear Data Structure
• A data structure is said to be non-linear if the data are not arranged in
sequence or a linear
• This structure is mainly used to represent data containing a
hierarchical relationship between elements.
• Trees and Graphs are the examples of non-linear data structures
29
Data Structure Classification
30
Graphical Representation
Array
Linked list
Queue
Tree
Stack
Array
• Array is a structure/container which can hold a fix number of items and these
items should be of the same type
• Most of the data structures make use of arrays to implement their algorithms
• Following are the important terms to understand the concept of Array
• Element - Each item stored in an array is called an element.
• Index - Each location of an element in an array has a numerical
index, which is used to identify the element.
32
Representation of Array
• Arrays can be declared in various ways in different languages.
• For illustration, let's take C array declaration
33
Stack
• It is a linear data structure.
• Consider an example of books stacked over one another. The book which is at
the top is the first one to be removed, i.e. the book which has been placed at the
bottommost position remains in the stack for the longest period of time. So, it
can be simply seen to follow LIFO(Last In First Out) order.
34
Working of a Stack
• In programming terms, putting an item on top of the stack is called push and removing an
item is called pop.
✓ Push: Add an element to the top of a stack
✓ Pop: Remove an element from the top of a stack
35
Example
• Suppose the following stack • Push 10 in stack
10
6
3
3
4
4
5
5
• It follows a particular order in which the operations are performed i.e. FIFO
(First In First Out).
• It is similar to the ticket queue outside a cinema hall, where the first person
entering the queue is the first person who gets the ticket.
37
Working of a Queue
• Queue follows the First In First Out (FIFO) rule - the item that goes in first is the item that
comes out first. In programming terms, putting items in the queue is called enqueue, and
removing items from the queue is called dequeue.
38
Example
• Suppose the following queue • Add 10 in queue (enqueue)
10 6 3 2 1
3 2 1
6 3 2 1 10 6 3 2
11 10 6 3 2
39
Linked List
• It is a linear data structure.
• Includes a series of connected nodes. Here, each node stores the data and the
address of the next node. For example:
40
Linked List
• Real life example:
1. Image viewers: When you navigate through images in an image viewer, a linked
list allows you to scroll through the images.
2. Music players: You can use a linked list to scroll through songs in a music player
41
Tree
• A Tree is a collection of elements called nodes
42
Tree
43
Graph
• A Non-Linear data structure consisting of two things
• Nodes / Vertices – set of elements
• Edges / Links – identified by a pair of nodes
44
Graph – Nodes and Edges
45
Types of Graph
46
Types of Graph
47
Types of Graph
48
Graph and Tree
A connected acyclic graph is called a tree
49
Graph and Tree
A connected acyclic graph is called a tree
50
Key Operations on Data Structures
Key Operations
• The data appearing in data structures are processed by means of certain
operations
52
Traversing
• Accessing each record/node exactly once so that certain items in the record
may be processed
53
Searching
• Finding the location of the desired node with a given key value, or
• Finding the locations of all such nodes which satisfy one or more conditions
54
Inserting / Deleting
• Adding a new node/record to the structure
55
Introduction To Basic Algorithms
and Problem-solving Techniques
Quick Sort
Array
2 6 5 3 8 7 1 0
Select Pivot
2 6 5 3 8 7 1 0
Pivot
Move Pivot to the end of array
2 6 5 3 8 7 1 0
Pivot SWAP
2 6 5 0 8 7 1 3
Pivot
59
Select:
itemFromLeft > Pivot and
itemFromRight < Pivot
2 6 5 0 8 7 1 3
Pivot
60
Swap itemFromLeft with itemFromRight.
2 6 5 0 8 7 1 3
Pivot
SWAP
2 1 5 0 8 7 6 3
Pivot
61
Quick sort is recursive so we will repeat this process again
Select:
itemFromLeft > Pivot and
itemFromRight < Pivot
> Pivot
itemFromLeft
2 1 5 0 8 7 6 3
Pivot
< Pivot
itemFromRight 62
Swap itemFromLeft with itemFromRight.
2 1 5 0 8 7 6 3
Pivot
SWAP
2 1 0 5 8 7 6 3
Pivot
63
Select:
itemFromLeft > Pivot and
itemFromRight < Pivot
> Pivot
itemFromLeft
2 1 0 5 8 7 6 3
Pivot
< Pivot
itemFromRight
2 1 0 5 8 7 6 3
Pivot
< Pivot
itemFromRight SWAP
2 1 0 3 8 7 6 5
Pivot 65
Pivot is now in correct position
• Right numbers are all smaller than pivot
• Left numbers are all greater than pivot
2 1 0 3 8 7 6 5
Pivot
66
Split the array. Now we have to repeat all the steps to sort
left and right arrays
2 1 0 3 8 7 6 5
Pivot
2 1 0 3 8 7 6 5
Right Left
Array Array
67
itemFromLeft
8 7 6 5 8 5 6 7
8 7 6 5 itemFromRight
Pivot 8 5 6 7
8 7 6 5
SWAP
SWAP
6 5 8 7
8 5 6 7
68
itemFromLeft
itemFromRight
SWAP
6 5 7 8
69
2 1 0 3 6 5 7 8
70
Binary Search Algorithm
71
mid = low + (high - low) / 2
0 + (9 - 0) / 2 = 4
31 > 27
72
31 < 35