space-complexity

Merge Sort Time and Space Complexity

折月煮酒 提交于 2019-11-27 19:05:50
Let's take this implementation of Merge Sort as an example void mergesort(Item a[], int l, int r) { if (r <= l) return; int m = (r+l)/2; mergesort(a, l, m); ------------ (1) mergesort(a, m+1, r); ------------(2) merge(a, l, m, r); a) The time complexity of this Merge Sort is O(nlg(n)). Will parallelizing (1) and (2) give any practical gain ? Theorotically, it appears that after parallelizing them also you would end up in O(nlg(n). But practically can we get any gains ? b) Space complexity of this Merge Sort here is O(n). However, if I choose to perform in-place merge sort using linked lists

Regarding in-place merge in an array

僤鯓⒐⒋嵵緔 提交于 2019-11-27 07:40:10
I came across the following question. Given an array of n elements and an integer k where k < n . Elements { a 0 ... a k } and { a k +1 ... a n } are already sorted. Give an algorithm to sort in O( n ) time and O(1) space. It does not seem to me like it can be done in O( n ) time and O(1) space. The problem really seems to be asking how to do the merge step of mergesort but in-place. If it was possible, wouldn't mergesort be implemented that way? I am unable to convince myself though and need some opinion. deinst This seems to indicate that it is possible to do in O(lg^2 n) space. I cannot see

Is imperative Quicksort in situ (in-place) or not?

◇◆丶佛笑我妖孽 提交于 2019-11-27 06:42:49
问题 Quicksort is often described as an in situ (in-place) algorithm, despite the fact that it requires O(log n) stack space. So does in situ mean "requires less than O(n) additional space", or does stack space generally not count as space complexity (but why would that be the case?), or is Quicksort actually not an in situ algorithm? 回答1: is Quicksort actually not an in situ algorithm? The standard implementation of it is not in situ . It's a horribly common misconception, but you as correctly

How do i reduce the space complexity in Sieve of Eratosthenes to generate prime between a and b?

雨燕双飞 提交于 2019-11-27 06:29:38
问题 After getting through some of the SO posts, i found Sieve of Eratosthenes is the best & fastest way of generating prime numbers. I want to generate the prime numbers between two numbers, say a and b . AFAIK, in Sieve's method, the space complexity is O( b ). PS: I wrote Big-O and not Theta, because i don't know whether the space requirement can be reduced. Can we reduce the space complexity in Sieve of Eratosthenes ? 回答1: If you have enough space to store all the primes up to sqrt(b) then you

Finding contiguous ranges in arrays

一世执手 提交于 2019-11-27 05:29:34
问题 You are given an array of integers. You have to output the largest range so that all numbers in the range are present in the array. The numbers might be present in any order. For example, suppose that the array is {2, 10, 3, 12, 5, 4, 11, 8, 7, 6, 15} Here we find two (nontrivial) ranges for which all the integers in these ranges are present in the array, namely [2,8] and [10,12]. Out of these [2,8] is the longer one. So we need to output that. When I was given this question, I was asked to