DSA Important Questions
DSA Important Questions
**7. Graphs**
**Q: Explain the representations of graphs (Adjacency Matrix and Adjacency List).**
1. **Adjacency Matrix:**
- If theres an edge between vertex `i` and `j`, `matrix[i][j] = 1`, otherwise `matrix[i][j] = 0`.
2. **Adjacency List:**
- An array of lists where each list contains the adjacent vertices of a vertex.
- **Code (C):**
```c
visited[vertex] = 1;
for (int i = 0; i < n; i++) {
```
- Uses a queue to explore all neighbors of a vertex before moving to the next level.
- **Code (C):**
```c
visited[start] = 1;
queue[rear++] = start;
visited[i] = 1;
queue[rear++] = i;
}
}
```
**8. Sorting**
**Q: Write algorithms and analyze time complexity for Bubble Sort, Quick Sort, and Merge Sort.**
1. **Bubble Sort:**
- Repeatedly compare adjacent elements and swap them if in the wrong order.
- **Code:**
```c
arr[j + 1] = temp;
```
2. **Quick Sort:**
- Partition the array around a pivot and recursively sort the partitions.
- **Code:**
```c
int i = low - 1;
i++;
arr[i] = arr[j];
arr[j] = temp;
arr[i + 1] = arr[high];
arr[high] = temp;
return i + 1;
quickSort(arr, pi + 1, high);
}
```
**10. Hashing**
- **Code:**
```c
struct Node {
int data;
};
newNode->data = key;
newNode->next = hashTable[index];
hashTable[index] = newNode;
void display() {
while (temp) {
temp = temp->next;
printf("NULL
");
```