complexity-theory

What's the fastest algorithm for sorting a linked list?

纵然是瞬间 提交于 2019-11-26 14:03:05
I'm curious if O(n log n) is the best a linked list can do. It is reasonable to expect that you cannot do any better than O(N log N) in running time . However, the interesting part is to investigate whether you can sort it in-place , stably , its worst-case behavior and so on. Simon Tatham, of Putty fame, explains how to sort a linked list with merge sort . He concludes with the following comments: Like any self-respecting sort algorithm, this has running time O(N log N). Because this is Mergesort, the worst-case running time is still O(N log N); there are no pathological cases. Auxiliary

Are there any O(1/n) algorithms?

百般思念 提交于 2019-11-26 12:34:16
Are there any O(1/n) algorithms? Or anything else which is less than O(1)? Konrad Rudolph This question isn't as stupid as it might seem. At least theoretically, something such as O (1/ n ) is completely sensible when we take the mathematical definition of the Big O notation : Now you can easily substitute g ( x ) for 1/ x … it's obvious that the above definition still holds for some f . For the purpose of estimating asymptotic run-time growth, this is less viable … a meaningful algorithm cannot get faster as the input grows. Sure, you can construct an arbitrary algorithm to fulfill this, e.g.

Big-oh vs big-theta [duplicate]

那年仲夏 提交于 2019-11-26 12:20:25
问题 Possible Duplicate: What is the difference between Θ(n) and O(n)? It seems to me like when people talk about algorithm complexity informally, they talk about big-oh. But in formal situations, I often see big-theta with the occasional big-oh thrown in. I know mathematically what the difference is between the two, but in english, in what situation would using big-oh when you mean big-theta be incorrect, or vice versa (an example algorithm would be appreciated)? Bonus: why do people seemingly

What is Big O notation? Do you use it? [duplicate]

给你一囗甜甜゛ 提交于 2019-11-26 12:03:40
问题 This question already has an answer here: What is a plain English explanation of “Big O” notation? 39 answers What is Big O notation? Do you use it? I missed this university class I guess :D Does anyone use it and give some real life examples of where they used it? See also: Big-O for Eight Year Olds? Big O, how do you calculate/approximate it? Did you apply computational complexity theory in real life? 回答1: One important thing most people forget when talking about Big-O, thus I feel the need

How to find the lowest common ancestor of two nodes in any binary tree?

一个人想着一个人 提交于 2019-11-26 11:58:02
The Binary Tree here is may not necessarily be a Binary Search Tree. The structure could be taken as - struct node { int data; struct node *left; struct node *right; }; The maximum solution I could work out with a friend was something of this sort - Consider this binary tree : The inorder traversal yields - 8, 4, 9, 2, 5, 1, 6, 3, 7 And the postorder traversal yields - 8, 9, 4, 5, 2, 6, 7, 3, 1 So for instance, if we want to find the common ancestor of nodes 8 and 5, then we make a list of all the nodes which are between 8 and 5 in the inorder tree traversal, which in this case happens to be

Finding out the duplicate element in an array

ⅰ亾dé卋堺 提交于 2019-11-26 11:54:58
问题 There is an array of size n and the elements contained in the array are between 1 and n-1 such that each element occurs once and just one element occurs more than once. We need to find this element. Though this is a very FAQ, I still haven\'t found a proper answer. Most suggestions are that I should add up all the elements in the array and then subtract from it the sum of all the indices, but this won\'t work if the number of elements is very large. It will overflow. There have also been

A Regex that will never be matched by anything

雨燕双飞 提交于 2019-11-26 11:43:35
This might sound like a stupid question, but I had a long talk with some of my fellow developers and it sounded like a fun thing to think of. So; what's your thought - what does a Regex look like, that will never be matched by any string, ever! Edit : Why I want this? Well, firstly because I find it interesting to think of such an expression and secondly because I need it for a script. In that script I define a dictionary as Dictionary<string, Regex> . This contains, as you see, a string and an expression. Based on that dictionary I create methods that all use this dictionary as only reference

Is log(n!) = Θ(n·log(n))?

别来无恙 提交于 2019-11-26 11:29:54
I am to show that log( n !) = Θ( n ·log( n )) . A hint was given that I should show the upper bound with n n and show the lower bound with ( n /2) ( n /2) . This does not seem all that intuitive to me. Why would that be the case? I can definitely see how to convert n n to n ·log( n ) (i.e. log both sides of an equation), but that's kind of working backwards. What would be the correct approach to tackle this problem? Should I draw the recursion tree? There is nothing recursive about this, so that doesn't seem like a likely approach.. Mick Remember that log(n!) = log(1) + log(2) + ... + log(n-1)

Determining complexity for recursive functions (Big O notation)

流过昼夜 提交于 2019-11-26 11:26:07
I have a Computer Science Midterm tomorrow and I need help determining the complexity of these recursive functions. I know how to solve simple cases, but I am still trying to learn how to solve these harder cases. These were just a few of the example problems that I could not figure out. Any help would be much appreciated and would greatly help in my studies, Thank you! int recursiveFun1(int n) { if (n <= 0) return 1; else return 1 + recursiveFun1(n-1); } int recursiveFun2(int n) { if (n <= 0) return 1; else return 1 + recursiveFun2(n-5); } int recursiveFun3(int n) { if (n <= 0) return 1; else

Sorting in linear time? [closed]

别说谁变了你拦得住时间么 提交于 2019-11-26 09:03:13
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 6 years ago . Given an input set of n integers in the range [0..n^3-1], provide a linear time sorting algorithm. This is a review for my test on thursday, and I have no idea how to approach this problem. 回答1: Also take a look at related sorts too: pigeonhole sort or counting sort, as well as radix sort as mentioned by Pukku.