Introduction to Data Structures, Algorithms, and Arrays
Introduction to Data Structures and Algorithms
Definition of Data: Data refers to raw facts, figures, symbols, or values collected for processing. Data by itself may not provide meaningful information; however, after processing, data becomes information.
Examples of Data:
Student name
Roll number
Marks
Mobile number
Salary details
Data Structure Definition:
A method of organizing and storing data in a computer so that it can be accessed and processed efficiently.
Alternatively, it is the logical arrangement of data elements along with the relationship between them.
Need for Data Structures:
Efficient storage of data.
Fast searching and retrieval.
Easy insertion and deletion of data.
Better memory utilization.
Faster processing of information.
Organizing data systematically.
Reducing program complexity.
Advantages of Data Structures:
Efficient Storage: Helps in storing large amounts of data in an organized manner.
Fast Access: Data can be accessed and retrieved quickly and easily.
Easy Processing: Operations like insertion, deletion, searching, and sorting become easier.
Better Memory Utilization: Helps in proper utilization of computer memory and reduces memory wastage.
Faster Execution: Programs execute faster and more efficiently using suitable data structures.
Classification of Data Structures
Primitive Data Structures: These are basic and fundamental data types directly supported by the programming language and computer hardware. They store only a single value.
INTEGER (int): Used to store whole numbers without decimal points. Example:
int age = 20; printf("%d", age);.CHARACTER (char): Used to store a single character. Example:
char grade = 'A'; printf("%c", grade);.FLOAT (float): Used to store decimal numbers with single precision. Example:
float mark = 89.5; printf("%f", mark);.DOUBLE (double): Used to store large decimal numbers with double precision. Example:
double salary = 45678.9876; printf("%lf", salary);.
Non-Primitive Data Structures: Complex data structures derived from primitive data types. They are used to store multiple values and relationships between data elements.
Linear Data Structures: Elements are arranged sequentially one after another.
Array: A collection of similar type of elements stored in consecutive memory locations. Example:
int a[5] = {10, 20, 30, 40, 50};.Linked List: A collection of nodes where each node contains data and the address of the next node. Nodes are connected using pointers.
Stack: A linear structure where insertion and deletion occur at only one end called the TOP. It follows LIFO ().
Queue: A linear structure where insertion is done at the REAR and deletion is done at the FRONT. It follows FIFO ().
Non-Linear Data Structures: Elements are not arranged sequentially; one element can be connected to multiple elements to represent hierarchical or network relationships.
Tree: A hierarchical structure consisting of nodes connected by edges. The topmost node is the ROOT node.
Graph: A structure consisting of Vertices (Nodes) and Edges (Connections).
Algorithm Analysis and Mathematical Background
Algorithm: A finite set of step-by-step instructions or a systematic procedure used to solve a problem or perform a specific task in a finite number of steps.
Mathematical Background of Analysis: Used to measure and compare efficiency based on execution time, memory usage, and growth rate.
Constant Function: . Execution time remains the same for all inputs. Complexity: .
Linear Function: . Execution time increases directly with input size. Complexity: .
Quadratic Function: . Execution time increases as the square of input size (e.g., nested loops). Complexity: .
Logarithmic Function: . Execution time increases slowly even for large inputs. Complexity: .
Exponential Function: . Execution time increases very rapidly. Complexity: .
Order of Growth: O(1) < O(\log(n)) < O(n) < O(n^2) < O(2^n). Speed ratings: (Very Fast), (Fast), (Moderate), (Slow), (Very Slow).
Performance Analysis: Time and Space Complexity
Performance Analysis: Analyzing the efficiency of a program before execution. Actual execution time is performance measurement.
Space Complexity: The amount of memory space required by an algorithm to execute completely. Components include:
Fixed Part: Independent of input size. Includes program instructions, constants, simple variables, and fixed-size variables.
Variable Part: Depends on the problem instance. Includes dynamic variables, referenced variables, and recursion stack space.
Representation: , where is the fixed part and is the variable part.
Constant Space Example: An algorithm returning a math expression like
a + b + b * c + (a + b - c) / (a + b) + 4.0has no recursion or dynamic allocation; thus and it uses constant space.
Time Complexity: The amount of time required by an algorithm to execute completely. Compile time is ignored; only run time matters.
Representation:
Step Count Method: Assumes each meaningful operation takes one unit of time. .
Frequency Count Method: Counts how many times each statement is executed.
Factors Affecting Runtime: Number of comparisons, number of data exchanges, recursive calls, program size, and input data size.
Comparison:
Time Complexity: Measures execution speed; depends on number of operations.
Space Complexity: Measures memory usage; depends on memory allocation.
Computational and Asymptotic Complexity Cases
Best Case Analysis: Minimum time required. Occurs when the desired element is at the first position or minimum operations are performed. Represents the lower bound. Example: Linear search finding the item at index 0.
Worst Case Analysis: Maximum time required. Occurs when the element is at the last position or not present. Represents the upper bound and is the most common metric. Example: Linear search checking all elements.
Average Case Analysis: Average execution time for all possible inputs based on probability. Usually lies between best and worst case.
Asymptotic Notations:
Big O Notation (): Represents the upper bound (worst-case). if there exist constants and such that for all . Example: for , so .
Omega Notation (): Represents the lower bound (best-case). if for all . Example: , so .
Theta Notation (): Represents the exact behavior (both upper and lower bound). for all .
Arrays: Implementation and Memory Allocation
Arrays: A collection of homogeneous (same data type) elements stored in contiguous/consecutive memory locations. Access is via an index or subscript (starting at 0 in C).
One-Dimensional Array Declaration:
data_type array_name[size];. Example:int num[5];.Size Calculation: . For
int num[5], , so .Memory Requirement: . Example:
float val[10];().Initialization:
int ex[10] = {12, 23, 9, 17, 16, 49};(Remaining 4 elements become 0).char word[10] = {'h', 'e', 'l', 'l', 'o', '\0'};orchar name[20] = "John Doe";.
Address Calculation Formula (1D Array):
: Base Address (address of the first element).
: Size of one element in bytes.
: Index of the target element.
: Lower Bound of the array.
Problem Example: Base address = , , element size = . Find address of . Result: .
Advantages/Limitations:
Advantages: Fast access via index, efficient memory utilization for known sizes, simple implementation.
Limitations: Fixed size, homogeneous only, difficult insertions/deletions (requires shifting).
Array Operations and Algorithms
Traversal: Visiting each element exactly once. Used for displaying elements or performing calculations.
Insertion: Adding an element at a specified position. Elements after the position must be shifted right.
Deletion: Removing an element. Subsequent elements must be shifted left to fill the gap.
Addition/Subtraction/Multiplication: Performed on corresponding elements of two arrays. Algorithm: .
Applications:
Sparse Matrix Representation: A matrix where most elements are zero. Stored by only keeping non-zero elements to save space. Types: Lower Triangular (zeros above diagonal), Upper Triangular (zeros below diagonal), and Tridiagonal (non-zeros only on/adjacent to main diagonal).
Polynomial Representation: Storing coefficients in an array where the index represents the exponent. Example: stores
[7, 2, 4, 5].Polynomial Addition: Comparing exponents and adding coefficients of the same degree.
Searching Techniques
Linear Search (Sequential Search):
Checks elements one by one from the start.
Works on sorted and unsorted arrays.
Best Case: (found at index 0).
Worst Case: (found at last index or not present).
Average Case: .
Binary Search:
Used only for sorted arrays. Follows Divide and Conquer.
Repeatedly divides the search space in half using
mid = (beg + end) / 2.Best Case: .
Worst/Average Case: .
Space Complexity: .
Sorting Techniques
Selection Sort:
Repeatedly finds the minimum element from the unsorted part and swaps it with the first unsorted element.
Time Complexity: for all cases (Best, Average, Worst).
Space Complexity: .
Performs fewer swaps than bubble sort.
Bubble Sort:
Compares adjacent elements and swaps them if they are in the wrong order. Larger elements "bubble" to the end.
Optimized Bubble Sort: Uses a
flagto stop if no swaps occur in a pass.Worst/Average Case: .
Best Case (Optimized): .
Insertion Sort:
Virtually splits the array into sorted and unsorted parts. Inserts the next unsorted element into its proper place in the sorted part.
Worst/Average Case: .
Best Case: (already sorted, only comparisons).
Efficient for small/partially sorted data.
Quick Sort:
Divide and Conquer algorithm. Selects a pivot and partitions the array into elements smaller than the pivot (left) and larger than the pivot (right).
Partitioning: Rearranges elements around the pivot; the pivot reaches its final sorted position.
Best/Average Case: .
Worst Case: (occurs if the array is already sorted or reverse-sorted).
Space Complexity: due to recursion stack.