Simulation-based DSA Questions and Solutions
1. Bubble Sort Simulation (Step-by-Step)
Code:
#include <stdio.h>
void bubbleSort(int arr[], int n) {
int i, j, temp;
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
printf("Array after pass %d: ", i + 1);
for (int k = 0; k < n; k++) {
printf("%d ", arr[k]);
printf("\n");
int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr) / sizeof(arr[0]);
bubbleSort(arr, n);
return 0;
Simulation:
Consider the array arr[] = {64, 34, 25, 12, 22, 11, 90}.
- Initial array: {64, 34, 25, 12, 22, 11, 90}
Pass 1:
- Compare 64 and 34. Since 64 > 34, swap them. New array: {34, 64, 25, 12, 22, 11, 90}
- Compare 64 and 25. Since 64 > 25, swap them. New array: {34, 25, 64, 12, 22, 11, 90}
...
2. Insertion in an Array Simulation
Code:
#include <stdio.h>
void insertElement(int arr[], int *n, int element, int position) {
for (int i = *n; i > position; i--) {
arr[i] = arr[i - 1];
arr[position] = element;
(*n)++;
int main() {
int arr[100] = {1, 2, 3, 4, 5};
int n = 5, element = 10, position = 2;
insertElement(arr, &n, element, position);
printf("Array after insertion: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
return 0;
Simulation:
Consider the array arr[] = {1, 2, 3, 4, 5}, with element = 10, position = 2 (0-indexed).
...
3. Deletion in an Array Simulation
Code:
#include <stdio.h>
void deleteElement(int arr[], int *n, int position) {
for (int i = position; i < *n - 1; i++) {
arr[i] = arr[i + 1];
(*n)--;
int main() {
int arr[100] = {1, 2, 3, 4, 5};
int n = 5, position = 2;
deleteElement(arr, &n, position);
printf("Array after deletion: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
return 0;
Simulation:
Consider the array arr[] = {1, 2, 3, 4, 5}, and we want to delete the element at position = 2
(0-indexed).
...
4. Linear Search Simulation (Step-by-Step)
Code:
#include <stdio.h>
void linearSearch(int arr[], int n, int target) {
for (int i = 0; i < n; i++) {
printf("Comparing target %d with element %d at index %d\n", target, arr[i], i);
if (arr[i] == target) {
printf("Element found at index %d\n", i);
return;
}
printf("Element not found\n");
int main() {
int arr[] = {2, 3, 4, 10, 40};
int n = sizeof(arr) / sizeof(arr[0]);
int target = 10;
linearSearch(arr, n, target);
return 0;
Simulation:
Consider the array arr[] = {2, 3, 4, 10, 40}, and we want to search for the target = 10.
...
5. Binary Search Simulation (Step-by-Step)
Code:
#include <stdio.h>
int binarySearch(int arr[], int n, int target) {
int low = 0, high = n - 1;
while (low <= high) {
int mid = low + (high - low) / 2;
printf("Checking middle element: %d at index %d\n", arr[mid], mid);
if (arr[mid] == target)
return mid;
else if (arr[mid] < target)
low = mid + 1;
else
high = mid - 1;
return -1;
int main() {
int arr[] = {2, 3, 4, 10, 40};
int n = sizeof(arr) / sizeof(arr[0]);
int target = 10;
int result = binarySearch(arr, n, target);
if (result != -1)
printf("Element found at index %d\n", result);
else
printf("Element not found\n");
return 0;
Simulation:
Consider the array arr[] = {2, 3, 4, 10, 40}, and we want to search for the target = 10. The array is
sorted, so binary search is applicable.
...
6. Time Complexity Analysis of Bubble Sort (Step-by-Step)
Code:
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
void bubbleSort(int arr[], int n) {
int temp;
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
int main() {
int n[] = {10, 100, 1000};
for (int i = 0; i < 3; i++) {
int size = n[i];
int arr[size];
for (int j = 0; j < size; j++) {
arr[j] = rand() % 100;
}
clock_t start = clock();
bubbleSort(arr, size);
clock_t end = clock();
double time_taken = ((double)(end - start)) / CLOCKS_PER_SEC;
printf("Time taken to sort array of size %d: %f seconds\n", size, time_taken);
return 0;
Simulation:
This program measures the time taken to sort arrays of sizes 10, 100, and 1000 using bubble sort.