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

Insertion

Uploaded by

Tanvi Shinde
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 views2 pages

Insertion

Uploaded by

Tanvi Shinde
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/ 2

Name: Saheranjum M.

Makandar
Roll No: 72
Class: B-Tech SY DS
Batch: C4

Q1. Write a program to sort an array using insertion sort.

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

int main() {
int arr[10];
int n = sizeof(arr) / sizeof(arr[0]);

printf("Enter 10 elements:\n");
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
Name: Saheranjum M. Makandar
Roll No: 72
Class: B-Tech SY DS
Batch: C4

insertionSort(arr, n);

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

return 0;
}

Output:

You might also like