time-complexity

Analyzing shell sort algorithm (big O)

[亡魂溺海] 提交于 2020-03-21 06:58:12
问题 This is the shell sort algorithm. void shellSort(int array[], int n){ for (int gap = n/2; gap > 0; gap /= 2){ for (int i = gap; i < n; i += 1) { int temp = array[i]; int j; for (j = i; j >= gap && array[j - gap] > temp; j -= gap){ array[j] = array[j - gap]; } array[j] = temp; } } } I'm certain that the outer loop of this algorithm runs logn times but I'm not sure with the middle loop and the innermost loop. This site https://stackabuse.com/shell-sort-in-java/ said that the middle loop runs n

Analyzing shell sort algorithm (big O)

拜拜、爱过 提交于 2020-03-21 06:58:05
问题 This is the shell sort algorithm. void shellSort(int array[], int n){ for (int gap = n/2; gap > 0; gap /= 2){ for (int i = gap; i < n; i += 1) { int temp = array[i]; int j; for (j = i; j >= gap && array[j - gap] > temp; j -= gap){ array[j] = array[j - gap]; } array[j] = temp; } } } I'm certain that the outer loop of this algorithm runs logn times but I'm not sure with the middle loop and the innermost loop. This site https://stackabuse.com/shell-sort-in-java/ said that the middle loop runs n

how to reduce time taken

隐身守侯 提交于 2020-03-04 20:04:29
问题 #include <iostream> using namespace std; void rotateByOne(int arr[], int n) { int x = arr[0]; for (int y = 0; y < n - 1; y++) { arr[y] = arr[y + 1]; } arr[n - 1] = x; } int main() { int n, d; cin >> n >> d; int arr[n]; for (int i = 0; i < n; i++) { cin >> arr[i]; } while (d != 0) { rotateByOne(arr, n); d--; } for (int i = 0; i < n; i++) { cout << arr[i] << " "; } return 0; } How Do i Reduce the compile time of this code which is written to take an array input of n integers and rotate array

how to reduce time taken

僤鯓⒐⒋嵵緔 提交于 2020-03-04 20:03:26
问题 #include <iostream> using namespace std; void rotateByOne(int arr[], int n) { int x = arr[0]; for (int y = 0; y < n - 1; y++) { arr[y] = arr[y + 1]; } arr[n - 1] = x; } int main() { int n, d; cin >> n >> d; int arr[n]; for (int i = 0; i < n; i++) { cin >> arr[i]; } while (d != 0) { rotateByOne(arr, n); d--; } for (int i = 0; i < n; i++) { cout << arr[i] << " "; } return 0; } How Do i Reduce the compile time of this code which is written to take an array input of n integers and rotate array

Sorted squares of numbers in a list in O(n)?

不羁岁月 提交于 2020-03-02 16:54:16
问题 Given a list of integers in sorted order, say, [-9, -2, 0, 2, 3] , we have to square each element and return the result in a sorted order. So, the output would be: [0, 4, 4, 9, 81] . I could figure out two approaches: O(NlogN) approach - We insert the square of each element in a hashset. Then copy the elements into a list, sort it and then return it. O(n) approach - If there is a bound for the input elements (say -100 to -100), then we create a boolean list of size 20000 (to store -10000 to

Sorted squares of numbers in a list in O(n)?

风格不统一 提交于 2020-03-02 16:52:29
问题 Given a list of integers in sorted order, say, [-9, -2, 0, 2, 3] , we have to square each element and return the result in a sorted order. So, the output would be: [0, 4, 4, 9, 81] . I could figure out two approaches: O(NlogN) approach - We insert the square of each element in a hashset. Then copy the elements into a list, sort it and then return it. O(n) approach - If there is a bound for the input elements (say -100 to -100), then we create a boolean list of size 20000 (to store -10000 to

new [], delete [] complexity

余生长醉 提交于 2020-02-26 09:09:09
问题 I already know that the new[] operator first allocates memory and then calls the constructor for each element and that the delete[] operator first calls the destructor for each element and then frees memory and, because of that, they both have an O(n) time complexity. But if I have a class, for which I have not defined any constructor/destructor, will the complexity still be O(n), or will it be just O(1)? For instance, if I have two classes: class foo { public: int a; foo() { a = 0; // more

Example of Big O of 2^n

China☆狼群 提交于 2020-02-26 06:23:41
问题 So I can picture what an algorithm is that has a complexity of n^c, just the number of nested for loops. for (var i = 0; i < dataset.len; i++ { for (var j = 0; j < dataset.len; j++) { //do stuff with i and j } } Log is something that splits the data set in half every time, binary search does this (not entirely sure what code for this looks like). But what is a simple example of an algorithm that is c^n or more specifically 2^n. Is O(2^n) based on loops through data? Or how data is split? Or

TreeMap - Search Time Complexity

↘锁芯ラ 提交于 2020-02-21 09:46:31
问题 What is the time complexity of a get() and put() in a TreeMap? Is the implementation same as a Red-Black Tree? 回答1: From here: http://java.sun.com/javase/6/docs/api/java/util/TreeMap.html This implementation provides guaranteed log(n) time cost for the containsKey, get, put and remove operations 回答2: TreeMap is: A Red-Black tree based NavigableMap implementation. This implementation provides guaranteed log(n) time cost for the containsKey, get, put and remove operations. Algorithms are

What is the time complexity and space complexity of array[::-1]

笑着哭i 提交于 2020-02-04 05:19:05
问题 When reverse a list in Python, I usually use the array[::-1] for reversing and I know that a more common way might swap from two sides of the list. But I'm not sure the difference between these two solutions such as time complexity and space complexity. Code for this two methods below: def reverse(array): array[:] = array[::-1] def reverse(array): start, end = 0, len(array)-1 while start < end: array[start], array[end] = array[end], array[start] start += 1 end -= 1 回答1: In C python, assuming