How to find the kth minimum element in an array [on hold]



I am looking for an efficient algorithm to find the k'th minimum element in an unsorted array of n elements, where 1 <= k <= n. The obvious thing is to sort the array first, then pick the k'th element, which will result in a running time of O(n * log(n)) But I guess it can be done in a more efficient way, since sorting the array seems to do "too much". For example, for k=1 the task is to find the minimum of the array, which can be done in O(n).

Does anyone know a better algorithm for k>=2?


Related to : How to find the kth minimum element in an array [on hold]
how to find the maximum and minimum index of an element in a vector?
Programming Languages

I have data set with thousands values as following in a vector:

1, 1, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 6, 6, 6, 6,
6

The property of the data:

1) The values are consecutive integers by starting from 1.

2) The number of each value is not always the same.

The aim:

Find the minimum and maximum index of each value in the vector.The expected result should be:

 minimum index: [0, 3,5,9,14,16]
 maximum index: [2, 4,8,13,15,20]

Could some one give me easy solution? Thanks


JS: how can i find last and first viewing element in scroll element? [on hold]
Programming Languages

I making book reader web application. And i view all text of book in one element. For make page simulation i make scroll element. One page is the viewport of user. Some times text is broken. And i need to hide that text. How I can quickly find the breaking element? elementFromPoint is good, but it finds only parent element. I find a decision to check all elements position by recusive function by condition some think like this

if  (element.top < book.top && book.top <
element.bottom)

Its works, but it works without good performance for long books. How i can resolve it?

Demo: http://jsfiddle.net/SHfz4/14/ red text is broken and i want to get to him and hide him.


Which one is better median of median or randomized quick sort to finding kth smallest element in unsorted array?
Programming Languages

I was going through the question how to find kth smallest element in unsorted array. I read following algorithm

  1. using concept of randomized partitioning ( O(n) ) as per the cousera. randomized function guarantee not have worst case. so we come upd following equation.

    T(n) = T(n/2) + c ( on average)
    
  2. Median of median of median ( O(n))

    as per wiki

    T(n) = T(2n/10) + T(7n/10) + cn.
    

Both algorithm give o(n), so which one will be better? which alog is to use where, any example will be good.


Find the minimum absolute difference between sum of two sub array in an array
Programming Languages

I have an array with N elements (not sorted). Each element of the array can be any integer (positive or negative). Now I have to partitioned this array in a way that absolute(sum(sub_array1) - sum(sub_array2)) is minimal.

Example: A = {3, 4, 1, 2, -5}

partition1 = sub_array1 {3}, sub_array2 {4, 1, 2, -5} => abs(3-2) = 1

partition2 = sub_array1 {3, 4}, sub_array2 {1, 2, -5} => abs(7-(-2)) = 9

partition3 = sub_array1 {3, 4, 1}, sub_array2 {2, -5} => abs(8-(-3)) = 11

partition4 = sub_array1 {3, 4, 1, 2}, sub_array2{-5} => abs(10-(-5)) = 15

Answer = 1

I have achieved the solution with O(N^2), but I want to do it with at least O(Nlog(N)), and without threading (parallel solution).


Find the kth smallest element from 2 sorted arrays
Programming Languages

I came across the solution for this problem:

I would like to know the concept behind selecting the low and high values?

int findKthSmallest(int arr1[],int size1,int arr2[],int
size2,int k)
{
//check whether we have k element after combining both arrays.
assert(size1 + size2 >= k);
assert(k>0);
//find index low <= w < high such that w from arr1 and (k-w)
from arr2, are the smallest k numbers of resultant.
 int low = max(0 , k - size2 ); //define start point
 int high = min(size1 , k);//define end point

while (low < high)
{
    int w = low + (high - low) /2;
    //Can we include arr1[w] in the k smallest numbers that we are
finding?
    if ((w < size1) && (k-w > 0) && (arr1[w]
< arr2[k-w-1]))
    {
        low = w + 1;
    }
    else if ((w> 0) && (k-w < size2 ) &&
(arr1[w-1] > arr2[k-w]))
    {
        //can  we discard arr1[w -l] from the set of k smallest
numbers.
        high = w;
    }
    else
    {
        //Both the inequalities were false, break.
        low = w;
        break;
    }
}
if (low == 0)
{
    return arr2[k -1];
}
else if (low == k)
{
    return arr1[k-1];
}
else
{
    return max(arr1[low -1], arr2[k - low - 1]);
}
return 0;
}

How to find the kth minimum element in an array [on hold]
Programming Languages

I am looking for an efficient algorithm to find the k'th minimum element in an unsorted array of n elements, where 1 <= k <= n. The obvious thing is to sort the array first, then pick the k'th element, which will result in a running time of O(n * log(n)) But I guess it can be done in a more efficient way, since sorting the array seems to do "too much". For example, for k=1 the task is to find the minimum of the array, which can be done in O(n).

Does anyone know a better algorithm for k>=2?



Privacy Policy - Copyrights Notice - Feedback - Report Violation - RSS 2017 © bighow.org All Rights Reserved .