Dsa 1
Dsa 1
Dsa 1
Where:
• Index = The index of the element whose address is to be found (not the value of the
element).
• B = Base address of the array.
• W = Storage size of one element in bytes.
• LB = Lower bound of the index (if not specified, assume zero).
Example: Given the base address of an array A[1300 ………… 1900] as 1020 and the
size of each element is 2 bytes in the memory, find the address of A[1700].
Given:
Formula used:
Address of A[Index] = B + W * (Index – LB)
Address of A[1700] = 1020 + 2 * (1700 – 1300)
= 1020 + 2 * (400)
= 1020 + 800
Address of A[1700] = 1820
IN 2D ARRAY:
1. Row-Major Order:
In row-major order, elements of the array are stored row by row in memory.
The formula to calculate the address of an element A[i][j] in a 2D array stored in row-major order is:
Where:
- B = Base address of the array (the address of the first element).
- W = Size of each element in bytes.
- i = Row index.
- j = Column index.
- LR = Lower bound of row index (default is 0 if not specified).
- LC = Lower bound of column index (default is 0 if not specified).
- NC = Number of columns in the array.
2. Column-Major Order:
Where:
- B, W, i, j, LR, and LC are the same as above.
- NR = Number of rows in the array.
Example:
Let's say we have a 2D array A[4][5] (4 rows and 5 columns) stored in row-major order. The base address
B is 1000, and each element takes 2 bytes of memory.
Given:
- Base address B = 1000
- Size of each element W = 2 bytes
- Number of columns NC = 5
- Lower bounds LR = LC = 0
- Row index i = 2
- Column index j = 3
Using the row-major order formula:
Address of A[2][3] = 1000 + 2 × ((2 - 0) × 5 + (3 - 0))
Result:
Steps:
1. Initialize the resultant matrix C with dimensions m × p. Set all elements of C to 0.
C[i][j] = 0 for all i, j
2. Loop through the rows of matrix A using index i (from 0 to m-1).
3. Loop through the columns of matrix B using index j (from 0 to p-1).
4. Compute the dot product of the i-th row of A and the j-th column of B.
5. Accumulate the product into C[i][j].
6. Once all iterations are complete, matrix C contains the result.
PSEUDO CODE FOR MATRIX MULTIPLICATION:
1. Array Traversal:
Algorithm:
Pseudocode:
ArrayTraversal(A, n)
for (i = 0; i<n; i++)
Access A[i]
Process A[i]
end for
2. Array Insertion:
Algorithm:
1. Start with an array A of size n and the position pos where you want to insert a new element.
2. If the array is full, return an error or resize the array.
3. Shift all elements from index pos to n-1 one position to the right.
4. Insert the new element at position pos.
5. Increase the size of the array by 1.
6. End.
Pseudocode:
end for
A[pos] = element
n=n+1
3. Array Deletion:
Algorithm:
1. Start with an array A of size n and the position pos of the element to be deleted.
2. Shift all elements from index pos+1 to n-1 one position to the left.
3. Decrease the size of the array by 1.
4. End.
Pseudocode:
ArrayDeletion(A, n, pos)
for (i = pos; i<n; i++)
A[i] = A[i+1
end for
n=n-1
Binary Search:
cc
Pseudocode:
Explanation of Time Complexity O(log n)
Binary Search operates by dividing the search range in half during each step, which is the key
reason why its time complexity is O(logn)O(\log n)O(logn). Let's break down the
mathematical reasoning behind it:
Step-by-Step Breakdown:
• Let's calculate how many steps it takes to search through a sorted array with n = 10^6
elements using binary search.
o Binary search reduces the search space by half in each step.
o The number of steps required is approximately log₂(n).
Bubble Sort works by repeatedly stepping through the list, comparing adjacent elements, and
swapping them if they are in the wrong order. This process is repeated until the list is sorted.
Pseudocode:
BubbleSort(A, n)
1. for i = 0 to n-1:
a. for j = 0 to n-i-2:
i. if A[j] > A[j+1]:
- Swap A[j] and A[j+1]
2. End.
If the total number of steps in Bubble Sort is 21, how many elements are in the array?
The number of comparisons in Bubble Sort can be expressed as the sum of the first n−1 natural
numbers:
Total Comparisons=n(n−1)/2
n(n−1)/2 = 21
n(n−1)=42
n^2 – n – 42 =0
So, n=7.
Answer:
Selection Sort:
Algorithm for Selection Sort:
Steps:
SelectionSort(A, n)
for i = 0 to n-1 do
minIndex = i
for j = i+1 to n-1 do
if A[j] < A[minIndex] then
minIndex = j
end if
end for
Swap(A[i], A[minIndex])
end for
end
Example:
1. Find the smallest element in the array (11), and swap it with the first element.
o Array becomes: [11, 25, 12, 22, 64]
2. Find the smallest element in the unsorted portion (12), and swap it with the second element.
o Array becomes: [11, 12, 25, 22, 64]
3. Find the smallest element in the remaining unsorted portion (22), and swap it with the third
element.
o Array becomes: [11, 12, 22, 25, 64]
4. No further swaps are needed, as the array is now sorted.
Insertion Sort:
Insertion Sort Algorithm:
Pseudocode:
InsertionSort(A, n)
for i = 1 to n-1 do
key = A[i]
j = i - 1