sorted-array | John von Neumann 's sorted arrays
kandi X-RAY | sorted-array Summary
kandi X-RAY | sorted-array Summary
An implementation of John von Neumann's sorted arrays in JavaScript. Implements insertion sort and binary search for fast insertion and deletion.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
- Compares two strings .
- Set a class on the prototype
sorted-array Key Features
sorted-array Examples and Code Snippets
Community Discussions
Trending Discussions on sorted-array
QUESTION
What is the sorting algorithm most optimized for sorting an array that consists of 2 sorted sub-arrays?
This question came up when I was solving: https://leetcode.com/problems/squares-of-a-sorted-array/
My solution is as follows:
...ANSWER
Answered 2021-Mar-17 at 13:14In this case, the theoretical time complexity is O(n) because you don't need to sort at all (merely merge two ordered lists). Performing a sort generally has a O(NlogN) complexity.
Complexity and performance are two different things however. Your O(n) solution in Python code is competing with O(NlogN) in highly optimized low level C code. In theory there should be a point where the size of the list is large enough for the Python based O(n) solution to catch up with the O(NlogN) profile of the native sort but you can expect that this would be a very large number of elements (probably larger than your computer's memory can hold). If Python's native sort is smart enough to switch to a radix sorting algorithm for integers or if its sorting algorithm benefits from partially sorted data, it will be impossible to catch up.
QUESTION
I discovered this popular ~9-year-old SO question and decided to double-check its outcomes.
So, I have AMD Ryzen 9 5950X, clang++ 10 and Linux, I copy-pasted code from the question and here is what I got:
Sorted - 0.549702s:
...ANSWER
Answered 2021-Mar-07 at 22:18Several of the answers in the question you link talk about rewriting the code to be branchless and thus avoiding any branch prediction issues. That's what your updated compiler is doing.
Specifically, clang++ 10 with -O3
vectorizes the inner loop. See the code on godbolt, lines 36-67 of the assembly. The code is a little bit complicated, but one thing you definitely don't see is any conditional branch on the data[c] >= 128
test. Instead it uses vector compare instructions (pcmpgtd
) whose output is a mask with 1s for matching elements and 0s for non-matching. The subsequent pand
with this mask replaces the non-matching elements by 0, so that they do not contribute anything when unconditionally added to the sum.
The rough C++ equivalent would be
QUESTION
Suppose I do k groups. As the size of the input array is not constant, the number of elements in a group will be n/k. Sorting this will take (n/k)log(n/k). For k groups it will be (n)log(n/k) which is O(nlogn). Then how come the algorithm is of O(n)?
Edit: From Gfg https://www.geeksforgeeks.org/kth-smallestlargest-element-unsorted-array-set-3-worst-case-linear-time/amp/
...ANSWER
Answered 2021-Mar-09 at 06:48You are not sorting the elements in each group, you are only looking for their medians, which is O(n/k)
(this is theoretically done on small and constant values of n/k
, which is then just considered as O(1)
). So you have to run O(n/k)
algorithm k
times, which yields O(n)
.
Note that even if you do sort, since the algorithm uses a constant size of groups, then the size of each group: n/k = C
(for some constant C
), and you get: O(nlog(n/k)) = O(nlog(C)) = O(n)
QUESTION
This is a long question. I'm trying to reverse-engineer some basic Git functionalities, and am having some trouble wrapping my head around what git add
really does under the hood. I'm already familiar with the three trees of Git, and that the index file is not really a tree but rather a sorted-array representation of the tree.
My original hypothesis is as follows: when git add
is run,
- If
exists in working directory:
- Create an index file from that reflects state of in working directory
- Overwrite the relevant section of index file with this (sub-)index.
- If
exists only in current index file:
- This means has been deleted in working directory, so...
- Delete relevant section of index file that corresponds to .
- If
does not exist in working directory or index file:
fatal: pathspec <...> did not match any files
This hypothesis reflects a "do what you're told to do" git add
, that only looks at the path and registers changes at or under this path to the index file. For most cases, this is how the actual git add
seems to work.
But there are some cases that don't seem very straightforward:
1. Replacing a file with a directory ...ANSWER
Answered 2021-Mar-04 at 11:36Actually, this just means you have found a bug in (at least some versions of) Git.
Git understands that OSes cannot support two entities, one being a file and another being a directory/folder, with the same name. That is, we cannot have both file1
being a file and file1
being a directory.1
Now, the thing about Git's index is that it has no ability to hold directories in it at all.2 The only allowed entities are files. So either file1
exists, or else file1/subdir/something
exists, but never both. Git has a bunch of rather complicated code inside it, for both the index itself and for handling of OS-level files during git checkout
, git reset
, and the like, that is supposed to take care of "D/F" (directory/file) conflicts. Git needs to be able to handle these when doing a git checkout
of a commit where somefile
is a file, then git checkout
of a different commit where somefile/file
is a file so somefile
must be removed and a directory must be inserted. It needs to be able to handle the checkout where we go back to the first situation, so that somefile/file
must be removed, then somefile/
must be rmdir-ed, then somefile
can be created as a file. And, it must handle merges where somefile
was a file in one or two of the three commits but somefile/file
exists in the other two or one commits.
Apparently, someone missed a corner case. I was able to reproduce this myself, using your steps, and:
QUESTION
I'm looking for sorted container for JavaScript.
I'm using C++ std::set
, https://en.cppreference.com/w/cpp/container/set
and try porting my code to JavaScript.
JavaScript Map is not ordered container. I need some ordered container.
I don't need completely compatible container of std::set
on C++.
My requirements are
- Custom comparator support
- Automatically sorted
- Find the specific value. If value is not found, get the next value (insertion position value).
- Iterator increment/decrement operation (move to prev/next element)
Here is C++ code example that demonstrates my requirements: https://wandbox.org/permlink/wGnTvTPyOej4G9jo
...ANSWER
Answered 2021-Feb-04 at 11:36collctions/sorted-array-set
http://www.collectionsjs.com/sorted-array-set
It satisfies following requirement efficiently.
Custom comparator support. See http://www.collectionsjs.com/sorted-set constructor (top-right of the page).
Automatically sorted. It is obvious. The collection is sorted-set.
Find the specific value. If value is not found, get the next value (insertion position value). Use
findLeastGreaterThanOrEqual(value)
http://www.collectionsjs.com/method/find-least-greater-than-or-equal If you want to find the specific value, and if value is not found, get the previous value, then you can usefindGreatestLessThanOrEqual(value)
http://www.collectionsjs.com/method/find-greatest-less-than-or-equal Time complexity is O(logN).
It is inefficient but it also satisfies the following requirement.
- Iterator increment/decrement operation (move to prev/next element).
There are no iterators to access the sibling elements but you can use
findLGreatestLessThan(value)
http://www.collectionsjs.com/method/find-greatest-less-than to access the previous element, and can usefindLeastGreaterThan(value)
http://www.collectionsjs.com/method/find-least-greater-than to access the next element. The search is started from the root element of the tree. So each time to access to the sibling element, it requires O(logN) time complexity.
QUESTION
I am trying to solve the problem of finding the median of two sorted arrays on Leetcode but I am getting the following error: Line 811: Char 16: runtime error: reference binding to null pointer of type 'int' (stl_iterator.h). Can someone help me solve this error?
...ANSWER
Answered 2021-Jan-26 at 17:40Declare num3
as vectornums3(nums1.size() + nums2.size());
. Also in your merge second parameter needs to be nums1.end()
QUESTION
After I complete a Leetcode question, I always try to also determine the asymptotic time complexity, for practice.
I am now looking at problem 26. Remove Duplicates from Sorted Array:
Given a sorted array nums, remove the duplicates in-place such that each element appears only once and returns the new length.
Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.
Clarification:Confused why the returned value is an integer but your answer is an array?
Note that the input array is passed in by reference, which means a modification to the input array will be known to the caller as well.
Internally you can think of this:
...
ANSWER
Answered 2020-Oct-30 at 22:37For this problem, I got O(log n) from my research. Execution time halves for each time it's run. Can someone please verify or determine if I am wrong?
The execution time does not halve for each run: imagine an extreme case where the input has 100 values and they are all the same. Then at each level of the recursion tree one of those duplicates will be found and removed. Then a deeper recursive call is made. So for every duplicate value there is a level in the recursion tree. So in this extreme case, the recursion tree will have a depth of 99.
Even if you would revise the algorithm, it would not be possible to make it O(log n), as all values in the array need to be read at least once, and that alone already gives it a time complexity of O(n).
Your implementation uses splice
which needs to shift all the values that follow the deletion point, so one splice
is already O(n), making your algorithm O(n²) (worst case).
Because of the recursion, it also uses O(n) extra space in the worst case (for the call stack).
Are all recursive functions inherently O(logn)?
No. Using recursion does not say anything about the overall time complexity. It could be anything. You typically get O(logn) when you can ignore O(n) (like half) of the current array when making the recursive call. This is for instance the case with a Binary Search algorithm.
ImprovementYou can avoid the extra space by not using recursion, but an iterative method. Also, you are not required to actually change the length of the given array, only to return what its new length should be. So you can avoid using splice
. Instead, use two indexes in the array: one that runs to the next character that is different, and another, a slower one, to which you copy that new character. When the faster index reaches the end of the input, the slower one indicates the size of the part that has the unique values.
Here is how that looks:
QUESTION
I've been trying to wrap my head around recursion with regards to Binary Search Trees however, I'm having no luck. Could someone explain to me in the simplest of forms how this block of code (that's widely used from this problem) works on converting an array to a BST:
...ANSWER
Answered 2020-Oct-25 at 05:40helper(i,j)
is used to convert array[i:j+1]
to a BST.
QUESTION
Last night, i have tried some of this type of easy problems in leetcode. Though, till now i have tried almost 20 problems in leetcode, and all of them, examples and clarifications are well discussed or easy to understand. But when i tried this problem 26. Remove duplicates from sorted array, at first look, i tried to understand the description.
After a little bit, it becomes harder for me to well understood, then i tried to understand the examples and clarification, it seems more complex for me.
...ANSWER
Answered 2020-Oct-03 at 01:56As question said, you need to return distinct number of elements that is correct om your part, but another thing question mentioned is you need to.do it inplace, no extra memory overhead and you are clearly allocating new list instead of using old one.
In simple words use loops instead of built-in utilities/functionalities.
QUESTION
i don't understand this particular sample code from geeksforgeeks on k-way merge sort. More specifically, I don't understand what the "n" in the for loop in the base case for divide() is. Is it the number of students in each array? (which in this case is 4)
Also could you please explain what the for loop in divide() is doing in relation to the divide() process? + what is the base case (l==r) supposed to mean?
Thank you.
Approach: The idea becomes clear once we start looking at the k arrays as the intermediate state of the merge sort algorithm. Since there are k arrays that are already sorted, merge the k arrays. Create a recursive function which will take k arrays and divide them into two parts and call the function recursively with each half. The base cases are when the value of k is less than 3. See this article to merge two arrays in O(n) time.
...Algorithm: Initialize the output array with the size N*k. Call the function divide. Let l and r represent the range of arrays that are to be merged and thus vary between 0 to k-1. At each step, we call the left and right half of the range recursively so that, they will be sorted and stored in the output array. After that, we merge the left and right half. For merging, we need to determine the range of indexes for the left and right halves in the output array. We can easily find that. Left part will start from the index l * n of the output array. Similarly, right part will start from the index ((l + r) / 2 + 1) * n of the output array.
ANSWER
Answered 2020-Sep-20 at 14:36I think it's an advanced version of merge sort
. Here is the scenario, we have k
sorted arrays with n
elements (as you called them students) in each. The goal is to have a single sorted array (output
) of all the elements which is the size of k*n
. Since the k
arrays are sorted we just need an algorithm to merge them. To do so, in a recursive function divide(l, r, output, arr)
, we divide l-th
to r-th
arrays into two sets and recursively call divide for them. For example, for k=3
we have l=0
and r=2
. In the divide function since l!=r
we skip the first if and we call two functions divide(0, 1, output, arr)
and divide(2, 2, output, arr)
. Again, in the first function call, we call divide(0, 0, output, arr)
and divide(1, 1, output, arr)
. Now, when we have l==r
we should save the l-th (equal to r-th) array to output (Note that the the merge
function is an in place function). To do so, remember for converting a 2d
array A[n][m]
to a 1d
array B[n*m]
we can access to A[i][j]
with B[i*n+j]
. For more clarification, I put some prints in divide function:
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install sorted-array
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page