Sorting distinct elements in an array - algorithm

I have a number of distinct elements in an array and want to find those items first before sorting the array. I was thinking of using a hash table to find the elements, but is that possible since I then have to access the table to get the elements again? Am I on the right track?

Related

nearly sorted array using heap sort

I have an assignment and here is the question I struggle.
We are given an unsorted array A[1…n]. Now, imagine its sorted
version. The unsorted array has the property that each element has a
distance of at most k positions, where 0<k<=n, from its index in the
sorted version. For example, when k is 2, an element at index 5 in the
sorted array, can be at one of the indices {3,4,5,6,7} in the unsorted
array. The unsorted array can be sorted efficiently by utilizing a
Min-Heap data structure.
Question : . Generate the array sizes of 100,1000,10000,100000 and
note running times, show results.
I have implemented my solution in java.
here is my running time tables.
in the first table, k values are determined according to the array.
in the second table, k values are hold same.
running time of the kHeapSort algorithm
I was struggling about analyzing these tables.I couldn't figure out how array size and k effects the algorithm.Any hint would be helpful.Thank you already.

How to join two already sorted arrays into one sorted array in M$ Flow efficiently

Microsoft Flow doesn't support any sort function for arrays or lists.
For my problems I can use sort function within ODATA request to have some data presorted by the databases I'm accessing. In my case, I want to have a list of all start and end dates from a sharepoint calendar in a single array.
I can pull all dates sorted by the start date and I can pull all dates sorted by the end date into separate arrays. Now I have two sorted arrays which I want to join into a single array.
There are very few possibilites in iterating over an array. But the task has some properties which could ease the problem.
Two arrays,
both presorted by the same property as the desired final arrays.
same size.
Perhaps I'm missing some feature of the ODATA-request or there's a simple workaround. I'd prefer not to use REST-api or messing around with the JSON or manually, but if there's really an elegant solution I won't reject it.
I have a solution, but I don't think it is a good one.
Prerequesites are the two already sorted arrays and two additional arrays.
Let's call the two sorted arrays I have extracted from the sharepoint list array A and B.
And let's call the additional arrays array S1 and S2.
Then I set up a foreach-loop on array B.
Within that loop I filter array A for all elements lesser or equal to the current item of array B.
The output of the filter operation is saved to array S1.
current item of array B is appendet to array S1.
Again filter Array A for all elements, but this time for greater than the current item of array B.
save the output of the filter operation to array S2.
make a union from S1 and S2.
save the output of the union expression to array A.
As every element of array A has to be copied n times for a n-element array, the effort for processing two arrays of n elements is not quite optimal, especially if you consider both arrays already sorted in advance.
n² comparisons
2n²+n copy operations (not taking into account the imperfections of the implementation of flow)
If I'd implement a complete sort from scratch it would perform better, I think, but I also think, there must be other means to join two presorted arrays of compatible content.

golang find first element smaller or equal to given element using sort.Search

golang find first element smaller or equal to given element using sort.Search() if the array is sorted in ascending order.
Note: I do not want to sort the array in descending order to use the sort.Search
In your “less” function, implement “more” instead. You may need to adjust the resulting index by 1.

behaviour of dimension.top in case of array dimensions in crossfilter

What exactly is the behaviour of dimension.top in case of array dimensions?
It seems to be sorting on the first value in the array across all rows.
Shouldn't it be sorting on all values putting at top the record that includes the highest value anywhere in the array?
It should return an array with the same number of elements as there are array elements in all rows. Each row will appear once for each element in its array, ordered by the value of that element. If you are seeing different behavior, please post an example and I or someone else will be happy to take a look.

How can i write this algorithm so that it works stable

Assume that we have an Mystery-Sort(A), which takes an array A of
length n as input, sorts the numbers in A in non-decreasing order, and returns the sorted array.
We do not know whether the algorithm implemented by Mystery-Sort is stable.
I need a procedure that takes an array of n integers and returns the sorted array in non-decreasing order, but i need the procedure to be stable.
How can i achieve this the pseudo-code of a stable sorting procedure Stable-Sort(A), which pre-processes and/or postprocesses
the elements in A in O(n) time, makes only one call to Mystery-Sort, and returns
the sorted array in non-decreasing order.
I see this as coming in two phases: a pre-processing phase in which you find all duplicated elements with their identifiers; a post-processing phase where you simply overwrite the found elements into their original order.
You didn't specify how you can differentiate elements that sort as the same value; I'll call that the id. In this first pass, construct a table with one row per value. Iterate through the array; store the id of each element (or the entire element) in the matching table row in the table. If there's already an element there, extend that row and store the current element.
At this point, if you wish, you can eliminate any row of the table with fewer than 2 elements.
For the post-procesing, iterate through the sorted array. If the value you find is in the table, then don't trust the order returned from Mystery-Sort. Instead, simply overwrite the next elements with the ones from that row of the table. This restores their original order.
When you reach the end of the sorted list, you're done.

Resources