0% found this document useful (0 votes)
16 views3 pages

Daa 1

The document contains three programming problems implemented in C: Bubble Sort, Insertion Sort, and Selection Sort using recursion. Each problem includes the code for the sorting algorithm and a main function to demonstrate sorting an example array. Additionally, the document includes student identifiers for Sahil Bisht and Srishti Chauhan.

Uploaded by

asteroidmusic05
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)
16 views3 pages

Daa 1

The document contains three programming problems implemented in C: Bubble Sort, Insertion Sort, and Selection Sort using recursion. Each problem includes the code for the sorting algorithm and a main function to demonstrate sorting an example array. Additionally, the document includes student identifiers for Sahil Bisht and Srishti Chauhan.

Uploaded by

asteroidmusic05
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/ 3

Name:Sahil Bisht

Section:D1
Roll No.:46

Problem Statement 8 : Write a program to implement Bubble Sort technique in the array by using
recursion.

Code:
#include <stdio.h>
void bubbleSort(int arr[], int n)
{
if (n == 1) {
return;
}
for (int i = 0; i < n - 1; i++) {
if (arr[i] > arr[i + 1]) {
int temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
}
}
bubbleSort(arr, n - 1);
}
void printArray(int arr[], int n) {
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
int main(){
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr) / sizeof(arr[0]);
printf("Original array: ");
printArray(arr, n);
bubbleSort(arr, n);
printf("Sorted array: ");
printArray(arr, n);
return 0;
}

OUTPUT:
Name:Sahil Bisht
Section:D1
Roll No.:46

Problem Statement 9: Write a program to implement Insertion Sort technique in the array by using
recursion.

Code:

#include <stdio.h>
void insertionSortRecursive(int arr[], int n)
{
if (n <= 1)
return;
insertionSortRecursive(arr, n - 1);
int last = arr[n - 1];
int j = n - 2;
while (j >= 0 && arr[j] > last) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = last;
}
void printArray(int arr[], int n) {
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
int main() {
int arr[] = {12, 11, 13, 5, 6};
int n = sizeof(arr) / sizeof(arr[0]);
printf("Original array: \n");
printArray(arr, n);
insertionSortRecursive(arr, n);
printf("Sorted array: \n");
printArray(arr, n);
return 0;
}

OUTPUT:
Name:Sahil Bisht
Section:D1
Roll No.:46

Problem Statement 10 : Write a program to implement Selection Sort technique in the array by using
recursion.
Code:

#include <stdio.h>
int findMinIndex(int arr[], int i, int j) {
if (i == j)
return i;
int k = findMinIndex(arr, i + 1, j);
return (arr[i] < arr[k]) ? i : k;
}
void selectionSortRecursive(int arr[], int n, int index) {
if (index == n)
return;
int minIndex = findMinIndex(arr, index, n - 1);
if (minIndex != index) {
int temp = arr[minIndex];
arr[minIndex] = arr[index];
arr[index] = temp; }
selectionSortRecursive(arr, n, index + 1);}
void printArray(int arr[], int n) {
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]); }
printf("\n");}
int main() {
int arr[] = {64, 25, 12, 22, 11};
int n = sizeof(arr) / sizeof(arr[0]);
printf("Original array: ");
printArray(arr, n);
selectionSortRecursive(arr, n, 0);
printf("Sorted array: ");
printArray(arr, n);
printf("Name-Srishti Chauhan\nRoll No.-59");
return 0;
}

OUTPUT:

You might also like