0% found this document useful (0 votes)
5 views

Untitled Document

The document describes an insertion sort algorithm for sorting an array of integers. It includes functions for sorting an array using insertion sort and printing the array. The main function prompts the user to enter array size and elements, calls the sorting function, and prints the sorted array.

Uploaded by

Ayush Singh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Untitled Document

The document describes an insertion sort algorithm for sorting an array of integers. It includes functions for sorting an array using insertion sort and printing the array. The main function prompts the user to enter array size and elements, calls the sorting function, and prints the sorted array.

Uploaded by

Ayush Singh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

dfvf

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

void printArray(int arr[], int n) {


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

int main() {
int x, i, target;

printf("Enter the array size: ");


scanf("%d", &x);

if (x <= 0) {
printf("Array size should be a positive integer.\n");
return 1;
}
int array[x];

printf("Enter the elements of array:\n");


for (i = 0; i < x; i++) {
printf("a[%d] = ", i);
scanf("%d", &array[i]);
}

printf("Entered array: ");


for (i = 0; i < x; i++) {
printf("%d\t", array[i]);
}
printf("\n");

insertionSort(array, x);

printf("Array after sorting:\n");


printArray(array, x);
return 0;
}

You might also like