Bca Unit 1 Data Structures
Bca Unit 1 Data Structures
1
Data and Data Item:
Data are simply collection of facts. Data represents values or set of values.
A data item refers to a single unit of value. Data items that can be divided into sub
items are called group items and those that can’t be divided are called elementary
items.
In the above example ( ID, Age, Gender, First, Middle, Last, Street, Area ) are
elementary data items, whereas (Name, Address ) are group data items.
Q) Explain about Data Representation in computers?
Data Representation:
It refers to the form in which the data is stored, processed and transmitted.
Data can be represented using methods like Integer, Real numbers, Character
and Strings.
The Basic unit of data representation is a Bit i.e. either 0 or 1. The combination
of bit values can be used to represent the data.
Eight bits together form 1 byte which represents a character. One or more
characters form a string.
2
INTEGER REPRESENTATION:
Integer is a basic type commonly used to store negative as well as positive nos.
The positive no’s are represented by Binary Number System. In this each bit
position represents the power of 2. The right most bit represent 2 0 which is 1 ,
21 which is 2 , next 22 which is 4 and so on.
For Example
The integer 38 is represented as 00100110
The Negative no’s are represented by methods like 1’s complement and
2’s Complement.
3
CHARACTER REPRESENTATION:
Character can be a alphabet, digit or special symbol. To represent data like
name of employee we use group of characters.
There are different codes to store data in character form such as BCD,
EBCDIC, ASCII, UNICODE e.tc.
BCD code uses 4bits to store data, EBCDIC code uses 8bits to store data,
ASCII Code uses 7 bits to store data, EASCII code uses 8bits to store the data
and UNICODE uses 16 bits to store the data.
For example, if 8 bits are used to represent a character, then 2 8=256 characters can
be represented as bit patterns.
4
For Example:
Java byte, short, int, long, char float, double, boolean, etc.
Character Datatype:
In this type, the information is represented in the form of characters. A
character can be Alphabet, Digit or Special Symbol.
Any symbol from A-Z, a-z , 0-9, Special symbols like #,@,$ etc is a character
The range of character type is 0 to 2n-1
Where n indicates no of bits
Eg:
In C, each character occupies 1 Byte (8bits)
5
So it has 28 =256 characters.
Abstract view:
Store set of integer type elements
Read elements by index
Modify the elements by Index
Perform sorting & searching.
6
The Examples of ADT are Arrays, structures, Stacks, Queues, Trees, Graphs
e.tc
Sometimes, these Abstract data types are also known as user defined data
types.
For Example, if the user wants to process a Date type of data in the form of
dd /mm /yy. Such type of data type is not available in any built-in types and
hence we derive an abstract data type named as date.
struct Date
{
int dd;
int mm;
int yy;
}
ADT Properties:
It can be easily revealed that triplet {D, F, A} is nothing but an abstract data type.
For example, we know integer data type “int” in c language has following
properties
8
It tells how data can be stored and accessed.
Provide a means to manage huge amount of data efficiently.
Operations on Data Structures:
3. Traversing: The process of visiting each and every data element is known as
“Traversing operation”.
4. Sorting: The process of arranging the given data elements either in ascending
or descending order is known as “sorting operation”.
7. Merging: The process of combining two data structures into a single data
structure is known as “merging operation”.
9
Primitive data structures:
These are Basic data structures that directly operate using the machine
instructions.
These Data Structures takes only one value at a time.
These are more complicated Data Structures derived from the primitive data
structures.
They emphasize on grouping same or different data items with relationship
between them.
These data structures are divided into two categories. They are
Linear Data Structures
Non-Linear Data Structures.
Linear data structure is a Data Structure in which the data items or elements are
organised Linearly or Sequentially.
It is a list which shows the relationship of adjacency between the elements.
10
All the data items(elements) in a linear data structure can be traversed in single
run.
It includes arrays, linked list, stack and queue data structures.
2. Linked list: Linked list is a collection of Nodes. Node consists of two parts:
data and Link. Data presents elements and Link represents the address of next
node.
11
4. Queue: Queue is a linear list of elements in which the elements are added at one
end called rear and deleted from another end called front. It follows first-in-
First-out (FIFO) organisation.
It is a data structure where the data items or elements are not organized
sequentially is called non-linear data structure.
In this, the data elements of the non-linear data structure could be connected to
more than one element
All the data elements in non-linear data structure cannot be traversed in single
run.
Examples of non-linear data structures are:
Trees
Graphs
Tree:
A tree is collection of nodes where these nodes are arranged hierarchically and form
a parent child relationship.
Graph:
12
Q) Differentiate between Linear & Non-Linear Data structure? (5m)***
S.NO Linear Data Structure Non-linear Data Structure
Its examples are: array, stack, While its examples are: trees and
6. queue, linked list, etc. graphs.
13
DATA STRUCTURES:
Data structure is a way of storing & organising the data in computer memory.
It refers to collection of variables that are connected in some specific manner.
Data structure can be defined as structural relationship present within the
dataset.
14
Time Complexity is the amount of Computational or Execution time required to run
an algorithm to complete.
Approaches in Designing Algorithms: (******vimp*******)
A complex program or system is divided into small units called Modules.
The modularity enhances Design clarity, Debugging, Testing, Documentation
and Maintenance.
Algorithm design involves two approaches. They are
TOP-DOWN APPROACH
BOTTOM-UP APPROACH.
TOP-DOWN APPROACH:
It is an approach of decomposing a complex module/component into sub
modules/components.
In this the Higher-level component is decomposed into lower-level
components.
Top-Down design method takes the form of step-wise Refinement.
In this we start with the top module and incrementally add sub modules in next
levels of refinement.
In each step, design is refined into most concrete level.
This approach is mainly used by structured oriented programming languages
like C, COBOL, PASCAL etc.
BOTTOM-UP APPROACH:
It is an approach of designing the basic or primitive modules and proceeds to
higher level components.
In this the lower-level components are composed to form the higher-level
component.
Bottom-up design method works with levels of abstraction.
Starting from bottom layer the operations that provide layer abstraction is
implemented and these are used to design still higher level of abstraction.
15
This approach is mainly used by object oriented programming languages like
c++, java , python etc.
16
Breakdown of problem takes
10 place Integration of Modules takes place.
Q) Explain about the Time Complexity and Space Complexity? (OR) Explain
about the Analysis of Algorithms (***vimp)
17
The total frequency count of the algorithms - A, B and C given by 1, (2n+1) and
(2n2+2n+1) respectively are expressed as O(1), O(n) and O(n2).
There are different time complexities analysed by the algorithm. They are
Best Case Time Complexity
worst Case Time Complexity
Average Case Time Complexity.
Best Case Time Complexity:
It is the measure of minimum time required by an algorithm to execute for an
input of size n.
worst Case Time Complexity:
It is the measure of Maximum time required by an algorithm to execute for an
input of size n.
Average Case Time Complexity:
It is the time required by an algorithm to execute for an input of size n.
18
If an algorithm performs f(n) basic operations when size of input is n, then the total
running time will be c.f(n).Where c is constant
Space Complexity
Space Complexity is the amount of Memory space required to run an algorithm to
complete.
It includes
a) Fixed Space: This includes the space for simple variables, fixed size
structured variable and constants
b) Variable Space: This includes the space needed by structured variables whose
size depends on particular instance of variables. It also includes the additional
space required when the function uses recursion.
S(P)=C + SP
Where S(P) is the Space Complexity
C is the fixed Space
SP is the Variable space
19
Q) Write Short notes on Big ‘O’ Notation? (*****VVIMP*****)
Big O notation is an asymptotic notation that measures the performance or
analysis of an algorithm.
algorithms.
O(1) - constant.
O(n) - linear
O(n2) - quadratic.
O(n3) - cubic.
O(2n) – Exponential
O(1) < O(log n)< O(n)<O(n log n)< O(n2) < O(n3)< O(2n)
function like 1,n,n2,n3,nlogn etc then f(n) = O(g(n)) if there exists constant C
time.
f(n)<=c.g(n)
Let f(n) = 2n+3 and g(n) = n.
Then f(n)<=c.g(n)
2n+3 <= c.n
Let's assume c=5
2n+3<=5*n
21
F(n)=Og(n)=O(n) for n>=1
1. Polynomial Functions
2. Logarithmic Functions
23
3. Exponential Functions
4. Factorial Functions
The factorial function is denoted n!n! and represents the product of all positive
integers up to n (i.e., n!=n×(n−1)×…× 1).
5. Modular Arithmetic
24
7. Summation (∑) and Product (π) Notations
8. Recurrence Relations
Recurrence relations are equations that express the overall time complexity of
recursive algorithms in terms of smaller subproblems.
• Looping Statements
Decision control statements execute the statements by the checking condition. The
decision making involves branching, which means jumping from one step to step in a
program.
❖ Simple if statement
❖ if..else statement
❖ if..else if statement
❖ Nested if
❖ Switch statement
25
✓ Simple if:
syntax:
if(condition)
statement/block
Eg:
if (age>=18)
✓ if..else:
In this statement we have two blocks or statements, if the condition is true then the
statement1/block1 is executed otherwise the statement2/block2 will be executed.
Syntax:
if (condition)
Statement1/block;
Else
Statement2/block;
Eg:
if (n >0)
printf(“%d is positive”,n);
else
printf(“%d is negative”,n);
✓ if..else if:
Syntax: if (condition1)
26
statement1/block1;
else if (condition 2)
statement2/block2;
..............
else if (condition N)
statement/blockn;
eg:
if (avg>=60)
printf(“first class”);
else if(avg>=50)
printf(“second class”);
else if(avg>=35)
printf(“third class”);
✓ Nested if:
Syntax:
if (condition1)
if (condition2)
Statement1/block1 ;
else
Statement2/block2;
if (condition3)
Statement3/block3;
else statement4/block
27
eg :
if (a>b)
if( a>c)
printf(“a is big”);
else
if(b>c)
printf(“b is big”);
else
printf(“c is big”);
✓ switch:
Syntax:
switch(expression)
break;
break;
...................................................
.................................................
break;
28
Eg: switch(month)
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 4:
case 6:
case 9:
default:printf(“invalid month”);
Looping is the process of repetition of instructions. Every loop contains initial value,
condition
and increment/decrement.
❖ while loop
❖ do-while loop
❖ for loop
29
while loop:
❖ It enter’s into loop minimum zero times if the condition is not satisfied.
Syntax:
initial value;
while (condition)
eg: i=1;
while(i<=10)
Statements; printf(“%d”,i);
Increment/decrement;
i++;
do-While loop:
condition is satisfied.
❖ It enter’s into loop minimum 1 time even if the condition is not satisfied.
Syntax:
initial value;
do
statements;
30
Increment/decrement;
}while(condition);
eg:
i=1;
do
printf(“%d”,i);
i++;
}while(i<=10);
For loop is an entry controlled loop statement that is used to repeat the loop for fixed
no of times.
It checks the condition at first and then enters into the loop.
Syntax:
body;
The initial value specifies the starting value, condition specifies the no of times the
loop to be repeated and increment/decrement specifies the next iteration value.
Eg: for(i=0;i<=100;i++)
printf(“%d”,i);
31
These sub-algorithms can simplify complex processes, improve readability,
and make code more modular.
They are commonly used for fundamental operations within data structures,
breaking down complex tasks, and optimizing efficiency.
1. Search Algorithms
Linear Search: Checks each element in a list sequentially until the desired element is
found or the list ends. Often used for unsorted data.
Complexity: O(n)
Binary Search: Finds an element in a sorted list by repeatedly dividing the search
interval in half, which reduces the search time significantly.
Complexity: O(logn)
2. Sorting Algorithms
Bubble Sort: A simple sorting algorithm that repeatedly swaps adjacent elements if
they are in the wrong order.
Complexity:
O(n2)
Merge Sort: A divide-and-conquer sorting algorithm that splits the list into smaller
sublists, sorts them, and then merges them.
Complexity: O(nlogn)
Quick Sort: A divide-and-conquer algorithm that selects a "pivot" and partitions the
array around the pivot so that elements on the left are less than the pivot, and
elements on the right are greater.
worst-case O(n2)
3. Insertion Algorithms
Insertion sub-algorithms are often used for adding elements to data structures such as
arrays, linked lists, trees, and hash tables.
32
Array Insertion: Adds an element at a specific index in an array by shifting
elements.
Complexity: O(n)
Binary Search Tree Insertion: Inserts an element in a binary search tree while
maintaining its properties.
worst-case O(n)
4. Deletion Algorithms
Deletion algorithms remove elements from data structures, such as arrays, linked
lists, and trees, often while maintaining structural properties.
Complexity: O(n)
Binary Search Tree Deletion: Deletes a node from a binary search tree while
maintaining its properties.
5. Traversal Algorithms
Complexity: O(n)
33
Complexity: O(n)
Inorder: Left, Root, Right (used in binary search trees for sorted order)
If f(n) is the computing time of an algorithm and g(n) represents a standard function
like 1,n,n2,n3,nlogn etc then f(n) = O(g(n)) if there exists constant C and positive
integer n0 such that: f(n)≤c.g(n) for all n≥no .
Big Omega notation describes a lower bound on the time complexity of an algorithm,
showing the best-case scenario. It represents the minimum time the algorithm will
take.
Use: It helps in understanding the minimum time the algorithm requires, which can
be useful for determining the efficiency in the most favorable conditions.
Big Theta notation provides a tight bound on the time complexity, describing both
the upper and lower bounds. It indicates that the algorithm’s running time grows
asymptotically as f(n)
34
Use: This notation is helpful for analyzing algorithms with predictable growth rates.
35