0% found this document useful (0 votes)
32 views12 pages

Anuj DAA

The document contains 6 programming problems to implement sorting algorithms in C like insertion sort, selection sort, merge sort, bubble sort, quick sort and heap sort. For each problem, the full code for the algorithm is given along with sample input/output.

Uploaded by

Jitendra Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views12 pages

Anuj DAA

The document contains 6 programming problems to implement sorting algorithms in C like insertion sort, selection sort, merge sort, bubble sort, quick sort and heap sort. For each problem, the full code for the algorithm is given along with sample input/output.

Uploaded by

Jitendra Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Experiment No.

& Name

Page No.: Date:

Lab Experiment No.1


Problem:
Write a program in C for insertion sort.
Program-
#include <stdio.h>
#include <conio.h>

void Insert_sort(int A[10], int n);

int main()
{
int i, n, A[10];
printf("NAME: ANUJ VERMA\n");
printf("ROLLNO. : 2207191539002 \n\n");
printf("\n\t\t Insertion Sort");
printf("\n How many elements are there?");
scanf("%d", &n);
printf("\n Enter the elements\n");
for (i = 0; i < n; i++)
scanf("%d", &A[i]);
Insert_sort(A, n);
getch();
return 0;
}

void Insert_sort(int A[10], int n)


{
int i, j, temp;
for (i = 1; i <= n - 1; i++)
{
temp = A[i];
j = i - 1;
while ((j >= 0) && (A[j] > temp))
{
A[j + 1] = A[j];
j = j - 1;
}
A[j + 1] = temp;
}
printf("\n The sorted list of elements is...\n");
for (i = 0; i < n; i++)
printf("\n%d", A[i]);
}

Signature of Faculty with Date


Experiment No. & Name

Page No.: Date:

OUTPUT:

Signature of Faculty with Date


Experiment No. & Name

Page No.: Date:

Lab Experiment No.2


Problem:
Write a program in C for selection sort.
Program-
#include <stdio.h>
#include <conio.h>

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

int main()
{
int i, n;
clrscr();
printf("Name=ANUJ VERMA\n");
printf("Rollno=2207191539002\n");
printf("\n\t\t Selection Sort\n");
printf("\nEnter the size of array:");
scanf("%d", &n);
int arr[n];
printf("\nEnter the elements:\n");
for (i = 0; i < n; i++)
scanf("%d", &arr[i]);
selectionSort(arr, n);
getch();
return 0;
}

void selectionSort(int arr[], int n)


{
int i, j, min_idx, temp;
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;
temp = arr[min_idx];
arr[min_idx] = arr[i];
arr[i] = temp;
}
printf("\nThe sorted array by selection sort : \n");
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
}

Signature of Faculty with Date


Experiment No. & Name

Page No.: Date:

OUTPUT:

Signature of Faculty with Date


Experiment No. & Name

Page No.: Date:

Lab Experiment No.3


Problem:
Write a program in C for merge sort.
Program-
#include<stdio.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++;
}
}

void mergeSort(int arr[], int l, int r)


{
if (l < r) {
int m = l + (r - l) / 2;

Signature of Faculty with Date


Experiment No. & Name

Page No.: Date:

mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);
merge(arr, l, m, r);
}
}

int main(){
printf("Name=ANUJ VERMA");
printf("\nRollno=2207191539002");
int n;
printf("\nEnter the size of array:");
scanf("%d", &n);
int arr[n];
for(int i=0; i<n; i++){
scanf("%d", &arr[i]);
}
mergeSort(arr, 0, n-1);

printf("The sorted array by merge sort : \n");


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

return 0;
}
Output:

Signature of Faculty with Date


Experiment No. & Name

Page No.: Date:

Lab Experiment No.4


Problem:
Write a program in C for Bubble sort.
Program-

#include <stdio.h>

void bubbleSort(int arr[], int n);

int main()
{
int n, i;
printf("Name=ANUJ VERMA\n");
printf("Rollno=2207191539002\n");
printf("\n\t\t Bubble Sort\n");
printf("Enter number of elements:");
scanf("%d", &n);
int array[n];
printf("Enter the elements:\n");
for (i = 0; i < n; i++)
scanf("%d", &array[i]);
bubbleSort(array, n);
return 0;
}

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;
}
}
}
printf("Sorted array by bubble sort:\n");
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
}

Signature of Faculty with Date


Experiment No. & Name

Page No.: Date:

Output:

Signature of Faculty with Date


Experiment No. & Name

Page No.: Date:

Lab Experiment No.5


Problem:
Write a program in C for quick sort.
Program-
#include <stdio.h>

void swap(int *a, int *b);

int partition(int array[], int low, int high) {


int pivot = array[high];
int i = low - 1;

for (int j = low; j <= high - 1; j++) {


if (array[j] <= pivot) {
i++;
swap(&array[i], &array[j]);
}
}
swap(&array[i + 1], &array[high]);
return (i + 1);
}

void quickSort(int array[], int low, int high) {


if (low < high) {
int pi = partition(array, low, high);
quickSort(array, low, pi - 1);
quickSort(array, pi + 1, high);
}
}

int main(){
printf("Name=ANUJ VERMA\n");
printf("Rollno=2207191539002\n");
int n;
printf("Enter the size of array:");
scanf("%d", &n);
int arr[n];
printf("Enter the elements:\n");
for(int i=0; i<n; i++){
scanf("%d", &arr[i]);
}
quickSort(arr, 0, n-1);

Signature of Faculty with Date


Experiment No. & Name

Page No.: Date:

printf("The sorted array by quick sort : \n");


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

return 0;
}

void swap(int *a, int *b) {


int temp = *a;
*a = *b;
*b = temp;
}

Output:

Signature of Faculty with Date


Experiment No. & Name

Page No.: Date:

Lab Experiment No.6


Problem:
Write a program in C for heap sort.
Program-
#include <stdio.h>

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) {
int temp = arr[i];
arr[i] = arr[largest];
arr[largest] = temp;
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--) {


int temp = arr[0];
arr[0] = arr[i];
arr[i] = temp;
heapify(arr, i, 0);
}
}

int main()
{
printf("Name=ANUJ VERMA\n");
printf("Rollno=2207191539002\n");
int n;
printf("Enter the size of array:");
scanf("%d", &n);

Signature of Faculty with Date


Experiment No. & Name

Page No.: Date:

int arr[n];
printf("Enter the elements:\n");
for (int i = 0; i < n; i++)
scanf("%d", &arr[i]);
heapSort(arr, n);
printf("The sorted array by heap sort : \n");
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
return 0;
}

Output:

Signature of Faculty with Date

You might also like