














Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
A comprehensive introduction to data structures and algorithms, covering fundamental concepts, data organization, and essential algorithms like linear search and binary search. It includes illustrative c programming examples to solidify understanding and practical application. Suitable for students and learners seeking a solid foundation in data structures and algorithms.
Typology: Study notes
1 / 22
This page cannot be seen from the preview
Don't miss anything!
An algorithm is a Step By Step process to solve a problem, where each step indicates an intermediate task. Algorithm contains finite number of steps that leads to the solution of the problem. For example when we are doing a mathematical calculation like addition, subtraction, multiplication or division. What do we do? We start with pen and paper and do the computation in one or two steps. If it is a bigger sum calculation of Simple interest or Profit and loss then we write some more steps. Algorithm is like that. But the language used in algorithm is somewhat similar to programming languages and somewhat with our human language(like English). An algorithm takes some value as input and produces output value in a finite time. Lets see an Example Adding two numbers Algorithm to add two numbers Step 1: Start Step 2: Declare the num1, num2, and sum variables. Step 3: Read the num1 and num2 values. Step 4: Add num1 and num2 and put the output in the sum variable Step 5: End Here this algorithm is the written interpretation of a program that is understandable to any person and it is understandable to any
person who can read English and it doesn’t matter whether he or she knows programming or not. If we write the above algorithm in c programming language then that will look like this #include<stdio.h> #include<conio.h> void main() { int num1 = 64; int num2 = 55; int sum = num1+num2; printf(“The sum is %d”,sum); getch(); } Thus it happens: Features Of Algorithm
Field: Field is a single unit of information that contains a single attribute value of an entity Record: Record is a collection of all field values of a given entity File: File is the collection of records of the entities in a given entity set Records may also be classified according to length. A file can have fixed-length records or variable-length records. In fixed-length records , all the records contain the same data items with the same amount of space assigned to each data item. In variable-length records file records may contain different lengths. Example: Student records have variable lengths, since different students take different numbers of courses. Variable-length records have a minimum and a maximum length. Abstract Data Type (ADT) Type: A type is a set of values of similar kind like integer, floating decimal, alphanumeric characters etc Data Type: Data Type is a type with a set of operations to alter the type. For Example: List is a datatype. Two traditional implementations of list data type are linked list and Arrays Image Of A List
Linked List Array Figure 1 Array Figure 2 There is a difference need to be made between the logical representation of data type and the physical representation of datatype in computer memory. For example, there are two traditional implementations for the list data type: the linked list and the array-based list. The list data type can therefore be implemented using a linked list or an array. But we don’t need to know how the list is implemented when we wish to use a list to help in a more complex design. For example, a list might be used to help implement a graph data structure. Abstract Datatype : An abstract datatype is a datatype that is represented with some attributes and functions. It is a datatype
Primitive data structure is the data structure that is atomic or indivisible. ex- int, real, float, Boolean etc NON PRIMITIVE DATA STRUCTURES Non Primitive data structures are composite data structures that can be divided into groups of homogeneous or heterogeneous data items. Example: Records, Arrays, Strings etc LINEAR DATA STRUCTURES
In Linear data structure data items are arranged in linear way. Example: Arrays NON LINEAR DATA STRUCTURES In Non Linear data structure the data items are not arranged in linear way. Example : tree HOMOGENEOUS DATA STRUCTURES In homogeneous data structure all the data items are of same data type. Example – Arrays – int arr[] = {5,4.3,2} Here all the data items are of same data type that is integer NON HOMOGENEOUS DATA STRUCTURE In Non homogeneous data structure all the data items are not of the same data type. Example- Records STATIC DATA STRUCTURE static data structures are those whose size, structure and memory location are fixed during compile time. DYNAMIC DATA STRUCTURE In Dynamic data structure the size and memory capacity of the data structure can be updated, increased or decreases during compile time.
loops for each operation. LIST: List is a collection of variable number of data items. List is the linear representation of array. An element of list must contain at least two fields, one for storing data or information and other for storing address of next element. STACK: Stack is an ordered collection of data items. In stack insertion and deletion occur at one end only and that is the top of the stack. Stack have the following features:
element base of stack remains same.
As said above the space complexity is one of the factor which accounts for the performance of the program. The space complexity can be measured using experimental method, which is done by running the program and then measuring the actual space occupied by the program during execution. But this is done very rarely. We estimate the space complexity of the program before running the program The reasons for estimating the space complexity before running the program even for the first time are: (1) We should know in advance, whether or not, sufficient memory is present in the computer. If this is not known and the program is executed directly, there is possibility that the program may consume more memory than the available during the execution of the program. This leads to insufficient memory error and the system may crash, leading to severe damages if that was a critical system. (2) In Multi user systems, we prefer, the programs of lesser size, because multiple copies of the program are run when multiple users access the system. Hence if the program occupies less space during execution, then more number of users can be accommodated. Space complexity is the sum of the following components: (i) Instruction space: The program which is written by the user is the source program. When this program is compiled, a compiled version of the program is generated. For executing the program an executable version of the program is generated. The space occupied by these three when the program is under execution, will account for the instruction space. The instruction space depends on the following factors: Compiler used – Some compiler generate optimized code which occupies less space.
Compiler options – Optimization options may be set in the compiler options. Target computer – The executable code produced by the compiler is dependent on the processor used. (ii) Data space: The space needed by the constants, simple variables, arrays, structures and other data structures will account for the data space. The Data space depends on the following factors:Structure size – It is the sum of the size of component variables of the structure. Array size – Total size of the array is the product of the size of the data type and the number of array locations. (iii) Environment stack space: The Environment stack space is used for saving information needed to resume execution of partially completed functions. That is whenever the control of the program is transferred from one function to another during a function call, then the values of the local variable of that function and return address are stored in the environment stack. This information is retrieved when the control comes back to the same function. The environment stack space depends on the following factors: Return address Values of all local variables and formal parameters. The Total space occupied by the program during the execution of the program is the sum of the fixed space and the variable space. (i) Fixed space - The space occupied by the instruction space, simple variables and constants. (ii) Variable space – The dynamically allocated space to the various data structures and the environment stack space varies according to the input from the user. Space complexity S(P) = c + Sp c -- Fixed space or constant space Sp -- Variable space We will be interested in estimating only the variable space because that is the one which varies according to the user input. Consider the following piece of code...
when n increases. Similarly, the running time of both operations will be nearly the same if n is significantly small. The time required by an algorithm falls under three types − Best Case − Minimum time required for program execution. Average Case − Average time required for program execution. Worst Case − Maximum time required for program execution. Asymptotic Notations Following are the commonly used asymptotic notations to calculate the running time complexity of an algorithm. Ο Notation Ω Notation θ Notation Big Oh Notation, Ο The notation Ο(n) is the formal way to express the upper bound of an algorithm's running time. It measures the worst case time complexity or the longest amount of time an algorithm can possibly take to complete. For example, for a function f(n)
Ο(f(n)) = { g(n) : there exists c > 0 and n0 such that f(n) ≤ c.g(n) for all n > n0. } Omega Notation, Ω The notation Ω(n) is the formal way to express the lower bound of an algorithm's running time. It measures the best case time complexity or the best amount of time an algorithm can possibly take to complete. For example, for a function f(n) Ω(f(n)) ≥ { g(n) : there exists c > 0 and n0 such that g(n) ≤ c.f(n) for all n > n0. } Theta Notation, θ The notation θ(n) is the formal way to express both the lower bound and the upper bound of an algorithm's running time. It is represented as follows −
When the program is bigger and complicated with loops occupy more space in memory but takes less time to be executed.
4. Lookup Table/Recalculation: Lookup table includes the entire table so it occupies more space but takes little time for computation. If it is recalculated as computer table entries are needed then computing time increases but less memory is needed. LINEAR SEARCH AND BINARY SEARCH TECHNIQUE AND THEIR COMPLEXITY ANALYSIS: Linear Search: Basic Concept
for (int i=0; i<size; i++) if (a[i] == key) return i; return -1; } int main() { int x[]={12,-3,78,67,6,50,19,10}, val; printf (”\nEnter number to search: ”); scanf (”%d”, &val); printf (”\nValue returned: %d \n”, linear_search (x,8,val); } Linear Search Properties: It searches the array for the number to be searched element by element.