Data Structure - UNIT-1
Data Structure - UNIT-1
ALGORITHM
UNIT-1
1.1 INTRODUCTION-BASIC TERMINOLOGY
Data Structure is a way of collecting and organising data in such a way that we can perform
operations on these data in an effective way.
Data : The term ‘DATA’ simply refers to a value or a set of values. These values may present anything
about something, like it may be roll no of a student, marks, name of an employee, address of person
etc.
Data item : A data item refers to a single unit of value. For eg. roll no of a student, marks, name of an
employee, address of person etc. are data items. Data items that can be divided into sub items are
called group items (Eg. Address, date, name), where as those who can not be divided in to sub items
are called elementary items (Eg. Roll no, marks, city, pin code etc.).
Entity with similar attributes ( e.g all employees of an organization) form an entity set
Basic Terminology of Data Organization(Cont..):
Information: processed data, Data with given attribute
Field : It is a single elementary unit of information representing an attribute of an entity
Record is the collection of field values of a given entity
File is the collection of records of the entities in a given entity set
1.2 BASIC TYPES OF DATA STRUCTURES:
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.
Then we also have some complex Data Structures, which are used to store large and connected data. Some
example of Abstract Data Structure are :
• Array
• Linked List
• Stack
• Queue
• Tree
• Graph
Classification of Data Structure:
Primitive data structures are the fundamental data types which are
supported by a programming language. Some basic data types are
integer, real, character, and boolean. The terms ‘data type’, ‘basic
data type’, and ‘primitive data type’ are often used interchangeably.
(1) Traversing: Accessing each record exactly once so that certain items in the record may be processed.
(2) Searching: Finding the location of a particular record with a given key value, or finding the location of
all records which satisfy one or more conditions.
(3) Inserting: Adding a new record to the structure.
(4) Deleting: Removing the record from the structure.
(5) Sorting: Managing the data or record in some logical order (Ascending or descending order).
(6) Merging: Combining the record in two different sorted files into a single sorted file.
Worst-case running time: This denotes the behaviour of an algorithm with respect to the worst-
possible case of the input instance. The worst-case running time of an algorithm is an upper bound on
the running time for any input.
Average-case running time: The average-case running time of an algorithm is an estimate of the
running time for an ‘average’ input.
Best-case running time: The term ‘best-case performance’ is used to analyse an algorithm under
optimal conditions. For example, the best case for a simple linear search on an array occurs when the
desired element is the first in the list.
Amortized running time: Amortized running time refers to the time required to perform a sequence of
(related) operations averaged over all the operations performed.
1.6 TIME–SPACE TRADE-OFF
The best algorithm to solve a particular problem at hand is no doubt the one that requires less
memory space and takes less time to complete its execution. But practically, designing such an
ideal algorithm is not a trivial task.
There can be more than one algorithm to solve a particular problem. One may require less
memory space, while the other may require less CPU time to execute.
Thus, it is not uncommon to sacrifice one thing for the other. Hence, there exists a time–space
trade-off among algorithms
1.7 Asymptotic Notations
The main idea of asymptotic analysis is to have a
measure of efficiency of algorithms that doesn’t
depend on machine specific constants, and doesn’t
require algorithms to be implemented and time
taken by programs to be compared. Asymptotic
notations are mathematical tools to represent time
complexity of algorithms for asymptotic analysis.
The following 3 asymptotic notations are mostly
used to represent time complexity of algorithms.
1) Θ Notation:
The theta notation bounds a function from above
and below, so it defines exact asymptotic behavior.
A simple way to get Theta notation of an expression
is to drop low order terms and ignore leading
constants.
Asymptotic Notations (Cont..)
2. Big O Notation:
The Big O notation defines an upper bound of an
algorithm, it bounds a function only from above. For
example, consider the case of Insertion Sort. It takes
linear time in best case and quadratic time in worst
case.
Asymptotic Notations (Cont..)
3) Ω Notation:
Just as Big O notation provides an asymptotic upper bound on a function, Ω notation provides an
asymptotic lower bound. Ω Notation can be useful when we have lower bound on time
complexity of an algorithm.
1.8 MATHEMATICAL FUNCTIONS:
If x is a real number, then it means that x lies between two integers which are called the
floor and ceiling of x. i.e.
|_x_| is called the floor of x. It is the greatest integer that is not greater than x.
| x | is called the ceiling of x. It is the smallest integer that is not less than x.
k (mod M)
25(mod 7) = 4
25(mod 5) = 0
MATHEMATICAL FUNCTIONS (Cont..):
n
? aj
j=1
5. Factorial Function:
n! denotes the product of the positive integers from 1 to n. n! is read as ‘n factorial’, i.e.
n! = 1 * 2 * 3 * ….. * (n-2) * (n-1) * n
E.g.
4! = 1 * 2 * 3 * 4 = 24
6. Permutations:
Let we have a set of n elements. A permutation of this set means the arrangement of the elements of the set in some order.
E.g.
Suppose the set contains a, b and c. The various permutations of these elements can be: abc, acb, bac, bca, cab, cba.
If there are n elements in the set then there will be n! permutations of those elements. It means if the set has 3 elements
then there will be 3! = 1 * 2 * 3 = 6 permutations of the elements.
Exponent means how many times a number is multiplied by itself. If m is a positive integer, then:
am = a * a * a * ….. * a (m times)
and
a-m = 1 / am
E.g.
24 = 2 * 2 * 2 * 2 = 16
1.9 SORTING - BUBBLE SORT:
Bubble sort is a very simple method that sorts the array elements by repeatedly moving the largest
element to the highest index position of the array segment (in case of arranging elements in
ascending order). In bubble sorting, consecutive adjacent pairs of elements in the array are
compared with each other. If the element at the lower index is greater than the element at the
higher index, the two elements are interchanged so that the element is placed before the bigger one.
This process will continue till the list of unsorted elements exhausts. The pseudocode for Bubble sort
is given below:
Searching means to find whether a particular value is present in an array or not. If the value is present
in the array, then searching is said to be successful and the searching process gives the location of that
value in the array. However, if the value is not present in the array, the searching process displays an
appropriate message and in this case searching is said to be unsuccessful. There are two popular
methods for searching the array elements: linear search and binary search.
LINEAR SEARCH:
Linear Search Linear search, also called as sequential search, is a very simple method used for
searching an array for a particular value. It works by comparing the value to be searched with every
element of the array one by one in a sequence until a match is found. Linear search is mostly used to
search an unordered list of elements (array in which data elements are not sorted).
For example, if an array A[] is declared and initialized as, int A[] = {10, 8, 2, 7, 3, 4, 9, 1, 6, 5}; and the
value to be searched is VAL = 7, then searching means to find whether the value ‘7’ is present in the
array or not. If yes, then it returns the position of its occurrence. Here, POS = 3 (index starting from 0).
ALGORITHM FOR LINEAR SEARCH:
Binary search is a searching algorithm that works efficiently with a sorted list.
Let us consider how this mechanism is applied to search for a value in a sorted array. Consider an array
A[] that is declared and initialized as int A[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};and the value to be
searched is VAL = 9.
Now, VAL = 9 and A[MID] = A[5] = 5A[5] is less than VAL, therefore, we now search for the value in the
second half of the array.
So, again we change the values of BEG and MID. Now, BEG = MID + 1 = 9, END = 10, MID = (9 + 10)/2 =
9Now, VAL = 9 and A[MID] = 9.
BINARY SEARCH(Cont..)
ALGORITHM FOR BINARY SEARCH
The concept of array is very much bound to the concept of pointer. For example, if we have an array
declared as, int arr[] = {1, 2, 3, 4, 5}; then in memory it would be stored as shown below
Here, ptr is made to point to the first element of the array. Execute the code given below and observe the
output which will make the concept clear to you.
main()
{
int arr[]={1,2,3,4,5};
printf("\n Address of array = %p %p %p", arr, &arr[0], &arr);
}
Similarly, writing ptr=&arr[2] makes ptr to point to the third element of the array that has index 2.
1D ARRAY WITH POINTERS(cont..):
1.14 2D ARRAY WITH POINTERS:
Initialization of Structure:
1.16 ACCESSING THE MEMBERS OF A STRUCTURE
1.17 ARRAY OF STRUCTURES:
REFERENCE: