DS Solutions
DS Solutions
Short Answers
2. What is linear data structure and non-linear data structure? Give some examples?
Linear data structure: Data elements are arranged in a sequential order. Examples: Arrays, Linked Lists, Stacks, Queues.
Non-linear data structure: Data elements are not arranged in a sequential order. Examples: Trees, Graphs.
8. Write the time complexity (best, average, worst case) of bubble sort, selection sort, and insertion sort.
Bubble sort: Best - O(n), Average - O(n²), Worst - O(n²).
Selection sort: Best - O(n²), Average - O(n²), Worst - O(n²).
Insertion sor: Best - O(n), Average - O(n²), Worst - O(n²).
---
Long Answers
1. Illustrate a program to search for an element in the given list of numbers using linear search approach.
#include <stdio.h>
int main() {
int arr[] = {21, 54, 32, 67, 2, 45, 21, 45, 8};
int n = sizeof(arr) / sizeof(arr[0]);
int target = 21;
int result = linearSearch(arr, n, target);
if (result != -1) {
printf("Element found at index: %d\n", result);
} else {
printf("Element not found\n");
}
return 0;
}
2. Illustrate a program to search for an element in the given list of numbers using binary search approach.
#include <stdio.h>
int main() {
int arr[] = {12, 16, 17, 19, 20, 22, 24, 29, 30, 32, 37};
int n = sizeof(arr) / sizeof(arr[0]);
int target = 32;
int result = binarySearch(arr, 0, n - 1, target);
if (result != -1) {
printf("Element found at index: %d\n", result);
} else {
printf("Element not found\n");
}
return 0;
}
3. Explain linear search algorithm in detail for the given elements: 21, 54, 32, 67, 2, 45, 21, 45, 8. Consider 21 as a search element.
- Start from the first element (21).
- Compare 21 with the target (21). They match, so the search is successful at index 0.
- If the target were not found, the algorithm would continue checking each element until the end of the list.
4. Outline an algorithm to implement Binary Search technique. Use the algorithm to search 32 in the following list of elements. Explain the
process at each step. 12, 16, 17, 19, 20, 22, 24, 29, 30, 32, 37.
Step 1: Initialize `left = 0`, `right = 10`.
Step 2: Calculate `mid = (0 + 10) / 2 = 5`. Compare `arr[5] = 22` with 32. Since 22 < 32, set `left = mid + 1 = 6`.
Step 3: Calculate `mid = (6 + 10) / 2 = 8`.
int main() {
int arr[] = {20, 12, 10, 15, 2};
int n = sizeof(arr) / sizeof(arr[0]);
selectionSort(arr, n);
printf("Sorted array: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
6. Illustrate Selection sort to identify how many iterations required to place 12 in its correct position for the unsorted list 20, 12, 10, 15, 2.
iteration 1: Find the smallest element (2) and swap it with the first element (20). List: [2, 12, 10, 15, 20].
Iteration 2: Find the next smallest element (10) and swap it with the second element (12). List: [2, 10, 12, 15, 20].
Iteration 3: 12 is already in its correct position.
- Total iterations required: 2
Unit-2: Solutions**
Short Answers
5.Discuss the drawback of a singly linked list compared to a doubly linked list.
Singly linked list: Can only traverse in one direction (forward).
Doubly linked list: Can traverse in both directions (forward and backward), making operations like deletion and insertion more efficient.
---
struct Node {
int data;
struct Node* next;
};
int main() {
// Creating a sample linked list: 10 -> 20 -> 30 -> NULL
struct Node* head = (struct Node*)malloc(sizeof(struct Node));
struct Node* second = (struct Node*)malloc(sizeof(struct Node));
struct Node* third = (struct Node*)malloc(sizeof(struct Node));
head->data = 10;
head->next = second;
second->data = 20;
second->next = third;
third->data = 30;
third->next = NULL;
// Free memory
free(head);
free(second);
free(third);
return 0;
}
```
3. Discuss different cases of insertion operations on singly linked lists. Design a pseudo code for the same.
Insertion at the beginning:
if (*head == NULL) {
*head = newNode;
return;
}
struct Node* temp = *head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
}
```
Insertion at a specific position:
if (position == 1) {
newNode->next = *head;
*head = newNode;
return;
}
if (temp == NULL) {
printf("Position out of range\n");
return;
}
newNode->next = temp->next;
temp->next = newNode;
}
4. Discuss different cases of deletion operations on singly linked lists. Design a code snippet for the same.
Deletion at the beginning:
5.With neat diagrams, explain the following operations in a singly linked list data structure
-Insert element at the beginning:
- Create a new node.
- Point the new node’s `next` to the current head.
- Update the head to point to the new node.
- Insert element at the end:
- Traverse to the last node.
- Point the last node’s `next` to the new node.
- Set the new node’s `next` to `NULL`.
- Insert the specified element:
- Traverse to the node before the desired position.
- Point the new node’s `next` to the next node.
- Update the previous node’s `next` to point to the new node.
- Search the given element:
- Traverse the list and compare each node’s data with the target value.
- Return the position if found, otherwise return `-1`.
Sort the given list:
-Use sorting algorithms like Bubble Sort