My issue
please correct code
My code
#include <stdio.h>
void InsertionSort(int arr[], int n) {
for (int i = 1; i < n; i++) {
int key = arr[i];
int j = i - 1;
// Move elements of arr[0..i-1], that are greater than key, to one position ahead of their current position
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = key;
}
}
int main() {
int n;
scanf("%d", &n); // Read the number of elements in the array
int arr[n];
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]); // Read the elements of the array
}
insertionSort(arr, n); // Sort the array using Insertion Sort
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]); // Print the sorted array
}
printf("\n");
return 0;
}
Learning course: Analysis and Design of Algorithms
Problem Link: Insertion Sort in Analysis and Design of Algorithms