0% found this document useful (0 votes)
3 views

Data Structures & Algorithms 01

Uploaded by

awaisbaryal25
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Data Structures & Algorithms 01

Uploaded by

awaisbaryal25
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

Data Structures & Algorithms

Lecture # 01
Mr. Muhammad Arshad
Introduction
• Data: data are simply values or set of values. A data items refers to a
single unit of values. Data items that are divided into sub items are
called group items. For example Employee name may be divided into
three sub items _ first name, middle name and last name.
• Entity: An entity is something that has certain attributes or properties
which may be assigned values. The values themselves may be either
numeric or non numeric.
Example:
Attribute Name Age Gender Number
Values John 34 Male 134-24553-3
Data Structure
• Data structures help us to organize the data in the computer, resulting
in more efficient programs. An efficient program executes faster and
helps minimize the usage of resources like memory, disk. Computers
are getting more powerful with the passage of time with the increase
in CPU speed in GHz, availability of faster network and the
maximization of disk space. Therefore people have started solving
more and more complex problems. As computer applications are
becoming complex, so there is need for more resources
Need for Data Structures
• Data structures organize data  more efficient programs.

• More powerful computers  more complex applications.

• More complex applications demand more calculations.


Organizing Data
• Any organization for a collection of records that can be searched,
processed in any order, or modified.

• The choice of data structure and algorithm can make the difference
between a program running in a few seconds or many days.
Efficiency
 A solution is said to be efficient if it solves the problem within its
resource constraints.
 Space
 Time

 The cost of a solution is the amount of resources that the solution


consumes.
Types of data structure
• Primitive- anything that can store data can be called as a data structure,
hence Integer, Float, Boolean, Char etc, all are data structures. They are
known as Primitive Data Structures.

• Composite: any data type (struct, array, string etc.) composed of


primitives or composite types.
• Abstract: data type that is defined by its behaviour (tuple, set, stack,
queue, graph etc.).
• Array
• Linked List
Non-primitive
• These are the Data Structures in which we perform all the major
operations like – sorting, merging and many more.
• Non-primitive data structures are the complex data structures we use in
programming. Mostly, all the non-primitive data structures are user-
defined data structures, though many languages provide in-built support
for these data structures, and, thus they are considered as the user-
defined data structure.
• Non-Primitive Data Structure is comprised of 3 categories, which are
Arrays, Files and Lists.
• List is also consist of 2 types: Linear List and Non-Linear List or we can
say Linear Data Structure and Non-Linear Data Structure.
Types of data structure
• Linear Data structure
• Array
• Stack
• Queues
Non Linear Data Structures
• Trees
• Binary tree
• Binary search tree
• Hash table
• Graph
• Heaps
Algorithms of data structure and application
• Algorithm is a step-by-step procedure, which defines a set of
instructions to be executed in a certain order to get the desired output.
Algorithms are generally created independent of underlying
languages, i.e. an algorithm can be implemented in more than one
programming language.
• From the data structure point of view, following are some important
categories of algorithms.
Algorithms of data structure and application
• Search − Algorithm to search an item in a data structure.
• Sort − Algorithm to sort items in a certain order.
• Insert − Algorithm to insert item in a data structure.
• Update − Algorithm to update an existing item in a data structure.
• Delete − Algorithm to delete an existing item from a data structure.
• The following computer problems can be solved using Data Structures
• Fibonacci number series
• Knapsack problem
Algorithms of data structure and application
• All pair shortest path by Floyd-Warshall
• Shortest path by Dijkstra.
• Project scheduling
Array
• An array is a group of consecutive memory location with same name
and data type. Simple variable is a single memory location with a
unique name and a type. But an array is a collection of different
adjacent memory locations. All these memory location have one
collective name and type .
Array
 Array declaration: int x[6];
 An array is collection of cells of the same type.
 The collection has the name ‘x’.
 The cells are numbered with consecutive
integers.
 To access a cell, use the array name and an
index:
x[0], x[1], x[2], x[3], x[4], x[5]
Implementation
• int main() {
• int arr[5],i;
• for(i=0;i<5;i++)
• {cout<<"enter values";
• cin>>arr[i];
•}
• cout<<"value of arry";
• for(i=0;i<5;i++)
• cout<<arr[i];
The LIST Data Structure
 The List is among the most generic of data structures.

 Real life:

a. shopping list,
b. groceries list,
c. list of people to invite to dinner
d. List of presents to get
Lists
 A list is collection of items that are all of the same type (grocery
items, integers, names)

 The items, or elements of the list, are stored in some particular order

 It is possible to insert new elements into various positions in the list


and remove any element of the list
Lists
 List is a set of elements in a linear order.
For example, data values a1, a2, a3, a4 can be arranged in a list:

(a3, a1, a2, a4)

In this list, a3, is the first element, a1 is the second element, and so on

 The order is important here; this is not just a random collection of


elements, it is an ordered collection
List Operations
Useful operations
• createList(): create a new list (presumably empty)
• copy(): set one list to be a copy of another
• clear(); clear a list (remove all elments)
• insert(X, ?): Insert element X at a particular position
in the list
• remove(?): Remove element at some position in
the list
• get(?): Get element at a given position
• update(X, ?): replace the element at a given position
• with X
• find(X): determine if the element X is in the list
• length(): return the length of the list.
List Operations
 We need to decide what is meant by “particular position”; we have
used “?” for this.

 There are two possibilities:

1. Use the actual index of element: insert after element 3, get element number
6. This approach is taken by arrays
2. Use a “current” marker or pointer to refer to a particular position in the list.
List Operations
 If we use the “current” marker, the following four methods would be
useful:

 start(): moves to “current” pointer to the very first element.


 tail(): moves to “current” pointer to the very last element.
 next(): move the current position forward one element
 back(): move the current position backward one element

You might also like