DS Unit 1
DS Unit 1
UNIT-I
Introduction to Linear Data Structures:
1. Definition and importance of linear data structures,
2. Abstract data types (ADTs) and their implementation,
3. Overview of time and space complexity
4. Analysis for linear data structures.
5. Searching Techniques: Linear & Binary Search,
6. Sorting Techniques: Bubble sort, Selection sort, Insertion Sort
Linear data structures are two subcategories based on memory allocation changes:
Static data structures have a fixed size. Elements can change order, but the
memory allocation for the data structure stays the same. An example static data
structure is an array.
Dynamic data structures have a modular size. Runtime changes allow altering the
data structure size. Examples of dynamic data structures include queues, stacks, and
lists.
Importance:
Linear data structures include arrays, linked lists, stacks, and queues. They are
crucial in many computing tasks due to their simplicity and efficiency in accessing
and manipulating data.
Queues: Follows the First In, First Out (FIFO) principle, making them suitable for
tasks such as process scheduling and buffering.
Implementation:
A sparse matrix is a matrix that contains mostly zero values. Instead of storing all elements
in the matrix, this ADT focuses only on non-zero elements, saving memory space.
Functions:
Create(max_row, max_col): Creates a sparse matrix with the given maximum row
and column sizes. It initializes the matrix to hold up to max_row x max_col items.
Transpose(a): Produces a new matrix by interchanging the row and column values
of every triple in matrix a.
Add(a, b): Adds two sparse matrices a and b together if they have the same
dimensions. It returns the matrix produced by adding corresponding items (with
identical row and column values).
Multiply(a, b): Multiplies two sparse matrices a and b if the number of columns in a
equals the number of rows in b. It returns the matrix d produced by multiplying a by
b according to a specified formula.
Operations:
Each triple <row, column, value> represents a non-zero element in the sparse
matrix.
Max-row and max-col represent the maximum row and column sizes respectively.
The index function retrieves the value of a specific element at row i and column j.
The item set represents the set of possible values that can be stored in the matrix.
Example:
Consider two sparse matrices, A and B, defined as follows:
I B.Tech II SEM R23 BIET
Matrix A:
1 0 3
0 2 0
0 0 0
Matrix B:
0 0 1
0 4 0
2 0 0
Operations:
Create(max_row, max_col):
Transpose(a):
Transpose matrix A:
1 0 0
0 2 0
3 0 0
Transpose matrix B:
0 0 2
0 4 0
1 0 0
Add(a, b):
Matrix A + Matrix B:
A[1,1] + B[1,1] = 1 + 0 = 1
A[1,3] + B[1,3] = 3 + 1 = 4
A[2,2] + B[2,2] = 2 + 4 = 6
A[3,1] + B[3,1] = 0 + 2 = 2
1 0 4
0 6 0
2 0 0
Multiply(a, b):
Matrix A * Matrix B:
I B.Tech II SEM R23 BIET
2 0 1
0 8 0
0 0 0
Implementation in C:
#include <stdio.h>
#include <stdlib.h>
int main() {
// Use the defined functions to create, manipulate, and print sparse matrices
return 0;
}
I B.Tech II SEM R23 BIET
Space complexity refers to the total amount of memory (primary memory) a program needs
to run to its completion. It can be broken down into three main components:
S(p) = C + Sp
Where:
- C is the fixed part, including instruction space and space for single and fixed-size
variables.
- Sp is the variable part, representing space needed for referenced variables, dependent
on instance characteristics and recursion state space.
Time Complexity:
Time complexity measures the amount of time it takes for an algorithm to run. Absolute
time complexity is determined by the number of instructions executed multiplied by the
time taken to execute one instruction.
Worst Case: The maximum time required for the program to execute. It is represented
by the Big O notation (\( O \)).
Average Case: The average time required for program execution. Represented by the
Theta notation (\( \Theta \)).
Best Case: The minimum time required for program execution. Represented by the
Omega notation (\( \Omega \)).
Time Complexity:
Access: O(1) - accessing elements by index.
Search: O(n) - linear search if the elements are unsorted.
Insertion/Deletion:
At the beginning: O(n) - requires shifting elements.
At the end: O(1) - if appending to the end.
In the middle: O(n) - requires shifting elements.
Linked Lists:
Space Complexity: Linked lists have a space complexity of O(n), where n is the number of
elements in the list. This is because each element requires additional memory for the
pointer to the next element.
Time Complexity:
Access: O(n) - sequential traversal required.
Search: O(n) - linear search if the elements are unsorted.
Insertion/Deletion:
At the beginning: O(1) - no shifting required.
At the end: O(n) - traversal required to reach the end.
In the middle: O(n) - traversal required to find insertion point.
Stacks:
Space Complexity: Stacks have a space complexity of O(n), where n is the maximum
number of elements in the stack.
Time Complexity:
Access/Search: O(n) - no direct access to arbitrary elements.
I B.Tech II SEM R23 BIET
Push/Pop: O(1) - constant time complexity for adding/removing elements from the top of
the stack.
Queues:
Space Complexity: Queues also have a space complexity of O(n), where n is the maximum
number of elements in the queue.
Time Complexity:
Access/Search: O(n) - no direct access to arbitrary elements.
Enqueue/Dequeue: O(1) - constant time complexity for adding/removing elements from the
front/rear of the queue.
Searching refers to the operation of fnding the position of a given item in a collection of
items. Searching in data structure refers to the process of fnding the location of an element
in a list.
b. External search:
The searching method in which elements are kept in secondary storage is called external
search. The time required is longer, and the perfect search technique for a lot of data.
B tree and B+ tree are external searching techniques.
i. If several elements are very large, this method of searching is very insufficient and
slow.
1. Start.
2. The array must be in ascending or descending sorted order.
Then find the middle element of the array, which will be referred to as mid.
3. Compare the middle element, mid with searched element or key element, key.
4. There are three cases assumed that the array is in ascending order:
i. If mid = key, that is, if mid is the desired element, then the search is successful.
ii. If mid > key, that is, the mid element is greater than key element, then search only
in the first half of the array.
iii. If mid < key, that is, the mid element is less than key element, then search only in
the second half of the array, considered array elements are sorted in ascending
order.
5. Repeat the above steps 1–3 until the key element is found.
6. Stop.
flag=1;
} if(item<a[mid]) end=mid-1;
else
beg=mid+1;
} if(flag==0)
printf("\n Item is not found in the list.");
return 0;
}
Bubble Sort:
Bubble Sort Algorithm:
1. Start traversing the array from the first element to the second last element.
2. Compare each pair of adjacent elements.
3. If the elements are in the wrong order (i.e., the current element is greater than
the next element), swap them.
4. Continue this process until the end of the array.
5. After the first pass, the largest element will be at the end of the array.
6. Repeat steps 1-5 for the remaining unsorted elements (excluding the already
sorted elements at the end of the array).
7. Continue this process until the entire array is sorted.
I B.Tech II SEM R23 BIET
#include <stdio.h>
1
5
Original array:
42715
Sorted array:
12457
Selection Sort:
Selection Sort Algorithm:
1. Start with the first element of the array as the minimum (assuming it's the
smallest).
2. Traverse the array from the second element to the last element.
3. Find the minimum element in the unsorted part of the array.
4. Swap the minimum element with the first element of the unsorted part.
5. Repeat steps 2-4 until the entire array is sorted.
#include <stdio.h>
// Function to perform Selection Sort
void selectionSort(int arr[], int n) {
int i, j, min_idx;
for (i = 0; i < n-1; i++) {
min_idx = i;
// Find the minimum element in the unsorted part of the array
for (j = i+1; j < n; j++) {
if (arr[j] < arr[min_idx]) {
min_idx = j;
}
}
// Swap the minimum element with the first element of the unsorted part
int temp = arr[i];
arr[i] = arr[min_idx];
arr[min_idx] = temp;
}
}
int main() {
int n;
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
I B.Tech II SEM R23 BIET
return 0;
}
Enter the number of elements in the array: 5
Enter 5 elements:
9
5
2
7
1
Original array:
95271
Sorted array:
12579
Insertion Sort:
Insertion Sort Algorithm:
#include <stdio.h>
// Move elements of arr[0..i-1], that are greater than key, to one position ahead of their
current position
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}
int main() {
int n;
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
return 0;
}
Original array:
952714
Sorted array:
124579