Data Structure... Lecture 1
Data Structure... Lecture 1
Basic Concepts
Information
Processed data
Non-Linear DS
In which elements are stored in non-sequential way E.g. Trees, Graphs
Why DS needed???
Data structures organize data more efficient programs More powerful computers more complex applications.
Operations on a DS
Inserting
Add new data to existing DS
Deleting
Remove data from the existing DS
Searching
Finding the item from the DS
Operations on a DS
Traversing
Accessing the data item in the DS
Sorting
Giving some ordering look to the data
Merging
Merging two data structures (e.g. Lists or arrays into one single data structure)
How to Selecting a DS
Select a data structure as follows:
Analyze the problem to determine the resource constraints a solution must meet. Determine the basic operations that must be supported. Quantify the resource constraints for each operation. Select the data structure that best meets these requirements.
Selecting DS
The choice of data structure and algorithm can make the difference between a program running in a few seconds or many days.
Selecting DS
Some Questions to Ask:
Are all data inserted into the data structure at the beginning, or are insertions intersperse with other operations?
Can data be deleted?
Are all data processed in some well-defined order, or is random access allowed?
Simple Vs Complex DS
Simple DS
Simple data structures can hold one or more pieces of data of a single type E.g. Variable, Array
Complex DS
Composite data structures can hold several pieces of data of one or more types. Structure etc
What is Algorithm?
Step-by-step procedure to solve a specific problem is called algorithm
Sample Algorithm1
Algorithm Sum [This algorithm is used to read two Numbers from users, sum two nos and show their result on screen] Step No.1 [Read Two Values] No1 Read Value No2 Read Value Step No.2 [Compute The Sum] Sum No1 + No2 Step no.3 [Show the result on the screen] Print Sum= Sum Step No.4 [Finish] Exit
Sample Algorithm2
Algorithm Max[This algorithm is used to find the Max of two nos]
Step No.1 [Read Two Values] No1 Read Value No2 Read Value
Step No.2 [Checnk weather No1 or No2 is greater] if No1 > No2 then Max No1 Else Max No2 End If Step no.3 [Show the Max on the screen] Print Max= Max Step No.4 [Finish] Exit
Practice Algorithm
Read three nos from the user and find the maximum of three nos Read two nos from the user and then print their product on the screen
Arrays
Stacks
Queues
Linked Lists
Arrays
Stacks
Queues
Linked Lists
Arrays
Linear DS Simplest type of DS
An array is collection of cells of the same type
Elements referenced by index Elements stored in successive memory locations
Arrays
Each array is given a name Elements in the array are accessed with reference to their position Each element in array has distinct position, which is termed as its Index
Arrays
An element in the array is referred by the array name and its position
Array-Name [element-index]
Arrays
Array elements are denoted by
A1, A2, A3, . . . . An Or A(1), A(2), A(3), . . . . A(n) Or A[1], A[2], A[3], . . . . . , A[n]
Array Layout
Array cells are contiguous in computer memory The memory can be thought of as an array E.g. x[6]
Arrays Length
The maximum no of elements that an array can hold is called the arrays size Arrays size can be found using the following formula
Length(array_name) = UB-LB+1 UB = Upper bound LB = Lower Bound
Arrays Length
UB = 5 LB = 0 Length (X) = UB-LB+1 = 5 -0 + 1 =6
Quiz??
How many elements will be in an array named Y, when LB= 0 and UB = 9 If the UB of an array P is 15, then how many elements can P hold
Array Representation
2000 2004 2008 2012 2016 2020
123 P[0]
456 P[1]
88 P[2]
996 P[3]
55 P[4]
443 P[5]
Array Representation.
Starting Address of the array is called its base address E.g. the base address of P is 2000 As the base address represents the starting memory address of the first element of an array, so we can easily compute the memory address of any other element given its location index
Array Representation
We use the following formula to compute the memory address of any element in the array Loc (X[Index]) = Base + Size (Index - 1)
Array Representation
2000 2004 2008 2012 2016 2020
123
P[0]
456
P[1]
88
P[2]
996
P[3]
55
P[4]
443
P[5]
Array Representation
We know that Loc (X[Index]) = Base + Size (Index - 1) Index = 3 Base = 2000 Size = 4 So, Loc (P[3]) = 2000 + 4 (3-1) = 2000 + 8 = 2008
Operations on Array DS
Traversing Inserting Deleting Searching
Sorting
Algorithm
Algorithm ArraySum[This algorithm is used to find the Sum of an Array P] Step No.1 [Read Values into Array P] For I = 1 To 10 P[I] Read (Value) Loop Step No.2 [Compute the Array Sum] For I = 1 To 10 Sum Sum + P [I] Loop Step no.3 [Show the Sum on the screen] Print Sum= Sum Step No.4 [Finish] Exit
Assignment Rules
Hand written Make sure to submit on or before the deadline Deadline for the first Assignment: October 19, Friday