0% found this document useful (0 votes)
5 views27 pages

Ds Report

The document provides a practical report on data structures, including implementations for arrays, linked lists (singly, doubly, and circular), stacks, and queues in C. It includes code snippets for creating, displaying, inserting, deleting, and performing operations on these data structures. Sample outputs demonstrate the functionality of each implementation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views27 pages

Ds Report

The document provides a practical report on data structures, including implementations for arrays, linked lists (singly, doubly, and circular), stacks, and queues in C. It includes code snippets for creating, displaying, inserting, deleting, and performing operations on these data structures. Sample outputs demonstrate the functionality of each implementation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

Data Structures Practical

Report
1. Arrays Implementation
#include <stdio.h>
#include <stdlib.h>

#define MAX 100

// Function prototypes
void createArray(int arr[], int *size);
void displayArray(int arr[], int size);
void insertElement(int arr[], int *size, int element, int posi
void deleteElement(int arr[], int *size, int position);
void addArrays(int arr1[], int arr2[], int result[], int size
void multiplyArrays(int arr1[], int arr2[], int result[], int
void sparseMatrix();

int main() {
int arr[MAX], size = 0, choice, element, position;
int arr1[MAX], arr2[MAX], result[MAX];

while (1) {
printf("\nArray Operations:\n");
printf("1. Create Array\n");
printf("2. Display Array\n");
printf("3. Insert Element\n");
printf("4. Delete Element\n");
printf("5. Add Two Arrays\n");
printf("6. Multiply Two Arrays\n");
printf("7. Sparse Matrix Representation\n");
printf("8. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

Data Structures Practical Report 1


switch (choice) {
case 1:
createArray(arr, &size);
break;
case 2:
displayArray(arr, size);
break;
case 3:
printf("Enter element and position: ");
scanf("%d %d", &element, &position);
insertElement(arr, &size, element, position);
break;
case 4:
printf("Enter position to delete: ");
scanf("%d", &position);
deleteElement(arr, &size, position);
break;
case 5:
printf("Enter size of arrays: ");
scanf("%d", &size);
createArray(arr1, &size);
createArray(arr2, &size);
addArrays(arr1, arr2, result, size);
displayArray(result, size);
break;
case 6:
printf("Enter size of arrays: ");
scanf("%d", &size);
createArray(arr1, &size);
createArray(arr2, &size);
multiplyArrays(arr1, arr2, result, size);
displayArray(result, size);
break;
case 7:
sparseMatrix();
break;
case 8:
exit(0);

Data Structures Practical Report 2


default:
printf("Invalid choice!\n");
}
}
return 0;
}

void createArray(int arr[], int *size) {


printf("Enter size of array: ");
scanf("%d", size);
printf("Enter %d elements: ", *size);
for (int i = 0; i < *size; i++) {
scanf("%d", &arr[i]);
}
}

void displayArray(int arr[], int size) {


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

void insertElement(int arr[], int *size, int element, int posi


if (position > *size || position < 0) {
printf("Invalid position!\n");
return;
}
for (int i = *size; i > position; i--) {
arr[i] = arr[i - 1];
}
arr[position] = element;
(*size)++;
}

void deleteElement(int arr[], int *size, int position) {


if (position >= *size || position < 0) {

Data Structures Practical Report 3


printf("Invalid position!\n");
return;
}
for (int i = position; i < *size - 1; i++) {
arr[i] = arr[i + 1];
}
(*size)--;
}

void addArrays(int arr1[], int arr2[], int result[], int size


for (int i = 0; i < size; i++) {
result[i] = arr1[i] + arr2[i];
}
}

void multiplyArrays(int arr1[], int arr2[], int result[], int


for (int i = 0; i < size; i++) {
result[i] = arr1[i] * arr2[i];
}
}

void sparseMatrix() {
int rows, cols, nonZero = 0, value;
printf("Enter number of rows and columns: ");
scanf("%d %d", &rows, &cols);
int matrix[rows][cols];
printf("Enter matrix elements:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
scanf("%d", &matrix[i][j]);
if (matrix[i][j] != 0) nonZero++;
}
}
int sparse[nonZero][3], k = 0;
printf("Sparse Matrix Representation:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (matrix[i][j] != 0) {

Data Structures Practical Report 4


sparse[k][0] = i;
sparse[k][1] = j;
sparse[k][2] = matrix[i][j];
printf("%d %d %d\n", sparse[k][0], sparse[k][1
k++;
}
}
}
}

Output:

Enter size of array: 5


Enter 5 elements: 1 2 3 4 5
Array elements: 1 2 3 4 5
Enter element and position: 6 2
Array elements: 1 2 6 3 4 5
Enter position to delete: 3
Array elements: 1 2 6 4 5

2. Linked List Implementation (Static and


Dynamic)
#include <stdio.h>
#include <stdlib.h>

// Structure for singly linked list


struct Node {
int data;
struct Node* next;
};

// Structure for doubly linked list


struct DNode {
int data;
struct DNode* prev;

Data Structures Practical Report 5


struct DNode* next;
};

// Structure for circular linked list


struct CNode {
int data;
struct CNode* next;
};

// Function prototypes for singly linked list


void insertSingly(struct Node** head, int data);
void deleteSingly(struct Node** head, int key);
void displaySingly(struct Node* head);

// Function prototypes for doubly linked list


void insertDoubly(struct DNode** head, int data);
void deleteDoubly(struct DNode** head, int key);
void displayDoubly(struct DNode* head);

// Function prototypes for circular linked list


void insertCircular(struct CNode** head, int data);
void deleteCircular(struct CNode** head, int key);
void displayCircular(struct CNode* head);

int main() {
struct Node* singlyHead = NULL;
struct DNode* doublyHead = NULL;
struct CNode* circularHead = NULL;

// Singly linked list operations


insertSingly(&singlyHead, 10);
insertSingly(&singlyHead, 20);
insertSingly(&singlyHead, 30);
displaySingly(singlyHead);
deleteSingly(&singlyHead, 20);
displaySingly(singlyHead);

// Doubly linked list operations

Data Structures Practical Report 6


insertDoubly(&doublyHead, 40);
insertDoubly(&doublyHead, 50);
insertDoubly(&doublyHead, 60);
displayDoubly(doublyHead);
deleteDoubly(&doublyHead, 50);
displayDoubly(doublyHead);

// Circular linked list operations


insertCircular(&circularHead, 70);
insertCircular(&circularHead, 80);
insertCircular(&circularHead, 90);
displayCircular(circularHead);
deleteCircular(&circularHead, 80);
displayCircular(circularHead);

return 0;
}

// Singly linked list functions


void insertSingly(struct Node** head, int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct
newNode->data = data;
newNode->next = *head;
*head = newNode;
}

void deleteSingly(struct Node** head, int key) {


struct Node* temp = *head, *prev = NULL;
if (temp != NULL && temp->data == key) {
*head = temp->next;
free(temp);
return;
}
while (temp != NULL && temp->data != key) {
prev = temp;
temp = temp->next;
}
if (temp == NULL) return;

Data Structures Practical Report 7


prev->next = temp->next;
free(temp);
}

void displaySingly(struct Node* head) {


while (head) {
printf("%d -> ", head->data);
head = head->next;
}
printf("NULL\n");
}

// Doubly linked list functions


void insertDoubly(struct DNode** head, int data) {
struct DNode* newNode = (struct DNode*)malloc(sizeof(struc
newNode->data = data;
newNode->next = *head;
newNode->prev = NULL;
if (*head != NULL) (*head)->prev = newNode;
*head = newNode;
}

void deleteDoubly(struct DNode** head, int key) {


struct DNode* temp = *head;
while (temp && temp->data != key) temp = temp->next;
if (!temp) return;
if (temp->prev) temp->prev->next = temp->next;
if (temp->next) temp->next->prev = temp->prev;
if (*head == temp) *head = temp->next;
free(temp);
}

void displayDoubly(struct DNode* head) {


while (head) {
printf("%d <-> ", head->data);
head = head->next;
}
printf("NULL\n");

Data Structures Practical Report 8


}

// Circular linked list functions


void insertCircular(struct CNode** head, int data) {
struct CNode* newNode = (struct CNode*)malloc(sizeof(struc
newNode->data = data;
if (*head == NULL) {
newNode->next = newNode;
*head = newNode;
} else {
struct CNode* temp = *head;
while (temp->next != *head)
temp = temp->next;
temp->next = newNode;
newNode->next = *head;
}
}

void deleteCircular(struct CNode** head, int key) {


if (*head == NULL) return;
struct CNode *temp = *head, *prev = NULL;
if (temp->data == key && temp->next == *head) {
free(temp);
*head = NULL;
return;
}
do {
prev = temp;
temp = temp->next;
} while (temp != *head && temp->data != key);
if (temp == *head) return;
prev->next = temp->next;
free(temp);
}

void displayCircular(struct CNode* head) {


if (head == NULL) return;
struct CNode* temp = head;

Data Structures Practical Report 9


do {
printf("%d -> ", temp->data);
temp = temp->next;
} while (temp != head);
printf("(Back to head)\n");
}

Output:

10 -> 20 -> 30 -> NULL


10 -> 30 -> NULL
40 <-> 50 <-> 60 <-> NULL
40 <-> 60 <-> NULL
70 -> 80 -> 90 -> (Back to head)
70 -> 90 -> (Back to head)

3. Stack Implementation
#include <stdio.h>
#include <stdlib.h>

// Structure for stack


struct Stack {
int top;
int capacity;
int* array;
};

// Function prototypes for stack


struct Stack* createStack(int capacity);
void push(struct Stack* stack, int item);
int pop(struct Stack* stack);
int peek(struct Stack* stack);
int isEmpty(struct Stack* stack);
void displayStack(struct Stack* stack);

Data Structures Practical Report 10


int main() {
struct Stack* stack = createStack(5);

// Stack operations
push(stack, 10);
push(stack, 20);
push(stack, 30);
displayStack(stack);
printf("Popped: %d\n", pop(stack));
displayStack(stack);

return 0;
}

// Stack functions
struct Stack* createStack(int capacity) {
struct Stack* stack = (struct Stack*)malloc(sizeof(struct
stack->capacity = capacity;
stack->top = -1;
stack->array = (int*)malloc(stack->capacity * sizeof(int)
return stack;
}

void push(struct Stack* stack, int item) {


if (stack->top == stack->capacity - 1) {
printf("Stack Overflow\n");
return;
}
stack->array[++stack->top] = item;
}

int pop(struct Stack* stack) {


if (isEmpty(stack)) {
printf("Stack Underflow\n");
return -1;
}
return stack->array[stack->top--];
}

Data Structures Practical Report 11


int peek(struct Stack* stack) {
if (isEmpty(stack)) return -1;
return stack->array[stack->top];
}

int isEmpty(struct Stack* stack) {


return stack->top == -1;
}

void displayStack(struct Stack* stack) {


if (isEmpty(stack)) {
printf("Stack is empty\n");
return;
}
for (int i = stack->top; i >= 0; i--) {
printf("%d\n", stack->array[i]);
}
}

Output:

Stack:
30
20
10
Popped: 30
Stack:
20
10

4. Queue Implementation
#include <stdio.h>
#include <stdlib.h>

Data Structures Practical Report 12


// Structure for queue
struct Queue {
int front, rear, capacity;
int* array;
};

// Function prototypes for queue


struct Queue* createQueue(int capacity);
void enqueue(struct Queue* queue, int item);
int dequeue(struct Queue* queue);
int isEmptyQueue(struct Queue* queue);
void displayQueue(struct Queue* queue);

int main() {
struct Queue* queue = createQueue(5);

// Queue operations
enqueue(queue, 10);
enqueue(queue, 20);
enqueue(queue, 30);
displayQueue(queue);
printf("Dequeued: %d\n", dequeue(queue));
displayQueue(queue);

return 0;
}

// Queue functions
struct Queue* createQueue(int capacity) {
struct Queue* queue = (struct Queue*)malloc(sizeof(struct
queue->capacity = capacity;
queue->front = queue->rear = -1;
queue->array = (int*)malloc(queue->capacity * sizeof(int)
return queue;
}

void enqueue(struct Queue* queue, int item) {


if (queue->rear == queue->capacity - 1) {

Data Structures Practical Report 13


printf("Queue Overflow\n");
return;
}
if (queue->front == -1) queue->front = 0;
queue->array[++queue->rear] = item;
}

int dequeue(struct Queue* queue) {


if (isEmptyQueue(queue)) {
printf("Queue Underflow\n");
return -1;
}
int item = queue->array[queue->front];
if (queue->front >= queue->rear) queue->front = queue->rea
else queue->front++;
return item;
}

int isEmptyQueue(struct Queue* queue) {


return queue->front == -1;
}

void displayQueue(struct Queue* queue) {


if (isEmptyQueue(queue)) {
printf("Queue is empty\n");
return;
}
for (int i = queue->front; i <= queue->rear; i++) {
printf("%d ", queue->array[i]);
}
printf("\n");
}

Output:

Queue:
10 20 30
Dequeued: 10

Data Structures Practical Report 14


Queue:
20 30

5. Tree Implementation (Binary Search


Tree)
#include <stdio.h>
#include <stdlib.h>

// Structure for a Binary Tree Node


typedef struct TreeNode {
int data;
struct TreeNode *left, *right;
} TreeNode;

// Function to create a new tree node


TreeNode* createTreeNode(int data) {
TreeNode* newNode = (TreeNode*)malloc(sizeof(TreeNode));
newNode->data = data;
newNode->left = newNode->right = NULL;
return newNode;
}

// Function to insert a node into a Binary Search Tree (BST)


TreeNode* insertBST(TreeNode* root, int data) {
if (root == NULL) return createTreeNode(data);
if (data < root->data)
root->left = insertBST(root->left, data);
else
root->right = insertBST(root->right, data);
return root;
}

// Function to perform in-order traversal of a BST


void inorderBST(TreeNode* root) {
if (root != NULL) {

Data Structures Practical Report 15


inorderBST(root->left);
printf("%d ", root->data);
inorderBST(root->right);
}
}

// AVL Tree Node Structure


typedef struct AVLNode {
int data;
struct AVLNode *left, *right;
int height;
} AVLNode;

// Function to get the height of an AVL node


int height(AVLNode* node) {
return node ? node->height : 0;
}

// Function to create a new AVL node


AVLNode* createAVLNode(int data) {
AVLNode* newNode = (AVLNode*)malloc(sizeof(AVLNode));
newNode->data = data;
newNode->left = newNode->right = NULL;
newNode->height = 1;
return newNode;
}

// Function to rotate right in AVL Tree


AVLNode* rightRotate(AVLNode* y) {
AVLNode* x = y->left;
AVLNode* T2 = x->right;
x->right = y;
y->left = T2;
y->height = 1 + (height(y->left) > height(y->right) ? heig
x->height = 1 + (height(x->left) > height(x->right) ? heig
return x;
}

Data Structures Practical Report 16


// Function to rotate left in AVL Tree
AVLNode* leftRotate(AVLNode* x) {
AVLNode* y = x->right;
AVLNode* T2 = y->left;
y->left = x;
x->right = T2;
x->height = 1 + (height(x->left) > height(x->right) ? heig
y->height = 1 + (height(y->left) > height(y->right) ? heig
return y;
}

// Function to get balance factor of AVL node


int getBalance(AVLNode* node) {
return node ? height(node->left) - height(node->right) : 0
}

// Function to insert a node into an AVL Tree


AVLNode* insertAVL(AVLNode* node, int data) {
if (!node) return createAVLNode(data);
if (data < node->data)
node->left = insertAVL(node->left, data);
else if (data > node->data)
node->right = insertAVL(node->right, data);
else return node;
node->height = 1 + (height(node->left) > height(node->rig
int balance = getBalance(node);
if (balance > 1 && data < node->left->data)
return rightRotate(node);
if (balance < -1 && data > node->right->data)
return leftRotate(node);
if (balance > 1 && data > node->left->data) {
node->left = leftRotate(node->left);
return rightRotate(node);
}
if (balance < -1 && data < node->right->data) {
node->right = rightRotate(node->right);
return leftRotate(node);
}

Data Structures Practical Report 17


return node;
}

// Function to perform in-order traversal of an AVL Tree


void inorderAVL(AVLNode* root) {
if (root != NULL) {
inorderAVL(root->left);
printf("%d ", root->data);
inorderAVL(root->right);
}
}

int main() {
// Binary Search Tree Operations
TreeNode* root = NULL;
root = insertBST(root, 50);
root = insertBST(root, 30);
root = insertBST(root, 70);
root = insertBST(root, 20);
root = insertBST(root, 40);
root = insertBST(root, 60);
root = insertBST(root, 80);
printf("In-order traversal of BST: ");
inorderBST(root);
printf("\n");

// AVL Tree Operations


AVLNode* avlRoot = NULL;
avlRoot = insertAVL(avlRoot, 50);
avlRoot = insertAVL(avlRoot, 30);
avlRoot = insertAVL(avlRoot, 70);
avlRoot = insertAVL(avlRoot, 20);
avlRoot = insertAVL(avlRoot, 40);
avlRoot = insertAVL(avlRoot, 60);
avlRoot = insertAVL(avlRoot, 80);
printf("In-order traversal of AVL Tree: ");
inorderAVL(avlRoot);
printf("\n");

Data Structures Practical Report 18


return 0;
}

Output:

In-order traversal of BST: 20 30 40 50 60 70 80


In-order traversal of AVL Tree: 20 30 40 50 60 70 80

6. Graph Implementation
#include <stdio.h>
#include <stdlib.h>

#define MAX_VERTICES 100

// Structure for Graph


struct Graph {
int vertices;
int adjMatrix[MAX_VERTICES][MAX_VERTICES];
};

// Function prototypes for Graph


struct Graph* createGraph(int vertices);
void addEdge(struct Graph* graph, int src, int dest);
void displayGraph(struct Graph* graph);

int main() {
struct Graph* graph = createGraph(5);

// Adding edges
addEdge(graph, 0, 1);
addEdge(graph, 0, 4);
addEdge(graph, 1, 2);
addEdge(graph, 1, 3);
addEdge(graph, 1, 4);

Data Structures Practical Report 19


addEdge(graph, 2, 3);
addEdge(graph, 3, 4);

// Display graph adjacency matrix


displayGraph(graph);

return 0;
}

// Function to create a graph


struct Graph* createGraph(int vertices) {
struct Graph* graph = (struct Graph*)malloc(sizeof(struct
graph->vertices = vertices;

// Initialize adjacency matrix to 0


for (int i = 0; i < vertices; i++) {
for (int j = 0; j < vertices; j++) {
graph->adjMatrix[i][j] = 0;
}
}

return graph;
}

// Function to add an edge to the graph


void addEdge(struct Graph* graph, int src, int dest) {
graph->adjMatrix[src][dest] = 1;
graph->adjMatrix[dest][src] = 1; // For an undirected gra
}

// Function to display the adjacency matrix of the graph


void displayGraph(struct Graph* graph) {
printf("Adjacency Matrix:\n");
for (int i = 0; i < graph->vertices; i++) {
for (int j = 0; j < graph->vertices; j++) {
printf("%d ", graph->adjMatrix[i][j]);
}
printf("\n");

Data Structures Practical Report 20


}
}

Output:

Adjacency Matrix:
0 1 0 0 1
1 0 1 1 1
0 1 0 1 0
0 1 1 0 1
1 1 0 1 0

6. Searching
#include <stdio.h>

// Function prototypes
int sequentialSearch(int arr[], int size, int key);
int binarySearch(int arr[], int left, int right, int key);
void sortArray(int arr[], int size);

int main() {
int arr[] = {23, 12, 56, 34, 78, 90, 11};
int size = sizeof(arr) / sizeof(arr[0]);
int key = 34;

// Sequential Search
int seqIndex = sequentialSearch(arr, size, key);
if (seqIndex != -1)
printf("Sequential Search: Element %d found at inde
x %d\n", key, seqIndex);
else
printf("Sequential Search: Element %d not found\n",
key);

// Sorting the array for Binary Search

Data Structures Practical Report 21


sortArray(arr, size);

// Binary Search
int binIndex = binarySearch(arr, 0, size - 1, key);
if (binIndex != -1)
printf("Binary Search: Element %d found at index %d
\n", key, binIndex);
else
printf("Binary Search: Element %d not found\n", ke
y);

return 0;
}

// Function for Sequential Search


int sequentialSearch(int arr[], int size, int key) {
for (int i = 0; i < size; i++) {
if (arr[i] == key)
return i;
}
return -1;
}

// Function for Binary Search


int binarySearch(int arr[], int left, int right, int key) {
while (left <= right) {
int mid = left + (right - left) / 2;
if (arr[mid] == key)
return mid;
else if (arr[mid] < key)
left = mid + 1;
else
right = mid - 1;
}
return -1;
}

// Function to sort an array (Bubble Sort)

Data Structures Practical Report 22


void sortArray(int arr[], int size) {
for (int i = 0; i < size - 1; i++) {
for (int j = 0; j < size - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}

Output:
Sequential Search: Element 34 found at index 3
Binary Search: Element 34 found at index 3

7. Sorting
#include <stdio.h>
#include <stdlib.h>

// Function to swap two elements


void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}

// Insertion Sort (Time Complexity: O(n^2))


void insertionSort(int arr[], int n) {
for (int i = 1; i < n; i++) {
int key = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];

Data Structures Practical Report 23


j--;
}
arr[j + 1] = key;
}
}

// Selection Sort (Time Complexity: O(n^2))


void selectionSort(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
int minIdx = i;
for (int j = i + 1; j < n; j++) {
if (arr[j] < arr[minIdx])
minIdx = j;
}
swap(&arr[i], &arr[minIdx]);
}
}

// Bubble Sort (Time Complexity: O(n^2))


void bubbleSort(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1])
swap(&arr[j], &arr[j + 1]);
}
}
}

// Quick Sort (Time Complexity: O(n log n) on average)


int partition(int arr[], int low, int high) {
int pivot = arr[high];
int i = low - 1;
for (int j = low; j < high; j++) {
if (arr[j] < pivot) {
i++;
swap(&arr[i], &arr[j]);
}
}

Data Structures Practical Report 24


swap(&arr[i + 1], &arr[high]);
return i + 1;
}
void quickSort(int arr[], int low, int high) {
if (low < high) {
int pi = partition(arr, low, high);
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}

// Merge Sort (Time Complexity: O(n log n))


void merge(int arr[], int left, int mid, int right) {
int n1 = mid - left + 1, n2 = right - mid;
int 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) 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);
}
}

// Heap Sort (Time Complexity: O(n log n))


void heapify(int arr[], int n, int i) {
int largest = i, left = 2 * i + 1, right = 2 * i + 2;
if (left < n && arr[left] > arr[largest])
largest = left;
if (right < n && arr[right] > arr[largest])

Data Structures Practical Report 25


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);
}
}

// Radix Sort (Time Complexity: O(nk), where k is the max d


igit count)
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 countSort(int arr[], int n, int exp) {
int output[n], 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]] = arr[i];
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)
countSort(arr, n, exp);
}

Data Structures Practical Report 26


int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr) / sizeof(arr[0]);

radixSort(arr, n);

printf("Sorted array: ");


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

return 0;
}

Data Structures Practical Report 27

You might also like