Daa 1
Daa 1
Daa 1
SYNTAX CODE:
#include <stdio.h>
int main() {
int array[50];
int size;
printf("Enter the elements in the array: \n");
scanf("%d",&size);
for(int i=0;i<size;i++){
scanf("%d",&array[i]);
}
for (int i = 1; i < size; i++)
{ int key = array[i];
int j = i - 1;
while (key < array[j] && j >= 0)
{ array[j + 1] = array[j];
--j;
}
array[j + 1] = key;
}
Time Complexity:
Space Complexity:
● O(1)
SYNTAX CODE:
#include <stdio.h>
void swap(int* x, int* y)
{
int temp = *x;
*x = *y;
*y = temp;
}
int main()
{
int arr[50];
int n;
printf("Enter the elements in the array: \n");
scanf("%d",&n);
for(int i=0;i<n;i++){
scanf("%d",&arr[i]);
}
int i, j, min_idx;
for (i = 0; i < n - 1; i++)
{ min_idx = i;
for (j = i + 1; j < n; j++)
if (arr[j] < arr[min_idx])
min_idx = j;
swap(&arr[min_idx], &arr[i]);
}
printf("Sorted array: \n");
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
return 0;
}
Time Complexity:
Space Complexity:
● O(1)
SYNTAX CODE:
#include <stdio.h>
void swap(int* a, int* b)
{ int t = *a;
*a = *b;
*b = t;
}
int partition(int arr[], int low, int high)
{ int pivot = arr[high];
int i = (low - 1);
OUTPUT:
Time Complexity:
Space Complexity:
● O(1)
7
EXPERIMENT -4
SYNTAX CODE:
#include <stdio.h>
void swap(int* arr, int i, int j)
{
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
void bubbleSort(int arr[], int n)
{
int i, j;
for (i = 0; i < n - 1; i++)
for (j = 0; j < n - i - 1; j++)
if (arr[j] > arr[j + 1])
swap(arr, j, j + 1);
}
int main()
{
int arr[50];
int n;
printf("Enter the elements in the array: \n");
scanf("%d",&n);
for(int i=0;i<n;i++){
scanf("%d",&arr[i]);
}
bubbleSort(arr, n);
printf("Sorted array: ");
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
return 0;
}
NAME:
NAME: DHRUV
DHRUV MISHRA
MISHRA
ROLL NO. : 2100910100068
OUTPUT:
Time Complexity:
Space Complexity:
● O(1)
9
EXPERIMENT -5
SYNTAX CODE:
#include <stdio.h>
#include <stdlib.h>
void merge(int arr[], int l, int m, int r)
{
int i, j, k;
int n1 = m - l + 1;
int n2 = r - m;
int L[n1], R[n2];
for (i = 0; i < n1; i++)
L[i] = arr[l + i];
for (j = 0; j < n2; j++)
R[j] = arr[m + 1 + j];
i = 0;j = 0;
k = l;
while (i < n1 && j < n2)
{ if (L[i] <= R[j]) {
arr[k] = L[i];
i++;
}
else {
arr[k] = R[j];
j++;
} k+
+;
}
while (i < n1) {
arr[k] = L[i]; i+
+;
k++;
}
while (j < n2) {
arr[k] = R[j];
j++;
k++;
}
NAME: DHRUV MISHRA
ROLL NO. : 2100910100068
}
void mergeSort(int arr[], int l, int r)
{
if (l < r) {
int m = l + (r - l) / 2;
mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);
merge(arr, l, m, r);
}
}
int main()
{
int arr[50];
int n;
printf("Enter the elements in the array: \n");
scanf("%d",&n);
for(int i=0;i<n;i++){
scanf("%d",&arr[i]);
}
mergeSort(arr, 0,n- 1);
OUTPUT:
1
1
EXPERIMENT -6
SYNTAX CODE:
#include <stdio.h>
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 && arr[left] > arr[largest])
largest = left;
if (right < N && 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);
}
}
int main()
heapSort(arr, n);
printf("Sorted array is\n");
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
}
OUTPUT:
Time Complexity:
Space Complexity:
● O(1)
SYNTAX CODE:
#include <stdio.h>
#include <stdlib.h>
#define NARRAY 7
#define NBUCKET 6
#define INTERVAL 10
struct Node
{ int data;
struct Node *next;
};
void BucketSort(int arr[]);
struct Node *InsertionSort(struct Node *list);
void print(int arr[]);
void printBuckets(struct Node *list);
int getBucketIndex(int value);
BucketSort(array);
printf("------------\n");
printf("Sorted array: ");
print(array);
return 0;
}
OUTPUT:
Time Complexity:
Space Complexity:
● O(N+K)
SYNTAX CODE:
#include <stdio.h>
int getMax(int array[], int n)
{ int max = array[0];
for (int i = 1; i < n; i++)
if (array[i] > max)
max = array[i];
return max;
}
void countingSort(int array[], int size, int place)
{ int output[size + 1];
int max = (array[0] / place) % 10;
OUTPUT:
Time Complexity:
Space Complexity:
● O(N+d)
SYNTAX CODE:
#include <stdio.h>
OUTPUT:
Time Complexity:
Space Complexity:
● O(N+k)
11
1
EXPERIMENT - 10
AIM: Write a program to find Kth smallest element using quick sort in C.
SYNTAX CODE:
#include <stdio.h>
void swap(int* a, int* b)
{
int temp = *a;
*a = *b;
*b = temp;
}
int partition(int A[], int l, int r)
{
int x = A[r];
int i = l-1;
for ( int j=l;j<=r-1;j++ )
{
if (A[j] <= x)
{
i = i + 1;
swap(&A[i], &A[j]);
}
}
swap(&A[i+1],& A[r]);
return i+1;
}
int kthSmallest(int A[], int left, int right, int K)
{
if (left == right)
return A[left];
int pos = partition(A, left, right);
int count = pos - left + 1;
if ( K == pos )
return A[pos];
else if ( K<pos )
kthSmallest(A, left, pos-1, K);
else
kthSmallest(A, pos+1, right, K) ;
OUTPUT:
Time Complexity:
Space Complexity:
● O(N)
23
EXPERIMENT - 11
SYNTAX CODE:
#include <stdio.h>
#include <stdlib.h>
struct node
{ int data;
struct node *right_child;
struct node *left_child;
};
struct node* new_node(int x)
{ struct node *temp;
temp = malloc(sizeof(struct node));
temp->data = x;
temp->left_child = NULL;
temp->right_child = NULL;
return temp;
}
{ if (root == NULL)
return NULL;
if (x > root->data)
root->right_child = delete(root->right_child, x);
else if (x < root->data)
root->left_child = delete(root->left_child, x);
else {
if (root->left_child == NULL && root->right_child == NULL){
free(root);
return NULL;
}
else if (root->left_child == NULL || root->right_child == NULL)
{ struct node *temp;
if (root->left_child == NULL)
temp = root->right_child;
else
temp = root->left_child;
free(root);
return temp;
}
else {
struct node *temp = find_minimum(root->right_child);
root->data = temp->data;
root->right_child = delete(root->right_child, temp->data);
}
}
return root;
}
int main() {
struct node *root;
root = new_node(20);
insert(root, 5);
insert(root, 1);
insert(root, 25);
insert(root, 40);
insert(root, 45);
insert(root, 42);
inorder(root); printf("\
n");
OUTPUT: