0% found this document useful (0 votes)
2 views

Sanjeet Data Structure

Uploaded by

vikramsingal44
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Sanjeet Data Structure

Uploaded by

vikramsingal44
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Banwari Lal

Jindal Suiwala
College
Tosham
(BHIWANI)
Subject: Data Structure
C Programming Practical
Submitted to:
Priyanka Mam
(Assistant Professor
of Computer Department) Submitted by:
Name: Sanjeet Kumar
Class: B.C.A. 2nd Year
Roll no.: 1231393005
INDEX

I. Implement Insertion Sort. The program should


report the number of comparisons.
II. Implement Merge Sort. The program should
report the number of comparisons.
III. Implement Heap Sort. The program should
report the number of comparisons.
IV. Implement Randomized Quick Sort. The
program should report the number of
comparisons.
V. Implement radix sort.
VI. Create a Fibonacci series using function
VII. Implementation of graph using array
VIII. Implement of Recursive function.
IX. Implementation of Linked List Using array
X. Creation and traversal of binary search tree.
XI. Implementation of stack using array
XII. Implement of two string combination
/* Implement Insertion Sort. The program should report the
number of comparisons */

#include <stdio.h>
int main() {
int arr[] = {8, 3, 2, 5, 1, 3};
int n = sizeof(arr) / sizeof(arr[0]);
int i, j, key, comparisons = 0;
for (i = 1; i < n; i++) {
key = arr[i];
j = i - 1;
while (j >= 0 && arr[j] > key) {
comparisons++;
arr[j + 1] = arr[j];
j--;
}
comparisons++;
arr[j + 1] = key;
}
printf("Sorted array: ");
for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\nNumber of comparisons: %d\n", comparisons);
return 0;
}

Signature
/* Implement Merge Sort. The program should report the number
of comparisons */

#include <stdio.h>
int comparisons = 0;
void merge(int arr[], int left, int mid, int right) {
int n1 = mid - left + 1, n2 = right - mid, L[n1], R[n2];
for (int i = 0; i < n1; i++) L[i] = arr[left + i];
for (int j = 0; j < n2; j++) R[j] = arr[mid + 1 + j];
int i = 0, j = 0, k = left;
while (i < n1 && j < n2) {
comparisons++;
arr[k++] = (L[i] <= R[j]) ? L[i++] : R[j++];
}
while (i < n1) arr[k++] = L[i++];
while (j < n2) arr[k++] = R[j++];
}
void mergeSort(int arr[], int left, int right) {
if (left < right) {
int mid = left + (right - left) / 2;
mergeSort(arr, left, mid);
mergeSort(arr, mid + 1, right);
merge(arr, left, mid, right);
}
}
int main() {
int arr[] = {57, 26, 83, 2, 9, 82, 10};
int n = sizeof(arr) / sizeof(arr[0]);
mergeSort(arr, 0, n - 1);
printf("Sorted array: ");
for (int i = 0; i < n; i++) printf("%d ", arr[i]);
printf("\nComparisons: %d\n", comparisons);
return 0;
}

Signature
/* Implement Heap Sort. The program should report the number of
comparisons */

#include <stdio.h>
int comparisons = 0;
void swap(int* a, int* b) {
int temp = *a;
*a = *b;
*b = temp;
}

void heapify(int arr[], int n, int i) {


int largest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;
if (left < n) {
comparisons++;
if (arr[left] > arr[largest])
largest = left;
}

if (right < n) {
comparisons++;
if (arr[right] > arr[largest])
largest = right;
}

if (largest != i) {
swap(&arr[i], &arr[largest]);
heapify(arr, n, largest);
}
}

void heapSort(int arr[], int n) {


for (int i = n / 2 - 1; i >= 0; i--)
heapify(arr, n, i);
for (int i = n - 1; i > 0; i--) {
swap(&arr[0], &arr[i]);
heapify(arr, i, 0);
}
}

void printArray(int arr[], int n) {


for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
}

int main() {
int arr[] = {10, 11, 12, 9, 6, 7};
int n = sizeof(arr) / sizeof(arr[0]);
printf("Original array:\n");
printArray(arr, n);
heapSort(arr, n);
printf("Sorted array:\n");
printArray(arr, n);
printf("Number of comparisons: %d\n", comparisons);
return 0;
}

Signature
/* Implement Randomized Quick Sort. The program should report
the number of comparisons */

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int comparisons = 0;
void swap(int* a, int* b) {
int temp = *a;
*a = *b;
*b = temp;
}
int partition(int arr[], int low, int high) {
int pivot = arr[high];
int i = low - 1;
for (int j = low; j <= high - 1; j++) {
comparisons++;
if (arr[j] < pivot) {
i++;
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[high]);
return (i + 1);
}
int randomizedPartition(int arr[], int low, int high) {
int random = low + rand() % (high - low + 1);
swap(&arr[random], &arr[high]);
return partition(arr, low, high);
}
void randomizedQuickSort(int arr[], int low, int high) {
if (low < high) {
int pi = randomizedPartition(arr, low, high);
randomizedQuickSort(arr, low, pi - 1);
randomizedQuickSort(arr, pi + 1, high);
}
}
void printArray(int arr[], int size) {
for (int i = 0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");
}
int main() {
srand(time(0));
int arr[] = {48, 78, 1, 45, 7, 5};
int n = sizeof(arr) / sizeof(arr[0]);
printf("Original array:\n");
printArray(arr, n);
randomizedQuickSort(arr, 0, n - 1);
printf("Sorted array:\n");
printArray(arr, n);
printf("Number of comparisons: %d\n", comparisons);
return 0;
}

Signature
/* Implement radix sort */

#include <stdio.h>
#include <stdlib.h>
int getMax(int arr[], int n) {
int max = arr[0];
for (int i = 1; i < n; i++) {
if (arr[i] > max)
max = arr[i];
}
return max;
}
void countingSort(int arr[], int n, int exp) {
int output[n];
int count[10] = {0};
for (int i = 0; i < n; i++)
count[(arr[i] / exp) % 10]++;
for (int i = 1; i < 10; i++)
count[i] += count[i - 1];
for (int i = n - 1; i >= 0; i--) {
output[count[(arr[i] / exp) % 10] - 1] = arr[i];
count[(arr[i] / exp) % 10]--;
}
for (int i = 0; i < n; i++)
arr[i] = output[i];
}
void radixSort(int arr[], int n) {
int max = getMax(arr, n);
for (int exp = 1; max / exp > 0; exp *= 10)
countingSort(arr, n, exp);
}
void printArray(int arr[], int n) {
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n"); }
int main() {
int arr[] = {175, 10, 74, 91, 804, 58, 47, 44};
int n = sizeof(arr) / sizeof(arr[0]);
printf("Original array:\n");
printArray(arr, n);
radixSort(arr, n);
printf("Sorted array:\n");
printArray(arr, n);
return 0;
}

Signature
/* Create a Fibonacci series using function */

#include <stdio.h>
void generateFibonacci(int terms) {
int a = 0, b = 1, next;
if (terms >= 1) printf("%d ", a);
if (terms >= 2) printf("%d ", b);
for (int i = 3; i <= terms; i++) {
next = a + b;
printf("%d ", next);
a = b;
b = next;
}
printf("\n");
}
int main() {
int n;
printf("Enter the number of terms for Fibonacci series: ");
scanf("%d", &n);
if (n <= 0) printf("Please enter a positive integer.\n");
else {
printf("Fibonacci series up to %d terms: ", n);
generateFibonacci(n);
}
return 0;
}

Signature
/* Implementation of graph using array */

#include <stdio.h>
#define MAX_VERTICES 10
void createGraph(int graph[MAX_VERTICES][MAX_VERTICES], int vertices) {
int i, j;
int edges;
printf("Enter the number of edges: ");
scanf("%d", &edges);
for (i = 0; i < vertices; i++) {
for (j = 0; j < vertices; j++) {
graph[i][j] = 0;
}
}
printf("Enter the edges (vertex pairs): \n");
for (i = 0; i < edges; i++) {
int u, v;
printf("Edge %d: ", i + 1);
scanf("%d %d", &u, &v);
graph[u][v] = 1;
graph[v][u] = 1;
}
}
void displayGraph(int graph[MAX_VERTICES][MAX_VERTICES], int vertices) {
printf("\nAdjacency Matrix Representation of the Graph:\n");
for (int i = 0; i < vertices; i++) {
for (int j = 0; j < vertices; j++) {
printf("%d ", graph[i][j]);
}
printf("\n");
}
}
int main() {
int graph[MAX_VERTICES][MAX_VERTICES];
int vertices;
printf("Enter the number of vertices in the graph: ");
scanf("%d", &vertices);
createGraph(graph, vertices);
displayGraph(graph, vertices);
return 0;
}

Signature
/* Implement of Recursive function */

#include <stdio.h>
int fibonacci(int n) {
if (n == 0) {
return 0;
} else if (n == 1) {
return 1;
} else {
return fibonacci(n - 1) + fibonacci(n - 2);
}
}
int main() {
int n;
printf("Enter a number to find its Fibonacci number: ");
scanf("%d", &n);
if (n < 0) {
printf("Fibonacci is not defined for negative numbers.\n");
} else {
int result = fibonacci(n);
printf("The Fibonacci number at position %d is %d\n", n, result);
}
return 0;
}

Signature
/* Implementation of Linked List Using array */

#include <stdio.h>
int factorial(int n) {
if (n == 0 || n == 1) {
return 1;
} else {
return n * factorial(n - 1);
}
}
int main() {
int number;
printf("Enter a number to find its factorial: ");
scanf("%d", &number);
if (number < 0) {
printf("Factorial is not defined for negative numbers.\n");
} else {
int result = factorial(number);
printf("The factorial of %d is %d\n", number, result);
}
return 0;
}

Signature
/* Creation and traversal of binary search tree */
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* left;
struct Node* right;
};
struct Node* newNode(int data) {
struct Node* node = (struct Node*)malloc(sizeof(struct Node));
node->data = data;
node->left = node->right = NULL;
return node;
}
struct Node* insert(struct Node* root, int data) {
if (root == NULL) {
return newNode(data);
}
if (data < root->data) {
root->left = insert(root->left, data);
} else if (data > root->data) {
root->right = insert(root->right, data);
}
return root;
}
void inorderTraversal(struct Node* root) {
if (root != NULL) {
inorderTraversal(root->left);
printf("%d ", root->data);
inorderTraversal(root->right);
}
}
int main() {
struct Node* root = NULL;
int n, data;
printf("Enter the number of nodes in the BST: ");
scanf("%d", &n);
for (int i = 0; i < n; i++) {
printf("Enter value for node %d: ", i + 1);
scanf("%d", &data);
root = insert(root, data);
}
printf("\nIn-order traversal of the BST: ");
inorderTraversal(root);
return 0;
}

Signature
/* Implementation of stack using array */

#include <stdio.h>
#include <stdlib.h>
#define MAX 5
struct Stack {
int arr[MAX];
int top;
};
void initStack(struct Stack* stack) {
stack->top = -1;
}
int isFull(struct Stack* stack) {
return stack->top == MAX - 1;
}
int isEmpty(struct Stack* stack) {
return stack->top == -1;
}
void push(struct Stack* stack, int value) {
if (isFull(stack)) {
printf("Stack overflow! Cannot push %d\n", value);
} else {
stack->arr[++stack->top] = value;
printf("Pushed %d onto the stack\n", value);
}
}
int pop(struct Stack* stack) {
if (isEmpty(stack)) {
printf("Stack underflow! No element to pop.\n");
return -1;
} else {
int poppedValue = stack->arr[stack->top--];
printf("Popped %d from the stack\n", poppedValue);
return poppedValue;
}
}
int peek(struct Stack* stack) {
if (isEmpty(stack)) {
printf("Stack is empty! No element to peek.\n");
return -1;
} else {
return stack->arr[stack->top];
}
}
void display(struct Stack* stack) {
if (isEmpty(stack)) {
printf("Stack is empty!\n");
} else {
printf("Stack elements: ");
for (int i = 0; i <= stack->top; i++) {
printf("%d ", stack->arr[i]);
}
printf("\n");
}
}
int main() {
struct Stack stack;
initStack(&stack);
int choice, value;
do {
printf("\nStack Operations:\n");
printf("1. Push\n");
printf("2. Pop\n");
printf("3. Peek\n");
printf("4. Display\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value to push: ");
scanf("%d", &value);
push(&stack, value);
break;
case 2:
pop(&stack);
break;
case 3:
value = peek(&stack);
if (value != -1) {
printf("Top element is %d\n", value);
}
break;
case 4:
display(&stack);
break;
case 5:
printf("Exiting...\n");
break;
default:
printf("Invalid choice! Please try again.\n");
}
} while (choice != 5);
return 0;
}

Signature
/* Implement of two string combination */

#include <stdio.h>
#include <string.h>
void combineStrings(char str1[], char str2[], char result[]) {
strcpy(result, str1);
strcat(result, str2);
}
int main() {
char str1[100], str2[100], result[200];
printf("Enter the first string: ");
fgets(str1, sizeof(str1), stdin);
str1[strcspn(str1, "\n")] = '\0';
printf("Enter the second string: ");
fgets(str2, sizeof(str2), stdin);
str2[strcspn(str2, "\n")] = '\0';
combineStrings(str1, str2, result);
printf("Combined String: %s\n", result);
return 0;
}

Signature

You might also like