My professor assigned my class with implementing mergesort in arrays with 3-part partitioning and merging.
That was the exact question from the professor. Problem is I have found no such thing as a 3-way mergesort I only know of a 3-way quicksort so I thought that he probably meant to take an array, split it into 3 parts and then mergesort those 3 parts together and I'm doing this by mergesorting the first 2 parts together and then mergesorting the combined part with the 3rd part.
Did I think correctly and did I do the right thing (already implemented but I'm not posting the code since it doesn't have anything to do with my question) or have I understood wrong and there is something like a 3-way mergesort that I am not aware of.
The professor tends to give us assignments that have to do with stuff that we have not learned yet that's why I'm so sceptical about this and I looked as much as I could in Google etc.
Once you mergesorted the three sub arrays, you can merge them together in one go: compare the first elements of all three and place the smallest into the combined array, then take the next element from the array you took the smallest from and do the compariosn again until all subarray elements are accounted for.
Make sure you hande the case where there are only two elements in the array (so you cannot partition it into three non-empty parts). Also, in the above paragraph, one array will be empty before the others when merging, so you need to account for that too.
It is called a three part merge sort, because thats what it does.
Take a look at http://mathbits.com/MathBits/CompSci/Arrays/Merge.htm
Related
I have been searching for a visual representation of Merge Sort in the form of stack logic. Since we call the function recursively, I cannot shape in my mind that how we merge the sorted subarray further. As far as I understood, we first divide the problem into sub-problems. We do this by calling the MergeSort algorithm which works recursively by calling itself with the start and end index of the divided sub-arrays. Then we call the Merge function which sorts the array elements. Yet what I cannot understand is that after making the recursive calls, we come to the point of sorting the elements. In that part, we create 2 arrays with the size of the parameters that we sent from the function that works to divide the arrays into little pieces.
Well, after sorting the smallest array, how do we combine the new sorted array with the one that we had sorted?
If there is a problem with my question, I am really sorry. Since I'm confused and lost about this part, I have many questions in my mind. Therefore, I may not be clear.
Here is the visual representation of merge sort, please take a look and you will clearly understand the logic of merge sort. Link: https://www.hackerearth.com/practice/algorithms/sorting/merge-sort/visualize/
I've come across a problem that at first looks like the well known maximum sum subarray problem but there's a twist that makes things much more complex.
Suppose you have two arrays each containing the same amount of "1"s and "-1"s. Additionally, suppose each "1" in the first array has a corresponding or sibling "1" in the second array. Same for each "-1". The task is to find the optimal subarrays, one in the first array and one in the second, such that their combined sum is the largest (maximal) with the added constraint that an element in one subarray only counts towards the sum if the other subarray contains its sibling.
Anyone know what kind of problem this is? It looks like it could be a graph problem in disguise but I'm not sure which. If there's optimal substructure here I don't see that either. I know it can be solved by complete search but surely there's a faster way.
Below is an example of the setup to the problem.
Here the optimal solution is subarray [2..9] in the first array with subarray [4..9] in the second array for a sum of 8.
I've found answers to similar problems, but none of them exactly described my problem.
so on the risk of being down-voted to hell I was wondering if there is a standard method to solve my problem. Further, there's a chance that I'm asking the wrong question. Maybe the problem can be solved more efficiently another way.
So here's some background:
I'm looping through a list of particles. Each particle has a list of it's neighboring particles. Now I need to create a list of unique particle pairs of mutual neightbours.
Each particle can be identified by an integer number.
Should I just build a list of all the pair's including duplicates and use some kind of sort & comparator to eliminate duplicates or should I try to avoid adding duplicates into my list in the first place?
Performance is really important to me. I guess most of the loops may be vectorized and threaded. On average each particle has around 15 neighbours and I expect, that there will be 1e6 particles at most.
I do have some ideas, but I'm not an experienced coder and I don't want to waste 1 week to test every single method by benchmarking different situations just to find out that there's already a standard meyjod for my problem.
Any suggestions?
BTW: I'm using C.
Some pseudo-code
for i in nparticles
particle=particles[i]; //just an array containing the "index" of each particle
//each particle has a neightbor-list
for k in neighlist[i] //looping through all the neighbors
//k represent the index of the neighbor of particle "i"
if the pair (i,k) or (k,i) is not already in the pair-list, add it. otherwise don't
Sorting the elements each iteration is not a good idea since comparison sort is O(n log n) complex.
The next best thing would be to store the items in a search tree, better yet binary search tree, and better yet self equalizing binary search tree, you can find implementations on GitHub.
Even better solution would give an access time of O(1), you can achieve this in 2 different ways one is a simple identity array, where at each slot you would save say a pointer to item if there is on at this id or some flag defining that current id is empty. This is very fast but wasteful. You'll need O(N) memory.
The best solution in my opinion would be to use a set or a has-map. Which are basically the same because sets can be implemented using hash-map.
Here is a github project with c hash-map implementation.
And stack overflow answer to a similar question.
I'm struggling with one question on my assignment regarding bottom up merge sort.
Bottom Up merge sort divides an array into sub-arrays of two and sorts its members, then combines (merges) every two consecutive sub-arrays into another set of 4-sized sub-arrays, and so on until there are two arrays of size n/2, that merge into a completely sorted array.
I completely understand the algorithm but I'm having trouble with proving it formally using induction.
I'm supposed to prove its correctness under the assumption that n is a power of 2.
then I'm asked to calculate and prove its run time which is also by induction.
my current progress consists of proving that at each iteration i the number of sub arrays that are sorted is n/2^i,I'm not getting anywhere with that maybe because I'm looking at it in a wrong way.
Any guidance on how to prove this using induction?
I'm trying to create a program that will select the fastest sorting algorithm for a particular array of integers. I'm trying to check off the condition "is almost sorted," and was wondering what common practice to find this in the industry is.
Assume that there is a sorted array available to the coder. The two possible solutions I can think of are:
Loop through both lists simultaneously. Compare values at the index, find the percentage of correctly placed values. I understand that this is pretty quick (just O(N)), but it can be wildly inaccurate... what if everything is shifted by one space? This algorithm will give 0, but insertion sort will take a single run to do order this.
Find how far something is shifted from it's correct position in either direction (w/ wraparound). This seems to be a better solution, but could be pretty slow (O(N^2), since we might have to loop through a sorted list for every unsorted object, which could be corrected A BIT by comparing the value in a while loop).
Are there others? If not, which do I pick?
Thanks!