#include < stdio.h >
#include < stdlib.h >
int main() {
printf("Enter the total Elements");
int total;
scanf("%d", & total);
int arr[total];
for (int i = 0; i < total; i++) {
printf("\nEnter the number %d : ", i + 1);
scanf("%d", arr + i);
}
// insertion sort
for (int i = 1; i < total; i++) { // move the i to the left until it is small for(int j=i;j>0;j--){
if (arr[j] < arr[j - 1]) {
// swap
int tempVar = arr[j];
arr[j] = arr[j - 1];
arr[j - 1] = tempVar;
} else
break;
}
}
// print the sorted elements
for (int i = 0; i < total; i++) {
printf("%d,", arr[i]);
}
return 0;
}
Insertion Sort in C
