space-complexity

Will Arrays.sort() increase time complexity and space time complexity?

↘锁芯ラ 提交于 2019-12-31 08:38:51
问题 There is an array related problem, the requirement is that time complexity is O(n) and space complexity is O(1). If I use Arrays.sort(arr) , and use a for loop to one pass loop, for example: public static int hello(int[]A){ Arrays.sort(A); for(int i=0;i<A.length;i++){ .................... } return ....; } So the loop will cost O(n) time. My question is: will Arrays.sort() cost more time? If I use Arrays.sort() , will this time complexity still be O(n)? And will Arrays.sort() cost more space?

Why does QuickSort use O(log(n)) extra space?

女生的网名这么多〃 提交于 2019-12-29 03:53:26
问题 I have implemented the below quicksort algorithm. Online I've read that it has a space requirement of O(log(n)). Why is this the case? I'm not creating any extra data structures. Is it because my recursion will use some extra space on the stack? If this is the case, is it possible to do it with less memory by not having it be recursive (instead making it iterative)? private static void quickSort (int[] array, int left, int right) { int index = partition(array, left, right); //Sort left half

Why does QuickSort use O(log(n)) extra space?

为君一笑 提交于 2019-12-29 03:53:02
问题 I have implemented the below quicksort algorithm. Online I've read that it has a space requirement of O(log(n)). Why is this the case? I'm not creating any extra data structures. Is it because my recursion will use some extra space on the stack? If this is the case, is it possible to do it with less memory by not having it be recursive (instead making it iterative)? private static void quickSort (int[] array, int left, int right) { int index = partition(array, left, right); //Sort left half

Time and Space Complexity of list to str conversion in Python

落花浮王杯 提交于 2019-12-23 23:18:29
问题 Trying to find out what is the time complexity of casting to string str([1,2,6,...,3,6]) Pretty sure it's O(1) Not sure how to verify. Edit: about space complexity, That should not be linear to list size, thinking O(1) because string has max size. 回答1: It's linear, because bigger lists need more time and memory to convert. Graph generated using perfplot. Code, for reference: import numpy as np import perfplot perfplot.show( setup=lambda n: np.random.choice(100, n).tolist(), kernels=[ lambda

Expected space consumption of skip lists

為{幸葍}努か 提交于 2019-12-22 09:28:41
问题 What is the expected space used by the skip list after inserting n elements? I expect that in the worst case the space consumption may grow indefinitely. Wikipedia says “Space O(n)”. How can this be proven one way or another? 回答1: According to this thesis, which I find more reliable then wikipedia, wikipedia is wrong . Probabilistic Skip List is Theta(nlogn) worst case space complexity. Despite the fact that on the average the PSL performs reasonably well, in the worst case its Theta(n lg n)

Implementation of LinkedList in python __getitem__() method

萝らか妹 提交于 2019-12-22 07:58:27
问题 I am implementing a LinkedList in python(3.7.4) and the code of the module is below :- LinkedList.py class Node: def __init__(self,value): self.value = value self.ref = None class LinkedList(Node): def __init__(self): self.__head = None self.__cur = None self.__count = 0 def add(self,value): if self.__head is None: self.__cur = Node(value) self.__head = self.__cur else: self.__cur.ref = Node(value) self.__cur = self.__cur.ref self.__count += 1 def getList(self): temp = self.__head while temp!

Python list.clear() time and space complexity?

荒凉一梦 提交于 2019-12-21 16:36:06
问题 I am writing a blogpost on Python list.clear() method where I also want to mention about the time and space complexity of the underlying algorithm. I expected the time complexity to be O(N) , iterate over the elements and free the memory? But, I found an article where it is mentioned that it is actually an O(1) operation. Then, I searched the source code of the method in CPython implementation and found a method which I believe is the actual internal implementation of list.clear() , however,

Why is the median-of-medians algorithm described as using O(1) auxiliary space?

落爺英雄遲暮 提交于 2019-12-21 07:02:46
问题 Wikipedia lists the median-of-medians algorithm as requiring O(1) auxiliary space. However, in the middle of the algorithm, we make a recursive call on a subarray of size n/5 to find the median of medians. When this recursive call returns, we use the returned median of medians as a pivot to partition the array. Doesn't this algorithm push O(lg n) activation records onto the run-time stack as a part of the recursion? From what I can see, these recursive calls to find successive medians of

Is the space complexity of this subset algorithm actually O(n)?

十年热恋 提交于 2019-12-20 05:48:09
问题 This is problem 9.4 from Cracking the Coding Interview 5 th The Problem: Write a method to return all the subsets of a set. Here is my solution in Java.(tested it, it works!!!) public static List<Set<Integer>> subsets(Set<Integer> s) { Queue<Integer> copyToProtectData = new LinkedList<Integer>(); for(int member: s) { copyToProtectData.add(member); } List<Set<Integer>> subsets = new ArrayList<Set<Integer>>(); generateSubsets(copyToProtectData, subsets, new HashSet<Integer>()); return subsets;

Is fixed array size O(n) or O(1) in space?

筅森魡賤 提交于 2019-12-20 02:55:03
问题 Is an array declared like this: int array[M] , O(1) in space or O(n) ? where M is some fixed value. To me O(n) makes sense because it is not just a single variable but an entire array. But then i think it could be O(1) since we have a fixed size and it is not changing! 回答1: If your array is of a fixed size and it does not vary with the size of the input it is O(1) since it can be expressed as c * O(1) = O(1) , with c being some constant. An example would be if you needed an array of size 5 to