MY Pavithra Documents PDF
MY Pavithra Documents PDF
#include <stdio.h>
*xp = *yp;
*yp = temp;
int i, j;
int i;
printf("\n");
}
int main() {
int arr[] = {10, 56, 33, 78, 21, 45, 15, 100};
printArray(arr, n);
bubbleSort(arr, n);
printArray(arr, n);
return 0;
#include <stdio.h>
*xp = *yp;
*yp = temp;
int i, j;
int swapped;
if (swapped == 0) {
break;
int i;
printf("\n");
int main() {
printArray(arr, n);
bubbleSort(arr, n);
printf("Sorted array is \n");
printArray(arr, n);
return 0;
#include <stdio.h>
int main() {
int arr[100] = {1, 5, 8, 12};
int n = sizeof(arr) / sizeof(arr[0]); // Get the number of elements (optional)
insertElement(arr, n, x, pos);
printf("Array after insertion: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
#include <stdio.h>
printf("Invalid position!\n");
return;
// Decrement the array size (optional, if you want to keep track of the actual number of elements)
n--;
printf("\n");
}
int main() {
int pos;
scanf("%d", &pos);
deleteElement(arr, n, pos);
return 0;
Algorithm analysis is all about understanding how well an algorithm performs. It's like figuring out
how fast and efficient a recipe is for cooking a meal. Here are the key steps involved:
Meaning:
We want to know how much time and memory an algorithm needs to solve a problem.
This helps us choose the best algorithm for a specific task, especially when dealing with large
amounts of data.
Steps:
Choose Metrics:
Time Complexity: How long does the algorithm take to run? (e.g., O(n), O(n^2))
Space Complexity: How much memory does the algorithm use? (also in Big O notation)
Break down the steps and see how many times each step runs based on the input size.
Identify the key parts that use the most time and memory.
Understand how the algorithm performs based on the problem and typical data sizes.
Compare different algorithms for the same problem and choose the most efficient one overall.
Time Complexity: How long it takes the algorithm to run, based on the size of the input data.
Imagine how many steps it needs to complete the task.
Space Complexity: How much memory the algorithm uses, also based on the input data size.
Think of how much extra space it needs to borrow to work on the problem.
The time-space tradeoff is a fundamental concept in computer science where you can
improve the speed (time) of a program by using more memory (space), or vice-versa. There's a
constant balancing act between these two factors.
Lookup Tables vs. Calculations: A lookup table is a giant table that stores pre-computed
answers. This can significantly speed up calculations because you just look up the answer instead of
recomputing it every time. However, lookup tables take up a lot of space. If the calculation is simple
and storage space is limited, it might be faster to just do the calculation on the fly, even if it takes a
bit longer.
data_type array_name[size1][size2]...[sizeN];
int matrix[3][4];
#include <stdio.h>
int main() {
int dim1, dim2, dim3;
int arr[dim1][dim2][dim3];
return 0;
}
Regular matrices store all elements in a 2D array, wasting space for zeros in sparse matrices.
Sparse matrices use specialized representations to store only non-zero elements.
One common way is Compressed Sparse Row (CSR). It uses three arrays:
For example, a sparse matrix with zeros at (1,2) and (2,1) can be represented as:
Values: [3, 5] (non-zero values) Column Indices: [1, 0] (corresponding columns) Row
Pointers: [0, 1, 2] (start of non-zeros in each row)
#include <stdio.h>
int main() {
int m, n, non_zero_elements = 0;
int sparse_matrix[MAX_SIZE][MAX_SIZE] = {
{0, 7, 0, 0},
{0, 0, 3, 0},
{5, 0, 0, 1},
};
if (sparse_matrix[i][j] != 0) {
non_zero_elements++;
int k = 0;
if (sparse_matrix[i][j] != 0) {
k++;
return 0;
12. Write a short note on memory management functions in C with an example for each.?
C provides several functions for allocating and deallocating memory during program execution. These
functions are crucial for dynamic memory management, allowing you to work with data structures
whose size is not known beforehand. All these functions are declared in the `<stdlib.h>` header file.
- returns a `void*` pointer, which needs to be cast to the desired data type.
**Example:**
```c
if (ptr == NULL) {
```
2. **calloc()**
**Example:**
```c
char* buffer = (char*)calloc(100, sizeof(char)); // Allocate 100 bytes and initialize to zeros
if (buffer == NULL) {
// handle allocation failure
```
3. **realloc()**
- returns a `void*` pointer to the resized block (may change the original pointer).
**Example:**
```c
if (data == NULL) {
if (new_data == NULL) {
```
4. **free()**
```c
```
**Important Note:**
- Always check the return value of allocation functions (e.g., `malloc()`) for errors (NULL pointer).
- Ensure you free all dynamically allocated memory before the program exits to avoid memory leaks.
13. Write an Algorithm to count the number of nodes in a singly linked list?
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
};
new_node->data = data;
new_node->next = NULL;
return new_node;
int count = 0;
count++;
current = current->next;
return count;
head = head->next;
printf("\n");
int main() {
/* Create a sample linked list */
head->next = newNode(2);
head->next->next = newNode(3);
printList(head);
return 0;
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
};
new_node->data = data;
new_node->next = NULL;
return new_node;
}
// Function to traverse the linked list
current = current->next;
int main() {
head->next = newNode(2);
head->next->next = newNode(3);
traverse(head);
return 0;
15. Write an Algorithm to insert a node a) in the beginning b) in the end of a singly linked list c)
after a specific node?
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
};
new_node->data = data;
new_node->next = NULL;
return new_node;
new_node->next = (*head_ref);
(*head_ref) = new_node;
if (*head_ref == NULL) {
(*head_ref) = new_node;
return;
last = last->next;
}
last->next = new_node;
if (prev_node == NULL) {
return;
new_node->next = prev_node->next;
prev_node->next = new_node;
node = node->next;
printf("\n");
int main() {
head->next = newNode(2);
head->next->next = newNode(3);
// Insert at beginning
insertAtBeginning(&head, 0);
printList(head);
// Insert at end
insertAtEnd(&head, 4);
16. Write an algorithm to delete a node from a) beginning b) end c) specific key element of a
singly linked list?
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
};
new_node->data = data;
new_node->next = NULL;
return new_node;
*head_ref = (*head_ref)->next;
free(temp);
if (*head_ref == NULL) {
prev = current;
current = current->next;
if (prev == NULL) {
free(*head_ref);
*head_ref = NULL;
return;
prev->next = NULL;
free(current);
if (*head_ref == NULL) {
return;
if ((*head_ref)->data == key) {
deleteFromBeginning(head_ref);
return;
// If key is found
if (current->data == key) {
prev->next = current->next;
free(current);
return;
prev = current;
current = current->next;
node = node->next;
printf("\n");
int main() {
head->next = newNode(20);
head->next->next = newNode(30);
head->next->next->next = newNode(40);
printList(head);
deleteNode(&head, 20);
printList(head);
return 0;