forked from dimpeshmalviya/C-Language-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsertion_sort.c
More file actions
78 lines (60 loc) · 1.93 KB
/
insertion_sort.c
File metadata and controls
78 lines (60 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// Start from the second element of the array (index 1) and consider it as the “key”.
// Compare the key with all elements before it.
// Shift all elements that are greater than the key one position to the right.
// Insert the key in its correct position.
// Repeat steps 1–4 for all elements until the array is sorted.
//Sample Input :
//n=5
//arr = [4,2,5,1,3]
//Sample output
// arr = [1,2,3,4,5]
//Code
#include <stdio.h>
#include <stdlib.h> // Required for malloc and free
int main() {
int n, i, j, key;
int *arr = NULL; // Pointer to the array
printf("Enter the number of elements: ");
if (scanf("%d", &n) != 1 || n <= 0) {
printf("Invalid input for number of elements.\n");
return 1;
}
// 1. Dynamic Allocation using malloc()
// Allocate memory for n integers (n * sizeof(int) bytes)
arr = (int *)malloc(n * sizeof(int));
// Check if memory allocation was successful
if (arr == NULL) {
printf("Memory allocation failed.\n");
return 1; // Return an error code
}
printf("Enter %d elements: ", n);
for (i = 0; i < n; i++) {
if (scanf("%d", &arr[i]) != 1) {
printf("Invalid input for array element.\n");
free(arr); // Clean up allocated memory before exiting
return 1;
}
}
// --- Insertion Sort Algorithm ---
for (i = 1; i < n; i++) {
key = arr[i];
j = i - 1;
// Shift elements greater than key to the right
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j--;
}
// Insert the key into its correct position
arr[j + 1] = key;
}
// --- Output Section ---
printf("Sorted array: ");
for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
// 2. Freeing the memory
// Always release dynamically allocated memory when done
free(arr);
return 0;
}