Please help me in find in the time complexity of the following Insertion Sort ( I Did it in my way ) : | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Please help me in find in the time complexity of the following Insertion Sort ( I Did it in my way ) :

#include <stdio.h> void insertion_sort(int a[] , int n) { int i,j; for(i=1;i<n;i++) { int item = a[i]; j = i-1; while(j>=0) { if(a[j]<=item) { j=j+1; break; } j--; } if(j==-1) j=0; int k,temp; for(k=i;k>j;k--) { temp=a[k]; a[k]=a[k-1]; a[k-1]=temp; } a[j]=item; // Test Starts Here : int t; printf("\n Array After Sort %d : ",i); for(t=0;t<n;t++) { printf(" %d ",a[t]); } printf("\n"); } printf("\n\n Array After Complete Sorting : "); for(i=0;i<n;i++) { printf(" %d ",a[i]); } printf("\n\n"); } int main() { int arr[]={7,6,5,4,3,2,1}; int i; printf("\n Array Before Sort : "); for(i=0;i<7;i++) { printf(" %d ",arr[i]); } printf("\n\n"); insertion_sort(arr,7); return 0; }

12th Jan 2018, 9:44 AM
cool_boy12344
cool_boy12344 - avatar
1 Answer
+ 1
Without any guarantee your code seems to have O(n*n!) You have to calculate the worst and best Case
12th Jan 2018, 1:23 PM
I. Stark
I. Stark - avatar