English, asked by khurshidabegum, 10 months ago

In data structures, what is the time complexity of the insertion sort algorithm?​

Answers

Answered by raj22052003
1

Answer:

The best case running time of the insertion sort is O(n). The best case occurs when the input array is already sorted. As the elements are already sorted, only one comparison is made on each pass, so that the time required is O(n). ... The worst case time complexity of insertion sort is O(n2)

Answered by harsh05572
2

Answer:

/* Function to sort an array using insertion sort*/

void insertionSort(int arr[], int n)

{

int i, key, j;

for (i = 1; i < n; i++)

{

key = arr[i];

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 = j-1;

}

arr[j+1] = key;

}

}

If we take a closer look at the insertion sort code, we can notice that every iteration of while loop reduces one inversion. The while loop executes only if i > j and arr[i] 2).

Similar questions
Social Sciences, 10 months ago