Write an insertion sort algorithm in C language by performing following steps .make sure code is work. thanks
#include <stdio.h> void insertionSort(int array[], int arraySize ) { int i, j, key; for( j = 1; j < arraySize; j++) { key = array[j]; for( i = j-1; i >= 0 && array[i] > key; i--) array[i+1] = array[i]; array[i+1] = key; } } int main() { int n = 5, i; int array[500]; printf("Enter size of array: "); scanf("%d",&n); printf("Enter %d numbers\n",n); for(i = 0;i<n;i++){ scanf("%d",&array[i]); } insertionSort(array, n); printf("\n\nSorted array:\n"); for(i = 0;i<n;i++){ printf("%d ",array[i]); } printf("\n"); return 0; }
Get Answers For Free
Most questions answered within 1 hours.