DSA Practical File-1
DSA Practical File-1
#include <stdio.h>
int array[10];
void main()
{
input_array(array, 5);
bubble_sort(array, 5);
print_array(array, 5);
}
#include <stdio.h>
int array[10];
void main()
{
input_array(array, 5);
insertion_sort(array, 5);
print_array(array, 5);
}
A[i + 1] = key;
}
}
Output:
Practical 3
#include <stdio.h>
int array[10];
void main()
{
input_array(array, 5);
selection_sort(array, 5);
print_array(array, 5);
}
return loc;
}
Output:
Practical 4
#include <stdio.h>
#include <limits.h>
int array[10];
void input_array(int[], int);
void print_array(int[], int);
void merge_sort(int[], int, int);
void merge(int[], int, int, int);
void main()
{
input_array(array, 5);
merge_sort(array, 1, 5);
print_array(array, 5);
}
if (P < R)
{
Q = (P + R) / 2;
merge_sort(A, P, Q);
merge_sort(A, Q + 1, R);
print_array(array, 5);
merge(A, P, Q, R);
}
}
n1 = Q - P + 1;
n2 = R - Q;
LEFT[n1 + 1] = INT_MAX;
RIGHT[n2 + 1] = INT_MAX;
i = 1, j = 1;
#include <stdio.h>
#include <limits.h>
int array[10];
void input_array(int[], int);
void print_array(int[], int);
void quick_sort(int[], int, int);
int partition(int[], int, int);
void main()
{
input_array(array, 5);
quick_sort(array, 1, 5);
print_array(array, 5);
}
if (P < R)
{
Q = partition(A, P, R);
quick_sort(A, P, Q - 1);
quick_sort(A, Q + 1, R);
}
}
x = A[ R ];
i = P - 1;
temp = A[ i + 1 ];
A[ i + 1 ] = A[ R ];
A[ R ] = temp;
return ( i + 1 );
}
Output:
Practical 6
#include <stdio.h>
#include <stdlib.h>
struct Stack {
int array[MAX_SIZE];
int top;
};
int main() {
struct Stack myStack;
initialize(&myStack);
push(&myStack, 10);
push(&myStack, 20);
push(&myStack, 30);
return 0;
}
Output:
Practical 7
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
struct Stack {
struct Node* top;
};
newNode->data = value;
newNode->next = stack->top;
stack->top = newNode;
stack->top = poppedNode->next;
free(poppedNode);
return poppedValue;
}
int main() {
struct Stack myStack;
initialize(&myStack);
push(&myStack, 15);
push(&myStack, 25);
push(&myStack, 35);
return 0;
}
Output:
Practical 8
#include <stdio.h>
#include <stdlib.h>
struct Queue {
int array[MAX_SIZE];
int front, rear;
};
if (isEmpty(queue)) {
queue->front = 0; // If the queue is empty, set front to 0
}
removedValue = queue->array[queue->front];
if (queue->front == queue->rear) {
queue->front = -1;
queue->rear = -1;
} else {
queue->front = (queue->front + 1) % MAX_SIZE;
}
return removedValue;
}
int main() {
struct Queue myQueue;
initialize(&myQueue);
enqueue(&myQueue, 10);
enqueue(&myQueue, 20);
enqueue(&myQueue, 30);
return 0;
}
Output:
Practical 9
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
struct Queue {
struct Node* front;
struct Node* rear;
};
newNode->data = value;
newNode->next = NULL;
if (isEmpty(queue)) {
queue->front = newNode;
queue->rear = newNode;
} else {
queue->rear->next = newNode;
queue->rear = newNode;
}
if (queue->front == queue->rear) {
queue->front = NULL;
queue->rear = NULL;
} else {
queue->front = removedNode->next;
}
free(removedNode);
return removedValue;
}
int main() {
struct Queue myQueue;
initialize(&myQueue);
enqueue(&myQueue, 15);
enqueue(&myQueue, 25);
enqueue(&myQueue, 35);
return 0;
}
Output:
Practical 10
#include <stdio.h>
#include <stdlib.h>
struct TreeNode {
int data;
struct TreeNode* left;
struct TreeNode* right;
};
return root;
}
int main() {
struct TreeNode* root = NULL;
freeTree(root);
return 0;
}
Output: