0% found this document useful (0 votes)
12 views8 pages

Nishal 24mid0281 Dsa

The document contains a C programming assignment focused on sorting algorithms and binary search trees. It includes detailed algorithms and code for insertion sort, selection sort, and bubble sort, as well as for inserting, deleting, and searching in a binary search tree. The assignment also provides sample outputs for the implemented programs.

Uploaded by

nishalvenkatesan
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)
12 views8 pages

Nishal 24mid0281 Dsa

The document contains a C programming assignment focused on sorting algorithms and binary search trees. It includes detailed algorithms and code for insertion sort, selection sort, and bubble sort, as well as for inserting, deleting, and searching in a binary search tree. The assignment also provides sample outputs for the implemented programs.

Uploaded by

nishalvenkatesan
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/ 8

DATA STRUCTURES AND ALGORITHM ANALYSIS

ASSESSMENT 4

NAME:NISHAL V
REG NO: 24MID0281
SLOT: L43+L44
FACULTY: NITHYA NS

1.Write a C program to sort the following numbers using insertion, selection and bubble sort.

6 5 3 1 8 7 2.

ALGORITHM:
1. For Insertion Sort, start from the second element of the array.
2. Set this element as key, and compare it with the element before it.
3. Move larger elements one position to the right and place key in its correct position.
4. For Selection Sort, iterate through the array to find the smallest element in the unsorted
part.
5. Swap this smallest element with the element at the current position.
6. For Bubble Sort, compare adjacent elements and swap them if they are in the wrong order.
7. After each pass, the largest unsorted element is placed in its correct position.
8. Repeat this process for all elements until the array is sorted.
9. Insertion Sort works by progressively building the sorted part of the array.
10. Selection Sort always selects the smallest element, while Bubble Sort performs adjacent
swaps until the array is sorted.

CODE:
#include <stdio.h>
void insertionSort(int arr[], int n) {

int i, key, j;

for (i = 1; i < n; i++) {

key = arr[i]; j=i-

1;

while (j >= 0 && arr[j] > key) {

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

arr[j + 1] = key;

} void selectionSort(int arr[], int n)

{ int i, j, minIdx, temp; for (i =

0; i < n - 1; i++) { minIdx = i;

for (j = i + 1; j < n; j++) { if

(arr[j] < arr[minIdx]) {

minIdx = j;

temp = arr[minIdx];

arr[minIdx] = arr[i];

arr[i] = temp;

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

}}

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

for (int i = 0; i < n; i++) {

printf("%d ", arr[i]);

printf("\n");

} int main()

int n;

printf("Enter the number of elements: ");

scanf("%d", &n); int arr[n], arr1[n],

arr2[n]; printf("Enter the elements: ");

for (int i = 0; i < n; i++) { scanf("%d",

&arr[i]); arr1[i] = arr[i]; arr2[i] =

arr[i];
}

insertionSort(arr, n);

printf("Insertion Sort: ");

printArray(arr, n); selectionSort(arr1,

n); printf("Selection Sort: ");

printArray(arr1, n); bubbleSort(arr2,

n); printf("Bubble Sort: ");

printArray(arr2, n);

return 0;

OUTPUT:

2. Write a C program to insert 30,10,70,60,80,25,18,27,23 in to the binary search tree and display
the in- order traversal of BST and delete 30,18 from the BST and display the in-order traversal of
BST and search the element 15 and 60 from the BST.

1) Algorithm:
1. create a new node and set it as the root.
2. Otherwise, compare the key with the current node:

3. If the key is smaller, move to the left child.

4. If the key is larger, move to the right child.

5. Repeat until a NULL child is found, then insert the new node.
6. Start from the root, recursively traverse the left subtree.
7. Visit the current node and print its value.

8. Recursively traverse the right subtree to display the nodes in ascending order.

9. Locate the node to be deleted by comparing the key with the node’s data.

10. If the node has no children (leaf), remove it.

11. If the node has one child, link its parent to its child.

12. If the node has two children, replace it with its in-order successor (smallest node in the right
subtree), then delete the successor.

13. Start from the root and compare the key with the current node’s data.

14. If the key matches, the search is successful.

15. If the key is smaller, move to the left child; if larger, move to the right child.

16. Continue until the key is found or a NULL pointer is reached.

Code:

#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 inorder(struct Node* root) {

if (root != NULL) {

inorder(root->left); printf("%d

", root->data); inorder(root-

>right);

struct Node* minValueNode(struct Node* node) {

struct Node* current = node; while (current &&

current->left != NULL) current = current->left;

return current;

struct Node* deleteNode(struct Node* root, int key) {

if (root == NULL) return root; if (key < root->data)

root->left = deleteNode(root->left, key); else if (key

> root->data) root->right = deleteNode(root-

>right, key);

else { if (root->left == NULL) {

struct Node* temp = root->right;

free(root);

return temp; } else

if (root->right == NULL)

{ struct Node*

temp = root->left;
free(root); return

temp;

struct Node* temp = minValueNode(root->right);

root->data = temp->data; root->right =

deleteNode(root->right, temp->data);

return root;

struct Node* search(struct Node* root, int key) {

if (root == NULL || root->data == key)

return root; if (key < root->data) return

search(root->left, key); return search(root-

>right, key);

int main() { struct Node* root = NULL; int

elements[] = {30, 10, 70, 60, 80, 25, 18, 27, 23};

for (int i = 0; i < 9; i++) { root = insert(root,

elements[i]);

printf("In-order traversal of the BST before deletion:\n");

inorder(root); printf("\n");

root = deleteNode(root, 30);

root = deleteNode(root, 18);

printf("In-order traversal of the BST after deletion:\n");

inorder(root); printf("\n");
struct Node* result = search(root, 15); if

(result != NULL) printf("Element 15

found in the BST.\n");

else

printf("Element 15 not found in the BST.\n");

result = search(root, 60); if (result !=

NULL) printf("Element 60 found in the

BST.\n");

else printf("Element 60 not found in the

BST.\n");

return 0;

Output:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy