You are on page 1of 2

#include <math.

h>
#include <stdio.h>

void insertionSort(int number_list[], int n) {


int i, key, j;
int steps = 0; // Initialize the step counter
for (i = 1; i < n; i++) {
key = number_list[i];
j = i - 1;

while (j >= 0 && number_list[j] > key) {


number_list[j + 1] = number_list[j];
j = j - 1;
steps += 2; // Each assignment and comparison counts as a step
}
number_list[j + 1] = key;
steps += 1; // Increment the step count for the assignment
}
printf("Insertion Sort steps: %d\n", steps); // Print the total steps
}

void printArray(int number_list[], int n) {


int i;
int steps = 0; // Initialize the step counter
for (i = 0; i < n; i++) {
printf("%d ", number_list[i]);
steps += 1; // Each printf and array access counts as a step
}
printf("\nPrint Array steps: %d\n", steps); // Print the total steps
}

int main() {
int number_list_element;

printf("Enter number of elements to be printed: ");


scanf("%d", &number_list_element);
int number_list[number_list_element];

int steps = 0; // Initialize the step counter


for (int i = 0; i < number_list_element; i++) {
printf("Enter element number %d: ", i);
scanf("%d", &number_list[i]);
steps += 2; // Each printf, scanf, and array access counts as a step
}

printf("Input steps: %d\n", steps); // Print the total steps for input
insertionSort(number_list, number_list_element);
printArray(number_list, number_list_element);
return 0;
}

You might also like