Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- int main() {
- int size, i, j, key;
- printf("Enter the size of the array: ");
- scanf("%d", &size);
- int arr[size];
- printf("Enter the elements of the array:\n");
- for (i = 0; i < size; i++) {
- scanf("%d", &arr[i]);
- }
- printf("Original array: ");
- for (i = 0; i < size; i++) {
- printf("%d ", arr[i]);
- }
- printf("\n");
- // Insertion Sort algorithm
- for (i = 1; i < size; i++) {
- key = arr[i];
- j = i - 1;
- while (j >= 0 && arr[j] > key) {
- arr[j + 1] = arr[j];
- j = j - 1;
- }
- arr[j + 1] = key;
- }
- printf("Sorted array: ");
- for (i = 0; i < size; i++) {
- printf("%d ", arr[i]);
- }
- printf("\n");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment