We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Running Time of Algorithms
Running Time of Algorithms
+ 0 comments public static int runningTime(List<Integer> arr) { int n = arr.size(); int swaps = 0; for(int i = 0; i < n; i++) { for(int j = 0; j < n - i - 1; j++) { Integer e1 = arr.get(j); Integer e2 = arr.get(j + 1); if(e1 > e2) { //swap arr.set(j, e2); arr.set(j + 1, e1); swaps++; } } } return swaps; }
+ 0 comments Just find number of invesions !!!
+ 0 comments In case, you are looking for C Solution
int runningTime(int arr_count, int* arr) { int i, j, key, c=0; for (i=1; i<arr_count; ++i) { key = arr[i]; j = i-1; while(j >= 0 && arr[j] > key) { arr[j+1]=arr[j]; j=j-1; c++; } arr[j+1]=key; } return c; }
+ 0 comments def runningTime(arr): # Write your code here count=0 for i in range(len(arr)-1): j=i while arr[j]>arr[j+1]and j>=0: count+=1 #print(str(i)) #print(count) arr[j+1],arr[j]=arr[j],arr[j+1] j-=1 return count
+ 0 comments Python3
def runningTime(arr): shifts = 0 for i in range(1,len(arr)): key=arr[i] j=i-1 while j>=0 and key<arr[j]: arr[j+1]=arr[j] shifts +=1 j=j-1 arr[j+1]=key return shifts
Load more conversations
Sort 231 Discussions, By:
Please Login in order to post a comment