Ds Unit-1
Ds Unit-1
Data Structure:
A data structure is a specialized format for organizing, storing, and managing data in a computer so that it
can be accessed and modified efficiently. It defines the way data is arranged and the operations that can be
performed on it.
Importance:
Data structures are essential because they:
• Help in organizing data efficiently.
• Allow quick access and modification of data.
• Improve the performance of algorithms.
• Play a crucial role in solving complex computational problems.
Classification of Data Structures
Data structures are broadly classified into two types:
1. Linear Data Structures – Elements are arranged sequentially (e.g., Arrays, Linked Lists, Stacks,
Queues).
2. Non-Linear Data Structures – Elements are arranged in a hierarchical manner (e.g., Trees, Graphs).
RC1
Linear Data Structures
A data structure is considered linear if its elements form a sequence. The elements of a linear data structure
can be stored in:
• Contiguous memory locations (using arrays)
• Non-contiguous memory locations (using linked lists, where each element contains a pointer to the
next element)
Importance of Linear Data Structures
1. Simplicity and Easy Access – Easier to understand and manipulate.
2. Efficient Traversal – Simple to iterate through elements.
3. Data Integrity – Maintains ordered relationships among elements.
4. Memory Efficiency – Optimized memory usage in certain implementations.
5. Real-World Applications – Used in software applications, databases, and operating systems.
6. Ease of Algorithm Implementation – Fundamental to algorithm design.
7. Flexibility – Can be implemented dynamically (e.g., linked lists).
Advantages of ADTs
Encapsulation – Keeps data secure by hiding implementation details.
RC3
Time and Space Complexity Analysis for Linear Data Structures
Linear data structures, such as arrays, linked lists, stacks, and queues, require analyzing their time
complexity (how execution time increases with input size) and space complexity (how memory usage grows
with input size).
RC4
Space Complexity Table for Linear Data Structures
Linear Search
Linear search is a method of finding a particular value in a list by checking each element one by one
sequentially until the desired element is found or the list is exhausted.
Algorithm:
1. Start
2. Take the input list/array and the search element.
3. Iterate through each element in the list:
o Compare the current element with the search element.
o If they match, return the index (position) of the element.
o If not, continue to the next element.
4. If the element is not found after checking all elements, return "Element not found."
5. End
Example
Consider the following list of elements and the search element:
Search Element: 12
RC5
Index 0 1 2 3 4 5 6 7
Values 65 20 10 55 32 12 50 99
Step 2: Compare search element 12 with the second element (index 1).
12 == 20 → Not Matched → Move to next element.
Index 0 1 2 3 4 5 6 7
Values 65 20 10 55 32 12 50 99
Step 3: Compare search element 12 with the third element (index 2).
12 == 10 → Not Matched → Move to next element.
Index 0 1 2 3 4 5 6 7
Values 65 20 10 55 32 12 50 99
Step 4: Compare search element 12 with the fourth element (index 3).
12 == 55 → Not Matched → Move to next element.
Index 0 1 2 3 4 5 6 7
Values 65 20 10 55 32 12 50 99
Step 5: Compare search element 12 with the fifth element (index 4).
12 == 32 → Not Matched → Move to next element.
Index 0 1 2 3 4 5 6 7
Values 65 20 10 55 32 12 50 99
Step 6: Compare search element 12 with the sixth element (index 5).
12 == 12 → Matched! → Stop searching and display the result.
RC6
Index 0 1 2 3 4 5 6 7
Values 65 20 10 55 32 12 50 99
Code:
#include <stdio.h>
void main() {
int size, key;
int arr[size];
Binary Search
Binary search is an efficient searching technique used to check whether an element is present in a sorted
list. It repeatedly divides the search interval in half to locate the element.
RC7
Algorithm:
1. Start
2. Take the input sorted list/array and the search element.
3. Set low = 0 and high = size of array - 1.
4. Repeat until low ≤ high:
o Find the mid index: mid=(low+high)/2
o Compare arr[mid] with the search element:
▪ If arr[mid] == search element, return mid (element found).
▪ If arr[mid] < search element, search the right half (low = mid + 1).
▪ If arr[mid] > search element, search the left half (high = mid - 1).
5. If the element is not found, return "Element not found."
6. End
Example
Consider the sorted list:
Index 0 1 2 3 4 5 6 7 8
Values 3 5 7 9 10 16 21 35 36
Search Element: 35
Comparison: 35 > 10 → Search in the right sublist {16, 21, 35, 36}.
Code:
#include <stdio.h>
void main() {
int size, key;
int arr[size];
RC9
scanf("%d", &arr[i]);
}
Sorting Techniques
Sorting is the process of arranging elements in a specific order (ascending or descending). Below are three
fundamental sorting techniques:
Bubble Sort
• Bubble Sort is a simple sorting algorithm that repeatedly compares adjacent elements and swaps
them if they are in the wrong order.
• It "bubbles" the largest element to the end in each pass.
• The process repeats until the array is completely sorted.
Algorithm:
1. Start from the first element.
2. Compare it with the next element.
3. Swap if the first element is greater than the second.
4. Repeat this process for the entire array.
5. Reduce the range of comparison in each pass.
6. Stop when no swaps occur in a pass.
Time Complexity:
• Best Case: O(n)(already sorted)
• Worst/Average Case: O(n2)
Pass 1:
Compare 8 & 12 → No swap
RC10
8 12 2 4 11
Pass 2:
Compare 8 & 2 → Swap
2 8 4 11 12
Pass 3:
Compare 2 & 4 → No swap
2 4 8 11 12
Code:
#include <stdio.h>
void main() {
int n;
int arr[n];
bubbleSort(arr, n);
Selection Sort
Selection Sort is a simple sorting algorithm that repeatedly selects the smallest element from the unsorted
part and moves it to the correct position.
• Finds the minimum element and places it at the beginning.
• Repeats the process for the remaining unsorted part.
Algorithm:
1. Find the smallest element in the unsorted part.
2. Swap it with the first element of the unsorted part.
3. Repeat for the remaining elements until the array is sorted.
Time Complexity:
• Best Case: O(n2)
• Worst Case: O(n2)
• Average Case: O(n2)
RC12
Example: Sorting [8, 12, 2, 4, 11]
Initial Array:
8 12 2 4 11
Pass 1:
Find the minimum element in [8, 12, 2, 4, 11] → 2
Swap 2 with 8
2 12 8 4 11
Pass 2:
Find the minimum element in [12, 8, 4, 11] → 4
Swap 4 with 12
2 4 8 12 11
Pass 3:
Find the minimum element in [8, 12, 11] → 8
Pass 4:
Find the minimum element in [12, 11] → 11
Swap 11 with 12
2 4 8 11 12
Code:
#include <stdio.h>
void main() {
int n;
int arr[n];
selectionSort(arr, n);
Insertion Sort
Insertion Sort is a simple sorting algorithm that builds the final sorted array one element at a time. It works
similarly to how we sort playing cards in our hands:
• Takes one element at a time and inserts it into its correct position.
• Moves larger elements one position to the right to make space for the new element.
Algorithm:
1. Start from the second element (index 1).
2. Compare it with previous elements.
3. Shift larger elements to the right.
4. Insert the current element at the correct position.
5. Repeat for all elements.
RC14
Time Complexity:
• Best Case: O(n) (if already sorted)
• Worst/Average Case: O(n2)
Pass 1:
Take the second element (12), compare with 8
12 > 8 → No change
8 12 2 4 11
Pass 2:
Take the third element (2), compare with 8 and 12
Insert 2 at index 0
2 8 12 4 11
Pass 3:
Take the fourth element (4), compare with 2,8 and 12
Insert 4 at index 1
2 4 8 12 11
Pass 4:
Take the fifth element (11), compare with 2,4,8 and 12
Insert 11 at index 3
2 4 8 11 12
RC15
Code:
#include <stdio.h>
void main() {
int n;
int arr[n];
insertionSort(arr, n);
RC16