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