Nishal 24mid0281 Dsa
Nishal 24mid0281 Dsa
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;
1;
arr[j + 1] = arr[j]; j = j - 1;
arr[j + 1] = key;
minIdx = j;
temp = arr[minIdx];
arr[minIdx] = arr[i];
arr[i] = temp;
{
int i, j, temp; for (i = 0; i < n
1];
arr[j + 1] = temp;
}}
printf("\n");
} int main()
int n;
arr[i];
}
insertionSort(arr, n);
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:
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.
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.
15. If the key is smaller, move to the left child; if larger, move to the right child.
Code:
#include <stdio.h>
#include <stdlib.h>
struct Node {
Node* right;
};
>left, data);
else if (data > root->data) root-
root;
if (root != NULL) {
inorder(root->left); printf("%d
>right);
return current;
>right, key);
free(root);
if (root->right == NULL)
{ struct Node*
temp = root->left;
free(root); return
temp;
deleteNode(root->right, temp->data);
return root;
>right, key);
elements[] = {30, 10, 70, 60, 80, 25, 18, 27, 23};
elements[i]);
inorder(root); printf("\n");
inorder(root); printf("\n");
struct Node* result = search(root, 15); if
else
BST.\n");
BST.\n");
return 0;
Output: