You are on page 1of 28

Data Structure & Algorithms

Chapter 1
Introduction & Overview
Lecturer Imran Ali Memon
@ School of IT [SBBU,SBA]
Course Outline:

1. Introduction to Data Structures and Algorithms;


2. Complexity Analysis;
3. Arrays;
4. Sorting Algorithms:
Insertion Sort, Selection Sort, Bubble Sort, Shell Sort, Heap Sort, Quick Sort, Merge
Sort, Radix Sort, Bucket Sort;
5. Linked Lists: Singly Linked Lists, Doubly Linked Lists, Circular List;
6. Stacks, Queues, and Priority Queue;
7. Recursion: Function call and Recursion Implementation, Tail Recursion, Non-tail
Recursion, Indirect Recursion, Nested Recursion, Backtracking.
8. Trees: Binary Trees, Binary Heap, Binary Search Tree Traversal, Insertion, Deletion, and
Balancing a Tree; Heap; B-Tree; Spanning Tree, Splay Trees;
9. Graphs:
Representation, Traversal, Shortest Path, and cycle Detection; Isomorphic Graphs;
Graph Traversal Algorithms; Hashing; Memory Management and Garbage Collection.

Imran Ali Memon ( School of IT ) 2


References
• Data Structures by Seymour , SCHAUM’S Series.
• Data Structures with C++, SCHAUM’S Series.

Imran Ali Memon ( School of IT ) 3


Contents
1.1 Basic Terminology
1.2 Data Structures
1.3 Data Structure Operations

Imran Ali Memon ( School of IT ) 4


Introduction
• Contents of this course will repeat again & again in
every semester like C/C++.
• Foundation course.
• You have already seen basic data structures in C/C++
but focus was on programming.
– Int
– Char
– Float etc…
• Data have importance in Computer Science that’s why
Organization & Arrangement of data have great
importance.
Imran Ali Memon ( School of IT ) 5
Why DS?
• DS organize data.
• More powerful computers.
• Complex problem can be solved.
• Saving of resources.
– Space
– Time

Imran Ali Memon ( School of IT ) 6


Organizing Data
• Any organization for a collection of records that can
be searched, processed or modified.
• The choice of data structure and algorithm can
make the difference between a program running in
a few seconds or many days.
– Ex: Array vs Variable

Imran Ali Memon ( School of IT ) 7


1.1 Basic Terminology
• Data :
– Data are simply collection of facts and figures.
– Data are values or set of values.
• Data item :
– A data item refers to a single unit of values.

• Data items that are divided into sub items are group items; those
that are not are called elementary items.

• For example, a student’s name may be divided into


three sub items – [first name, middle name and last name] but
the ID of a student would normally be treated as a single item.

Imran Ali Memon ( School of IT ) 8


1.1 Basic Terminology

In the above example ( ID, Age, Gender, First, Middle, Last, Street, Area ) are
elementary data items, whereas (Name, Address ) are group data
Imran Ali Memon ( School of IT ) 9
1.1 Basic Terminology
• An entity (here entity means table) is something that has
certain attributes or properties which may be
assigned values.
• The values themselves may be either numeric or
non-numeric.

• Attributes: Name Age Gender Roll #

Std1 20 M 123
• Values:
Std2 19 F 122

Imran Ali Memon ( School of IT ) 10


1.1 Basic Terminology
• The term “information” is sometimes used for data with
given attributes or in other words meaningful or processed
data.
• A field is a single elementary unit of information representing
an attribute of an entity.
• A record is the collection of field values of a given entity.
• Records may also be classified according to length.
– Fixed length records (Name, F.Name, Age)
– Variable length records (Name, Address, Experience )
• A file is the collection of records of the entities in a given
entity set.

Imran Ali Memon ( School of IT ) 11


1.2 Data Structure
• The logical or mathematical model of a particular
organization of data is called a data structure.
• In computer science, a data structure is a particular way of
storing and organizing data in a computer’s memory so that
it can be used efficiently.
• Data may be organized in many different ways.
• The choice of a particular data model depends on the two
considerations first:
– It must be rich.
– Should be simple enough that one can effectively process the
data whenever necessary.

Imran Ali Memon ( School of IT ) 12


1.2 Data Structure
Classification of Data Structure:

Data Structure

Primitive Data Structure Non-primitive Data Structure

Integer
Linear Data Structure Non-Linear Data Structure
Boolean
Arrays Tree
Character
Linked List Graph
Float
Queue

Stack
Imran Ali Memon ( School of IT ) 13
1.2 Data Structure
• The non-primitive data structure can be classified
in two major types:

1. Linear Data Structure


2. Non-linear Data Structure

Imran Ali Memon ( School of IT ) 14


1.2 Data Structure
Linear Data Structure:
A data structure is said to be linear if its elements
form any sequence.
The common examples of linear data structure are
Arrays
Linked lists
Queues
Stacks

Imran Ali Memon ( School of IT ) 15


1.2 Data Structure
Non-linear Data Structure:
• This structure is mainly used to represent data
containing a hierarchical relationship between
elements.
• The common examples of non-linear data
structure are:
– Tree
– Graph

Imran Ali Memon ( School of IT ) 16


1.2 Data Structure
Arrays:
• The simplest type of data structure is a linear (or one
dimensional) array.
• A list of a finite number n of similar data referenced
respectively by a set of n consecutive numbers, usually
1, 2, 3 . . . . . . . n.
• Example:
A linear array A[8] consisting of numbers is pictured in
following figure.

Imran Ali Memon ( School of IT ) 17


1.2 Data Structure
Linked List:
• A linked list or one way list is a linear collection of data
elements called nodes.
• Where the linear order is given by means of pointers.
• Each node is divided into two parts:
– The first part contains the information of the node
– The second part contains the address of the next node
(link /next pointer field) in the list.

Imran Ali Memon ( School of IT ) 18


1.2 Data Structure
Queue:
• Also called FIFO (first in first out) system
• Linear list in which deletions can take place only at
one end of the list, the Front of the list and
insertion can take place only at the other end Rear.

Rear Front

Imran Ali Memon ( School of IT ) 19


1.2 Data Structure
Stack:
• Also called LIFO: Last In, First Out system.
• Linear list in which insertion and deletion take place only
at one end called top.
• The most recently added items are at the top of the stack.
• The last element to be added is the first
to be removed

Imran Ali Memon ( School of IT ) 20


1.2 Data Structure
Tree:
• Data frequently contain a hierarchical relationship
between various elements.
• The data structure which reflects this relationship is
called a rooted tree graph or simply a tree.

Imran Ali Memon ( School of IT ) 21


1.2 Data Structure
Graph:
• Data sometimes contains a relationship between
pairs of elements which is not necessarily
hierarchical in nature.
• e.g. an airline flights only between the cities
connected by lines. This data structure is called
Graph.

Imran Ali Memon ( School of IT ) 22


Assignments
• Real applications of stack, queue, linked list, tree,
graph in computer system.

Imran Ali Memon ( School of IT ) 23


1.3 Data Structure Operations
• The data appearing in our data structures are
processed by means of certain operations.

• In fact, the particular data structure that one


chooses for a given situation depends largely in the
frequency with which specific operations are
performed.

Imran Ali Memon ( School of IT ) 24


1.3 Data Structure Operations
The following four operations play a major role:
• Traversing:
– Accessing each record/node exactly once so that certain items in the
record may be processed.
(This accessing and processing is sometimes called “visiting” the record.)
• 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.
• Inserting:
– Adding a new node/record to the structure.
• Deleting:
– Removing a node/record from the structure.

Imran Ali Memon ( School of IT ) 25


1.3 Data Structure Operations
• Sometimes, two or more of the operations may be used in a
given situation.
• For e.g, we may want to delete a data element from a data
structure, which may mean we first need to search for the
location of the record.
• The following two operations, which are used in special
situations, will also be considered :
– Sorting : Sorting is the process of arranging all data items in a
data structure in a particular order say for example, either in
ascending order or in descending order.
– Merging : Combining the records of two different sorted files into
a single sorted file.

Imran Ali Memon ( School of IT ) 26


1.3 Data Structure Operations
• EXAMPLES & REAL LIFE APPLICATIONS

• A company contains employee file in which each


record contain the following data for a given
employee :

• Name, Address, Telephone number, Employee


age, gender, employ number.

Imran Ali Memon ( School of IT ) 27


1.3 Data Structure Operations
1. Suppose the company wants to announce a meeting through a
mailing. Then one would traverse the file to obtain employee
name and address for each member.
2. Suppose one wants to obtain address for the given employee
name. Then one would search the file for the record containing
employee name.
3. Suppose a new person joins the company. Then one would insert
his or her record into the file.
4. Suppose an employee dies. Then one would delete his or her
record from the file.
5. Suppose an employee has moved and has a new address and
telephone number. Given the name of the member, one would
first need to search for the record in the file. Then one would
perform the “update”- i..e., change items in the record with the
new data.
Imran Ali Memon ( School of IT ) 28

You might also like