luc058.c (1273B)
1 /* 2 * Author : Amit Dutta <amitdutta4255@gmail.com> 3 * Date : 08 Feb 2026 4 * Repo : https://github.com/notamitgamer/bsc 5 * License : MIT License (See the LICENSE file for details) 6 */ 7 8 /* Implement the Insertion Sort algorithm shown in Figure 13.3 on a set of 25 numbers. 9 */ 10 11 /* Let Us C, Chap- 13 (Arrays), Qn No.: B(d) */ 12 13 /* This file is auto-generated by a bot. */ 14 /* This code is not compiled; it is for reference only. */ 15 16 17 #include <stdio.h> 18 #include <math.h> 19 #include <stdlib.h> 20 21 void insertion_sort(int *, int); 22 23 int main() 24 { 25 int arr[25], i; 26 27 printf("Enter 25 integers for Insertion Sort:\n"); 28 for (i = 0; i < 25; i++) 29 { 30 scanf("%d", &arr[i]); 31 } 32 33 insertion_sort(arr, 25); 34 35 printf("\nSorted Array:\n"); 36 for (i = 0; i < 25; i++) 37 { 38 printf("%d ", arr[i]); 39 } 40 printf("\n"); 41 42 return 0; 43 } 44 45 void insertion_sort(int *arr, int n) 46 { 47 int i, j, key; 48 for (i = 1; i < n; i++) 49 { 50 key = arr[i]; 51 j = i - 1; 52 53 /* Move elements of arr[0..i-1], that are greater than key, 54 to one position ahead of their current position */ 55 while (j >= 0 && arr[j] > key) 56 { 57 arr[j + 1] = arr[j]; 58 j = j - 1; 59 } 60 arr[j + 1] = key; 61 } 62 }